Invalid result from GlobalMemoryStatusEx () on Windows

I am trying to get shared system memory using GlobalMemoryStatusEx ():

    MEMORYSTATUSEX memory;
    GlobalMemoryStatusEx(&memory);
#define PRINT(v) {printf("%s ~%.3fGB\n", (#v), ((double)v)/(1024.*1024.*1024.));}
    PRINT(memory.ullAvailPhys);
    PRINT(memory.ullTotalPhys);
    PRINT(memory.ullTotalVirtual);
    PRINT(memory.ullAvailPageFile);
    PRINT(memory.ullTotalPageFile);
#undef PRINT
    fflush(stdout);

But the result is very simple and incomprehensible.

memory.ullAvailPhys ~1.002GB
memory.ullTotalPhys ~1.002GB
memory.ullTotalVirtual ~0.154GB
memory.ullAvailPageFile ~0.002GB
memory.ullTotalPageFile ~1.002GB

My total physical memory is 8 GB, but the result does not close it. All values ​​are much smaller.

In addition, the values ​​of "total" are saved at each execution. For example, another result:

memory.ullAvailPhys ~0.979GB
memory.ullTotalPhys ~0.979GB
memory.ullTotalVirtual ~0.154GB
memory.ullAvailPageFile ~0.002GB
memory.ullTotalPageFile ~0.979GB

What am I doing wrong?

+4
source share
1 answer

This is the part you do not see:

MEMORYSTATUSEX memory = { sizeof memory };

MSDN :

dwLengthThe size of the structure in bytes. You must set this item before calling GlobalMemoryStatusEx.

If you noted the value returned GlobalMemoryStatusEx, you can see the problem by indicating an error indication there.

+8
source

All Articles