SystemEvents.LowMemory

SystemEvents.LowMemory doesn't seem to work, at least not the way it's documented.

 

I wrote a little app that added a handler for the LowMemory event:


SystemEvents.LowMemory += new EventHandler(SystemEvents_LowMemory);

And then went mad allocating memory:



// Go mad allocating memory
int iters = 0;
while (true)
{
   // Add a meg
   myJunk.Add(new byte[1024*1024]);
   iters++;
   Console.WriteLine("Allocated " + iters.ToString() + " mb");
}


It got up to 1035mb (which is past when I would have liked the event to fire) and then threw an OutOfMemoryException (without hitting the breakpoint I had in the SystemEvents_LowMemory handler.

 

I'd like to experiment with using “free“ memory (memory the system doesn't currently have another use for) as cache in my own apps, but I need to know when the system wants some memory back.  Maybe there's another way.