Assuming you are already on latest version (we did have some fix regarding clean up cache entries, like 1 year ago?), we don't have any further optimization on settings regarding cache/memory. Speaking from experience however it seems the underlying problem is somewhere else - something is allocating memory faster than it should. You can always reach out to developer support and ask to escalate. I will try to see if I can squeeze time for a quick look.
There are some options that you can use to configure default memory cache instance of IObjectInstanceCache. Here is reference link https://world.optimizely.com/documentation/developer-guides/archive/-net-core-preview/CMS/caching/monitoring-memorycache/
You can consider which value for MaxMemoryPressurePercentage and DefaultPollInterval is reasonalbe with your case first.
And here is priority of items in cache will be removed when exceeding threshold
You also can add more your own logic with cleaning up memory by injecting this interface IMemoryPressureEvents
One more thing that we can consider if your site is deployed in DXP and you do not see anything else could be optimized in your code is upgrading your DXP plan for autoscalling
Are you sure it is only MemoryCache allocating lots of memory?
If it mostly happens under high traffic, then it could also be caused by inefficient code. Like repeated string concatenations, LINQ to list enumerations, byte array instead of stream, and so on.
These could provoke GC freeing, even when the cache entries are reasonable.
I took a very quick look at the memory dumps provided in the ticket and my gut feeling is as if you are using some unmanaged library, most likely some server rendering like Chakra. is that a case? if yes there have been cases when those are source of memory leaks ... A long, long shot of course
Personally, I don't think adding/freeing memory is the main fix. Symptoms I see looks more aligned with app just waiting for available thread or crashing due to too many threads used. So fix would probably be getting "stronger" containers with more cores/CPU-power available and/or working through code and remote API:s getting faster responses. Or just adding more instances to raise the ceiling by balancing load...
My feeling is also that modern .NET on Linux is not as stable as on Windows. When we have the choice we for now use Windows on Azure for other apps. Some findings that could support this theory and a GH issue where others are also chosing Windows and getting better results:
Anyone have some experience of MemoryCache optimization (priorities or other entry options, compacting manually, set memory limit?) in new dotnet versions (8 on DXP in our case)?
We have support reporting this finding in a memory dump:
- From the dump file, we see the SQL connection has to wait GC to finish (gc_heap::try_allocate_more_space).
Then we got a list of item types in the cache. Sizes reported are reasonable to me but it seems like when app is under stress freeing up memory takes time and blocks other work.