I am writing a program that creates thumbnails for each page in a large document. For performance reasons, I would like to keep thumbnails in memory for as long as possible, but I would like the OS to be able to return this memory if it decides that there is another important application for it (for example, the user started to run various applications.)
I can always restore the thumbnail later if the memory is gone.
Is there any cross-platform method for tagging memory since it can be removed if necessary? The program is written in C ++.
EDIT: just to clarify, instead of being notified of low memory or regularly monitoring the amount of system memory, I think more about memory allocation and then βunlockingβ it when not in use. The OS can then steal unlocked memory (even for disk buffers if it thinks it would be better to use memory), and all I have to do is because the programmer simply βlocksβ the memory again before I intend to use it, If lock failure, I know that the memory was reused for something else, so I need to restore the sketch again, and if the lock succeeds, I can just continue to use the data earlier.
The reason is that maybe 20 pages of the document may be displayed on the screen, but I can save thumbnails of the other 200 or so pages if the user scrolls a bit. But if they do something for a while, this memory can be better used as a disk cache or for storing web pages or something else, so I would like to tell the OS that it can reuse part of my memory, if he wants to.
The need to control the amount of free system-wide memory may not reach the goal (my memory will never be recovered to improve disk caching), and receiving notifications with small memory will help only in emergency situations. I was hoping that thanks to the lock / unlock method, this could be achieved in an easier way and would benefit the system from operating in a non-emergency situation.
source share