1

I have a fairly large cache of intermediate results that I would like to flush when my application is running low on memory. I found SystemEvents.LowMemory as a possible means of detecting the low memory situation, however this method is mark obsolete and hence I do not want to use it. I've not been able to find an alternative.

Microsofts documented obsolete member page unfortunately does not provide a recommended alternative to this event.

https://learn.microsoft.com/en-us/dotnet/framework/whats-new/obsolete-members

  • A different approach here - without having to look for memory consumption and total available memory - would be to determine an upper limit of how large your cache can grow - for example 10k entries - and then throwing away the older entries. Or record when an entry was last requested and if it hasn't been requested in some time remove it from the cache. – Steffen Winkler Jun 27 '19 at 13:53
  • 1
    I already retire stale cache elements when a certain watermark is hit. I'm trying to be adoptive on what that watermark should be. Unfortunately the estimated memory usage of my cached can not be done my counting the items in the cache. Cache items are somewhat complex object structures. I would really like to avoid having to create a "memsize" method to estimate size. – Eddie Wyatt Jun 27 '19 at 14:02
  • There is `System.GC.GetTotalMemory(false)` to see how much memory your process is currently using for managed objects. That is only half the answer though (if even that) as it doesn't tell you how much memory the machine has in total. There are some answers to that question [here](https://stackoverflow.com/questions/105031/how-do-you-get-total-amount-of-ram-the-computer-has). – Steffen Winkler Jun 27 '19 at 15:06
  • 1
    Physical memory does not directly determine how much memory is available to a process – paging as an example. So knowing the amount of physical memory is not helpful. Something as simple as whether it’s it runs as a 32 or 64 bit app changes the amount of addressable memory. My caching already tweaks its cache size base on whether its running as a 32 or 64 bit application. private xxxCache() { if (System.Environment.Is64BitProcess) Watermark = 10_000; else Watermark = 4_000; } – Eddie Wyatt Jun 27 '19 at 18:29

0 Answers0