0

I want to calculate total physical memory usage of system. This is my code:

float totalmem(){

    DWORDLONG totalVirtualMem;
    MEMORYSTATUSEX memInfo;
    float virtualMemUsed;

    memInfo.dwLength = sizeof(MEMORYSTATUSEX);
    GlobalMemoryStatusEx(&memInfo);
    totalVirtualMem = memInfo.ullTotalPageFile;
    virtualMemUsed = (double) (memInfo.ullTotalPageFile - memInfo.ullAvailPageFile) / memInfo.ullTotalPageFile * 100;
    return virtualMemUsed;
}

But the answer is different from that windows task manager or process explorer shows. What's wrong with my code?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Mehrdad
  • 708
  • 1
  • 17
  • 38
  • 2
    Have a look at this **[How to determine CPU and memory consumption from inside a process?](http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process)** – Siva Charan Jun 22 '12 at 21:59
  • @hacker2012 I deleted my answer, 4 downvotes and no one explains why they did it..... – Cacho Santa Jun 22 '12 at 22:10

1 Answers1

0

The correct code:

float totalmem(){

DWORDLONG totalVirtualMem;
MEMORYSTATUSEX memInfo;
float virtualMemUsed;

memInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memInfo);
//totalVirtualMem = memInfo.ullTotalPageFile;
virtualMemUsed = (double) (memInfo.ullTotalPhys - memInfo.ullAvailPhys) / memInfo.ullTotalPhys * 100;
return virtualMemUsed;
}
Mehrdad
  • 708
  • 1
  • 17
  • 38