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, HI开发者_开发问答NSTANCE 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 dwLength
member of MEMORYSTATUSEX
before calling the function.
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
and you should be checking its return value before doing anything with the structure.
Are you sure you output the correct field? The first number you listed is about 55 GB, which wouldn't make much sense if you only had 4GB of ram.
as mentioned in msdn help: You can use the GlobalMemoryStatusEx function to determine how much memory your application can allocate without severely impacting other applications.
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.
The ullAvailPhys member of the MEMORYSTATUSEX structure at lpBuffer includes memory for all NUMA nodes.
so the return value can be diferent in diferent calls and it is the right answer.
精彩评论