Click to See Complete Forum and Search --> : Get available memory


C.Schlue
January 22nd, 2004, 05:35 AM
Does anyone of you know an API call, to find out, how much RAM is currently available? (WinXP OS)

Thanx,
Chris

Sam Hobbs
January 22nd, 2004, 11:28 AM
As far as I know, the answer to that question is not as easy as you like it to be. Do you mean physical or virtual memory? I know that "RAM" implies physical memory, but it would help to be sure.

I assume you want to know if it is safe for your program to allocate more memory. If so, then the answer might not be as simple as the amount of available memory. Others can help better, but they might need to know the purpose of determining available memory.

Andreas Masur
January 22nd, 2004, 11:37 AM
To get information about the memory usage of a process you can use the function 'GetProcessMemoryInfo()'...

#include <iostream>
#include <iomanip>
#include <psapi.h>

// Add 'psapi.lib' to your linker options

int main()
{
// Open current process
HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, ::GetCurrentProcessId());
if(hProcess)
{
PROCESS_MEMORY_COUNTERS ProcessMemoryCounters;

memset(&ProcessMemoryCounters, 0, sizeof(ProcessMemoryCounters));

// Set size of structure
ProcessMemoryCounters.cb = sizeof(ProcessMemoryCounters);

// Get memory usage
if(::GetProcessMemoryInfo(hProcess,
&ProcessMemoryCounters,
sizeof(ProcessMemoryCounters)) == TRUE)
{
std::cout << std::setfill('0') << std::hex
<< "PageFaultCount: 0x" << std::setw(8)
<< ProcessMemoryCounters.PageFaultCount << std::endl
<< "PeakWorkingSetSize: 0x" << std::setw(8)
<< ProcessMemoryCounters.PeakWorkingSetSize << std::endl
<< "WorkingSetSize: 0x" << std::setw(8)
<< ProcessMemoryCounters.WorkingSetSize << std::endl
<< "QuotaPeakPagedPoolUsage: 0x" << std::setw(8)
<< ProcessMemoryCounters.QuotaPeakPagedPoolUsage << std::endl
<< "QuotaPagedPoolUsage: 0x" << std::setw(8)
<< ProcessMemoryCounters.QuotaPagedPoolUsage << std::endl
<< "QuotaPeakNonPagedPoolUsage: 0x" << std::setw(8)
<< ProcessMemoryCounters.QuotaPeakNonPagedPoolUsage << std::endl
<< "QuotaNonPagedPoolUsage: 0x" << std::setw(8)
<< ProcessMemoryCounters.QuotaNonPagedPoolUsage << std::endl
<< "PagefileUsage: 0x" << std::setw(8)
<< ProcessMemoryCounters.PagefileUsage << std::endl
<< "PeakPagefileUsage: 0x" << std::setw(8)
<< ProcessMemoryCounters.PeakPagefileUsage <<std::endl;
}
else
std::cout << "Could not get memory usage (Error: "
<< ::GetLastError() << ")" << std::endl;

// Close process
::CloseHandle(hProcess);
}
else
std::cout << "Could not open process (Error "
<< ::GetLastError() << ")" << std::endl;

return 0;
}

Andreas Masur
January 22nd, 2004, 11:38 AM
In case you simply want the global memory...

#include <iostream>
#include <windows.h>
#include <memory.h>
#include <conio.h>

int main()
{
MEMORYSTATUS MemStat;

// Zero structure
memset(&MemStat, 0, sizeof(MemStat));

// Get RAM snapshot
::GlobalMemoryStatus(&MemStat);

// Print results
std::cout << "Length of structure: " << MemStat.dwLength
<< std::endl
<< "Memory usage: " << MemStat.dwMemoryLoad
<< " %" << std::endl
<< "Physical memory: " << MemStat.dwTotalPhys / 1024
<< " KB" << std::endl
<< "Free physical memory: " << MemStat.dwAvailPhys / 1024
<< " KB" << std::endl
<< "Paging file: " << MemStat.dwTotalPageFile / 1024
<< " KB" << std::endl
<< "Free paging file: " << MemStat.dwAvailPageFile / 1024
<< " KB" << std::endl
<< "Virtual memory: " << MemStat.dwTotalVirtual / 1024
<< " KB" << std::endl
<< "Free virtual memory: " << MemStat.dwAvailVirtual / 1024
<< " KB" << std::endl;

// Wait for keystroke
_getch();

return 0;
}

C.Schlue
January 22nd, 2004, 11:45 AM
Global memory... right.

Thats what I need. Looks simple - how come, I didn't find this solution??
I was trying araound with pdh.dll.

Thanks Andreas!!!