Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does MemoryCache know how much memory it uses?

The MemoryCache class in .net is supposed to evict items whenever the amount of memory it uses exceeds some limit.

How does it actually determine how much memory its items are using?

like image 417
Bradley Avatar asked Oct 11 '22 12:10

Bradley


1 Answers

MemoryCache has an internal CacheMemoryMonitor that periodically asks its references how big they are. It utilizes an internal type in mscorlib called SizedReference which is a reference that also keeps track of how much memory (approximately) its target uses.

It also collects various statistics from the GC to help ascertain if memory pressure should cause it to start an eviction process.

like image 168
dlev Avatar answered Oct 14 '22 05:10

dlev