Win32 / MFC: How to find free memory (RAM)?

Any suggestions / tips / links / tutorials would be appreciated! :)

0
source share
3 answers

There is really no answer. Under normal conditions, the OS will store something essentially in the entire system memory. Basically, as soon as he reads something in memory, he will keep a copy of it in memory until something else requires memory so that the first one is knocked out. There are a number of functions that can receive information about memory, but none of them even try to really return the amount of memory that is not fully used. The closest I know is GlobalMemoryStatusEx , which returns a number for the amount of available memory.

This means that there is currently both memory and disk in this memory, so a copy in memory can be erased without first having to write it to disk. For example, if you run the program, most of the code will remain in memory (for now, something else does not want memory) if you decide to run it again. Since this is just a copy of the program on disk, it can be discarded and (if necessary) reloaded from the disk, if necessary.

If you need more detailed information, you can use things like VirtualQueryEx to get it, but it usually overloads you with information about each block of memory used in this process, instead of giving a good, simple number saying "x bytes free".

+9
source

GlobalMemoryStatus / GlobalMemoryStatusEx

http://msdn.microsoft.com/en-us/library/aa366586(VS.85).aspx

+5
source

It is quite easy to answer, free RAM is always close enough to 0 to consider it zero and not disturb. Unused RAM is always used by the file system cache, this can be seen on the Performance tab of Taskmgr.exe.

If you really mean "free virtual memory", then the number that you really need, then the answer is "really impossible." You will need HeapWalk (), a very inconvenient and dangerous function to use. Only HeapWalk can detect blocks on the heap that are marked as free but are still displayed. The amount that you will receive is in any case pointless. The program never has enough free blocks of virtual memory; it always ends with blocks with a large amount of memory.

Detection of this condition is quite simple. Malloc returns NULL, the new operator throws std :: bad_alloc. Working with the condition is not easy. The solution takes less than two hundred dollars, approximately the license fee for the 64-bit version of Windows.

+3
source

All Articles