How determine amount of free virtual memory available for mallocs?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi.  I have a Visual C++ application.  We run on 32-bit versions of Vista and XP.  The application uses very large amounts of in-memory data (mallocs).  The user needs a gauge that indicates how much (virtual) memory is left for possible use.   The user will look at the gauge to make decisions.   The value must include all free virtual memory that is available for the process to malloc, that is, it should NOT include free memory that is dedicated to the "operating system" (in the 1 GB or 2 GB "OS only" region of VM space).<br/><br/>Ive researched this on the forums, and found two ways to obtain the amount of free virtual memory available for my application to malloc.  BTW: the mallocs are many medium-sized mallocs, not one huge malloc.  Both ways seem to work okay, but neither is very precise.   The two ways are shown below in code snippets.<br/><br/>My question is:  Is there a more accurate way to compute the total amount of Virtual memory remaining (available) for my application to malloc (in many medium-sized blocks)?    The code doesnt need to execute fast, so an algorithm that loops thru some memory-heap free-pool block by block and sums up the total is okay.<br/><br/>Thanks in advance for any replies,<br/>  noleander<br/><br/>------------ Code snippets that work okay, but are not too accurate ----------------
1) <br/>    MEMORYSTATUSEX statex;<br/>    statex.dwLength = sizeof (statex);<br/>    bool success2 = GlobalMemoryStatusEx (&statex);<br/>    if ( success2 ) {<br/>        float availApprox = float (statex.ullAvailVirtual);  <br/><br/><br/>AND<br/><br/>2)    <br/>    PROCESS_MEMORY_COUNTERS lMemInfo; <br/>    bool success = GetProcessMemoryInfo( <br/>        GetCurrentProcess(), <br/>        &lMemInfo,<br/>        sizeof(lMemInfo)<br/>        ); <br/>    if ( success ) {<br/>        float memInUse = float ( lMemInfo.PagefileUsage );<br/>        MEMORYSTATUSEX statex;<br/>        statex.dwLength = sizeof (statex);<br/>        bool success2 = GlobalMemoryStatusEx (&statex);<br/>        if ( success2 ) {<br/>            float totalVM =  float (statex.ullTotalVirtual); // 2 GB or 3 GB<br/>            float availableMem = totalVM - memInUse; 

View the full article
 
Back
Top