Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reserve memory for my application and leave a specified amount remaining?

I'm planning an application which will involve loading many pictures at one time and thus requires a large chunk of memory. For example, I might have 50 image objects created at once, taking a total of 1GB of RAM. But when the user goes to load 20 more pictures, I'd like to make sure that amount of memory is already reserved and ready.

Now this part might seem a little backwards from normal. Rather than specifying how much memory my application shall reserve, instead I need to specify how much memory to leave free for other applications, and adjust my application's memory periodically according to this specification. I must say I've never worked with reserving memory at all, and especially won't know how to leave this remaining available memory.

So for example, if the computer has 2048 MB of RAM, and the option is set to leave 50 MB free for other applications, and there is already 10MB of RAM being used by other apps, then it should reserve 2048-50-10 = 1988 MB for my app.

The trouble I foresee is suppose the user opens another application which requires 1GB. My app has to catch this and shrink its self.

Does this even sound like a feasible approach? Basically, I need to make sure there is as much memory reserved as possible at any given time, while leaving a decent amount available for other apps. Would it make a significant impact on performance if I do this, or not much at all? I might be loading and unloading images at rapid paces, and I don't want it to reserve/free this memory on demand, I want it to stay reserved.

like image 354
Jerry Dodge Avatar asked Dec 23 '11 22:12

Jerry Dodge


2 Answers

+1 for Sertac's mentioning of how SQL Server rides the line of allocating memory it needs, but releasing memory when Windows complains.

Applications can receive Window's complaints by using the CreateMemoryResourceNotification:

hLowMemory := CreateMemoryResourceNotification(LowMemoryResourceNotification);

Applications can use memory resource notification events to scale the memory usage as appropriate. If available memory is low, the application can reduce its working set. If available memory is high, the application can allocate more memory.

Any thread of the calling process can specify the memory resource notification handle in a call to the QueryMemoryResourceNotification function or one of the wait functions. The state of the object is signaled when the specified memory condition exists. This is a system-wide event, so all applications receive notification when the object is signaled. Note that there is a range of memory availability where neither the LowMemoryResourceNotification or HighMemoryResourceNotification object is signaled. In this case, applications should attempt to keep the memory use constant.

But it's also worth mentioning that you might as well allocate memory that you need. Your operating system has a very sophisiticated set of algorithms to swap out the least used memory when memory pressure is high. You can take advantage of this by simply allocating all the memory that you need. When Windows starts to run low, it will find those pages of memory that you are using the least and swap them out to disk. (This is how a well-known reverse proxy works).

The only thing left is to decide if you want to free some images when Windows says it's running low on RAM. But if you're not using the memory, it is going to be swapped out to disk for you.

like image 153
Ian Boyd Avatar answered Nov 15 '22 20:11

Ian Boyd


It's not realistic to account for other apps. Just ignore them. The system will page things in and out as needed. If you really wanted to do this you'd have to dynamically adapt to other processes as they start and finish. That's really not realistic. What's more it's not practical to inquire of other processes how much memory they need. Leave it all to the system.

Set a budget for your app and make sure you don't exceed it. Keep the most recently used images in memory and when you approach your memory budget throw away the least recently used images to make space.

If you are stressing the available resources then make sure you use FastMM and enable LARGE_ADDRESS_AWARE for your app so that you get 4GB address space when running on a 64 bit OS.

like image 29
David Heffernan Avatar answered Nov 15 '22 19:11

David Heffernan