Allocation of memory that can be freed up by the OS if necessary

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.

+4
source share
4 answers

Is there any cross-platform method for tagging memory since it can be removed if necessary? The program is written in C ++

For Windows, at least you can register for notification of memory resources .

HANDLE WINAPI CreateMemoryResourceNotification( _In_ MEMORY_RESOURCE_NOTIFICATION_TYPE NotificationType ); 

NotificationType

  • LowMemoryResourceNotification Available physical memory is low.
  • HighMemoryResourceNotification Available physical memory is high.

Just be careful when responding to both events. You can create a feedback loop (memory low, release thumbnails !, and then memory high, make all thumbnails!).

+4
source

AIX has a SIGDANGER signal, which is sent to applications when available memory is low. You can process this signal and free some memory.

There is a discussion among Linux users to implement this feature on Linux. But AFAIK, it is not yet implemented on Linux. Perhaps they believe that the application should not care about the low level of memory management, and it can be transparently processed in the OS through exchange.

In the posix_madvise standard, the posix_madvise function can be used to designate a memory area as less important. There is a tip POSIX_MADV_DONTNEED indicates that the application expects that it will not gain access to the specified range in the near future.

But unfortunately, the current Linux implementation will immediately free up the memory range when posix_madvise is called with this advice.

Thus, there is no portable solution to your question.

However, on almost every OS, you can read the current available memory through some OS interface. Thus, you can regularly read this value and automatically free memory when the available memory in the OS is low.

+2
source

You don’t have to do anything special. The OS will delete things from memory if they have not been used recently automatically. Some operating systems have platform-specific ways to improve this, but usually nothing special is required.

0
source

This question is very similar and has answers that cover things that are not covered here. Allocation of "temporary" memory (on Linux)

This should not be too difficult to do, because this is exactly what the page cache does, using unused memory to cache the hard drive. Theoretically, someone could write a file system so that when reading from a specific file, he computes something, and the page cache will cache it automatically.

All the basics of the automatically freed cache space are already present in any OS with a disk cache, and it’s hard to imagine that the API does not really matter for anything, especially in things like mobile web browsers.

0
source

Source: https://habr.com/ru/post/1485129/


All Articles