Memory Information
GlobalMemoryStatusEx
The GlobalMemoryStatusEx function retrieves information about the current system's memory usage, including available and total memory. This function is useful for applications that need to monitor or adapt to memory conditions.
Syntax
BOOL GlobalMemoryStatusEx(
LPMEMORYSTATUSEX lpBuffer
);
Parameters
lpBuffer
A pointer to a MEMORYSTATUSEX structure that receives and contains the current memory status information. The dwLength member of this structure must be initialized to sizeof(MEMORYSTATUSEX).
Return Value
If the function succeeds, the return value is non-zero. If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
The MEMORYSTATUSEX structure provides a snapshot of memory utilization. The values it returns can change rapidly, so they might not be accurate by the time the application uses them. For most applications, the precise details of memory usage are not critical; it is generally sufficient to know if memory is available.
The dwLength member of the MEMORYSTATUSEX structure must be set before calling this function.
MEMORYSTATUSEX Structure
| Member | Description |
|---|---|
dwLength |
Specifies the size of the structure, in bytes. This member must be set to sizeof(MEMORYSTATUSEX). |
dwMemoryLoad |
A number between 0 and 100 that specifies the approximate percentage of physical memory that is in use. |
ullTotalPhys |
The total size of physical memory, in bytes. |
ullAvailPhys |
The available physical memory, in bytes. |
ullTotalPageFile |
The total size of the virtual memory paging file, in bytes. |
ullAvailPageFile |
The available virtual memory for the paging file, in bytes. |
ullTotalVirtual |
The size of the user-mode portion of the virtual address space of the calling process, in bytes. This member is not used. |
ullAvailVirtual |
The available extended portion of the virtual address space of the calling process, in bytes. This member is not used. |
Processes in Use |
This field is deprecated and reserved. |
Processes Available |
This field is deprecated and reserved. |
Example
#include <windows.h>
#include <iostream>
int main() {
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
if (GlobalMemoryStatusEx(&memInfo)) {
std::cout << "Percentage of memory in use: " << (int)memInfo.dwMemoryLoad << "%" << std::endl;
std::cout << "Total physical memory: " << memInfo.ullTotalPhys / (1024 * 1024) << " MB" << std::endl;
std::cout << "Available physical memory: " << memInfo.ullAvailPhys / (1024 * 1024) << " MB" << std::endl;
std::cout << "Total page file: " << memInfo.ullTotalPageFile / (1024 * 1024) << " MB" << std::endl;
std::cout << "Available page file: " << memInfo.ullAvailPageFile / (1024 * 1024) << " MB" << std::endl;
} else {
std::cerr << "Failed to get memory status. Error code: " << GetLastError() << std::endl;
}
return 0;
}
Note: For more detailed memory management functions, refer to the Memory Management section.