R – Making a memory intensive background application “friendly”

cachingmemory-managementnetwindows

I have an application that periodically needs to process large blocks of data with a computationally trivial algorithm. It turns out I can also prevent slowing down the system from hard drive accesses by keeping the blocks of data in a memory cache. The application is a low-priority application so I'm working to minimize its impact on the system across the board, which means using extra memory (when available) to reduce the load on the CPU and hard drives. The cached data is just 64MB blocks of bytes, and the more of them I have in memory the less overhead the program will have on the drives.

What I need to do is dump the in-memory cache whenever any other application on the system needs more physical memory than is available, and do so fast enough that the user never feels the system slowing down due to high memory demands.

I'm particularly interested in how this would be accomplished in a .NET application.

Best Answer

One option would be to use the ASP.NET Cache, which scavenges items in response to low memory. While Microsoft attaches a big warning that it is only tested within an ASP.NET application, there's nothing to stop you accessing HttpRuntime.Cache in any application, and in practice it has worked when I've done so.

If that feels dirty and wrong (or just doesn't do quite what you need), we can at least draw inspiration from how ASP.NET knows to scavenge the cache. It periodically calls the kernel's GlobalMemoryStatusEx to find out the memory available. Specifically, the dwMemoryLoad property of the returned structure is the percentage of total memory in use. By default, ASP.NET considers memory to be running out when this reaches 90%.

There's some example code to call this yourself here.