Out of memory handling

I have an object that behaves like a cache. If necessary, it can fill in all available memory.

I would like to return the memory back to the operating system. Therefore, if there is a problem with any other distribution, the VM (or the GC or anyone else) should ask my "cache" to be compressed before the failure, with the exception of OutOfMemory.

How can I do this in C #?

+3
source share
4 answers

I would recommend using the built-in cache instead of reinventing the wheel. It will automatically take care of such situations: the items expire, depending on their priority, if the memory starts to work at a low level. It is also very extensible, and you can easily distribute it among popular vendors such as memcached .

Please note that it System.Runtime.Cachingis only available in .NET 4.0. Other caches are available in earlier versions, such as the corporate library, System.Web.Caching, Velocity, ...

+9
source

; , . , , , ..

0

Very interesting question. Have you tried playing with WeakReferences ? This seems to be exactly what you need.

0
source

I would suggest storing objects in the cache for a limited time and use the built-in cache.

0
source

Source: https://habr.com/ru/post/1783379/


All Articles