The description for GlobalMemoryStatusEx() in MSDN says:
The information returned by the GlobalMemoryStatusEx function is volatile. There is no guarantee that two sequential calls to this function will return the same information.
One piece of information returned by this function is “Total physical memory in bytes”. (This is different from the amount of FREE physical memory available, there is another member of the MEMORYSTATUSEX structure for that.)
How is it possible for the total physical memory to change every time the program is run? I output the values to the a text file and got these results:
55872198592
55837267904
8589934605
55835301824
55848146880
55849064384
55849129920
55836743616
8589934605
8589934605
8589934605
8589934605
55835105216
I have 4GB of system RAM. Do I need to call another API function if I am on a 64 bit OS?
Here is the code:
#include <Windows.h>
#include <string>
#include <sstream>
#include "Game.h"
#include <fstream>
void Game::CheckMemory(DWORDLONG& a)
{
MEMORYSTATUSEX status;
GlobalMemoryStatusEx(&status);
a = status.ullTotalPhys;
std::stringstream ss;
ss << "Total Physical Memory: " << status.ullTotalPhys << "bytes." << std::endl;
MessageBoxA(NULL, ss.str().c_str(), "Mem Summary", 0);
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
DWORDLONG a;
Game g;
g.CheckMemory(a);
std::fstream fs("test.txt", std::fstream::in | std::fstream::out | std::fstream::app);
fs << a << std::endl;
fs.close();
return 0;
}
You must initialize the
dwLengthmember ofMEMORYSTATUSEXbefore calling the function.and you should be checking its return value before doing anything with the structure.