Control amount of memory in a thread C#

Is it possible to control the amount of memory to be used in a method, if reaching a specific amount drops the thread?

I have a problem that occasionally generates memory leak, and if a leak occurs I want to bring down the thread.

This problem occurs from a third party component, until they fix it i wanted to have a solution.

Upvotes: 1

Views: 221

Answers (1)

Thomas Weller
Thomas Weller

Reputation: 59208

Short answer: No. Memory belongs to the process (in general) or in .NET at least to the AppDomain. CPU time belongs to threads.

Long answers:

a) There is ThreadLocalStorage, which was made for storing stuff that relates to a thread. In .NET it's ThreadLocal<T>. But since you can't modify the library, it will not make use of it.

b) If you implement your own memory management, you could potentially check on which thread memory is allocated. It seems possible, but I would not recommend it. That's more a thing for C and C++, where you could easily use a #define on new and malloc() to override how memory is allocated on the heap.

c) if your library is a native library (or a wrapper for a native library), also b) is useless, since memory allocations will take a diferent way.

Upvotes: 4

Related Questions