Low memory detection BEFORE distributions in Windows

We have an application that can potentially highlight a large number of small objects (depending on user input). Sometimes an application runs out of memory and crashes efficiently.

However, if we knew that memory allocations became hard, there are some objects with a lower priority that can be destroyed and, thus, allow graceful degradation of user results.

What is the best way to detect that the memory for a process is running low before calls to the "new" ones really don't work? We could call API functions like GetProcessWorkingSetSize() or GetProcessMemoryInfo() , but how do you know when limits are reached on a given machine (for example, with 80% of the maximum allocations)?

+4
source share
3 answers
  • At startup, allocate a memory reserve.
  • Then use set_new_handler () to set the hook that will detect distribution failures.
  • When happens:
    • Free up the reserve (so that you have enough free memory to work with).
    • Run code that finds and releases low priority objects.
    • When this does its job, try redistributing the reserve again (next time).
    • Finally return to try again to re-post.
+4
source

If this is a 32-bit process, you should make sure that you are not using more than 1.6 GB, which is 80% of 2.0 GB, the maximum allowed for your process. A call to GlobalMemoryStatusEx fill the struct MEMORYSTATUSEX.ullAvailVirtual , if it's only 400 MB (or less), then you are on the verge.

+2
source

Check this answer Win32 / MFC: How to find free memory (RAM)? .

You need to periodically find free memory and stop the allocation at a certain limit. As explained in the above answer, you can use GlobalMemoryStatusEx and / or VirtualQueryEx .

0
source

All Articles