Dot Net MemoryCache Eviction

When does the eviction of points on the MemoryCache network occur? How can I simulate eviction in a console application? Whenever I try to add objects to the memory cache until an eviction occurs, I get an OutofMemoryException instead.

+4
source share
1 answer

See MemoryCacheElement, this is what controls the default behavior if you don't pass values ​​in NameValueCollection config in the constructor or you use the default instance.

Looking at the default values ​​for MemoryCacheElement, it checks every two minutes (however, it goes faster the closer you are to the high pressure limit ). Once inside a timer callback, it will recoup the percentage to trim the MemoryCache, and then it will call MemoryCache.Trim(Int32)using the calculated percentage.

One thing to note in percentage calculation , if there are no Gen 2 garbage collectors, the cache does not try to compress.

It is very possible that how your test console program works, it used all the memory before the Gen 2 collection could happen or was still in the initial two-minute slow mode to check the memory pressure before it could clear the items from.

If you want to simulate eviction, just call

MemoryCache.Default.Trim(50);

.

+2

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


All Articles