I’ve been looking and trying to figure out how to use MemoryCache (Microsoft.Extensions.Caching.Memory).
From what I read the MemoryCache checks for expired items when you access them (Sliding and Absolute expiration). But does it also check when you add items?
Is there a limit to it when it comes to RAM? Say I have a server with 4GB, will the MemoryCache continue to add items until the RAM is full and eventually cause crashes / instabilities? Or will it self compact before that happens?
The Size/SizeLimit properties are apparently of own measures, no unit, meaning I can’t say “Limit this MemoryCache to 1GB”, and it won’t go above that. How would one use MemoryCache if there are multiple sites on a server and I want to limit each site to 1GB RAM for the MemoryCache?


Or is there a better solution to MemoryCache for .NET 5?
Memory cache does not self-compact. It has Compact method that you can call manually.
There is virtually no chance to get out-of-memory on x64 system.
Current implementation will check expirations on add
How would one use MemoryCache if there are multiple sites on a server and I want to limit each site to 1GB RAM for the MemoryCache?
You can’t limit sites to “size of memory cache”, only to “RAM of process”. “Size of memory cache” is impossible to track metrics, because it stores only pointers to data. But that data can be something like interned strings, that are shared between multiple entries.
Doesn’t all .NET have a memory cap of 2 gigs whether it’s 32 or 64 bit? Has that changed?
Or is there a better solution to MemoryCache for .NET 5?
There’s practically nothing for alternatives.
The Size/SizeLimit properties are apparently of own measures, no unit, meaning I can’t say “Limit this MemoryCache to 1GB”, and it won’t go above that. How would one use MemoryCache if there are multiple sites on a server and I want to limit each site to 1GB RAM for the MemoryCache?
Like /u/Alikont mentioned, tracking the actual allocated size of everything in cache is a very difficult, if not practically impossible, problem.
System.Runtime.MemoryCache did track an approximate size, but you have to use reflection to drill down through private fields (._stats._cacheMemoryMonitor._sizedRef.ApproximateSize iirc) but that can still be wildly incorrect.
One memory caching technique that can provide exact sizes, and has some other pros and cons, is to serialize all objects to binary and store blobs in memory, deserializing to rehydrate objects when retrieved.
NFX Cache can provide an example of that, but it’s not written in a manner that makes it anywhere near easy to plug in and use (it’s intermingled with an entire app framework)


C# devs
null reference exceptions

source

Categories: .NetCodingTech