Making the background application memory saturated

I have an application that periodically needs to process large blocks of data using a computationally trivial algorithm. It turns out that I can also prevent the system from slowing down from accessing the hard drive by storing data blocks in the memory cache. The application is a low-priority application, so I try to minimize its impact on the system in all directions, which means using additional memory (if available) to reduce the load on the processor and hard drives. Cache data is only 64 MB of blocks of bytes, and the more they have in memory, the less program overhead will be on the program.

What I need to do is flush the cache in memory when any other application in the system needs more physical memory than is available, and do it fast enough so that the user never feels that the system is slowing down due to high memory requirements .

I am particularly interested in how this will be implemented in a .NET application.

+4
source share
3 answers

One option is to use the ASP.NET cache, which removes items in response to low memory. Although Microsoft gives a big warning that it is only tested in an ASP.NET application, you have nothing to stop accessing HttpRuntime.Cache in any application, and in practice it worked when I did it.

If this seems dirty and wrong (or just not exactly what you need), we can at least draw inspiration from how ASP.NET knows to clear the cache. It periodically calls the GlobalMemoryStatusEx kernel to find out the available memory. In particular, the dwMemoryLoad property of the returned structure is a percentage of the total used memory. By default, ASP.NET thinks that memory runs out when it reaches 90%.

Here is sample code to call it here .

+2
source

You can use P / Invoke for the Win32 CreateMemoryResourceNotification API with the LowMemoryResourceNotification parameter. This returns a notification object that you can either poll (using QueryMemoryResourceNotification) or wait (in the background thread, you will need to use one of the Win32 wait methods and not the .NET wait method, or perhaps get a custom class from WaitHandle to encapsulate the object Win32). You will respond by dropping blocks from your cache and forcing garbage collection; youโ€™ll need to check if itโ€™s enough that the user never feels that the system is slowing down. "

In addition, looking at the documents, the threshold for receiving notifications with low memory is really low memory (32 MB on a 4 GB system!). I suspect that by that time the user may already feel a slowdown, and swapping your application in memory to reset its blocks may cause noticeable disk access. Again, you can appreciate this when testing.

+1
source

You can use the WeakReference class to weakly reference memory blocks. This allows the garbage collector to remove objects if necessary.

Yor objects will be on the heap of large objects, as they are larger than 85 kb. The most common garbage collection considers only the first generation heap, so it will not collect your objects. If the system notifies the .NET system that it is running out of memory, the garbage collector will perform more thorough collection, deleting your objects in a bunch of large objects.

-one
source

All Articles