-1

In my application i was trying to figure out memory utilization for a particular process in windows machine using below mentioned api.

GetProcessMemoryInfo(hProcess, &info, sizeof(info)); when i checked the value of info.WorkingSetSize it was exactly 14757395258967641292.

So i want to clear whether the returned value is in bytes(for naked eye this cannot be in bytes format)? if not how to convert it into bytes or kilobytes.

void PrintProcessNameAndID( DWORD processID )

{

TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
PROCESS_MEMORY_COUNTERS info, info1, info2;
SIZE_T MemoryUsage;
SIZE_T one,two,three, four;
// Get a handle to the process.

HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                               PROCESS_VM_READ,
                               FALSE, processID );

// Get the process name.

if (NULL != hProcess )
{
    HMODULE hMod;
    DWORD cbNeeded;

    if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
         &cbNeeded) )
    {
        GetModuleBaseName( hProcess, hMod, szProcessName, 
                           sizeof(szProcessName)/sizeof(TCHAR) );
    }
}

// Print the process name and identifier.
//_tprintf( TEXT("%s  (PID: %u)"), szProcessName, processID );
GetProcessMemoryInfo(hProcess, &info, sizeof(info));
MemoryUsage = (info.WorkingSetSize);

}

Darshan
  • 121
  • 1
  • 11
  • This value seems very large. Please, show your code, see also this post http://stackoverflow.com/questions/19094626/process-memory-counters-is-giving-negative-values-about-memory-usage – terence hill Dec 23 '15 at 11:17
  • hi i took the complete code from here https://msdn.microsoft.com/en-us/library/windows/desktop/ms682050(v=vs.85).aspx – Darshan Dec 23 '15 at 11:23
  • you mean this printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );? – terence hill Dec 23 '15 at 11:32
  • No i didn't use printf statement actually i was debugging so i got to know the value SIZE_T MemoryUsage = (info.WorkingSetSize); here i was getting memoryusage value as above mentioned. – Darshan Dec 23 '15 at 11:37
  • The field is in the struct (Windows Dev Center) PROCESS_MEMORY_COUNTERS and it returns the size in bytes as stated in the API page: WorkingSetSize: The current working set size, in bytes. The problem lies somewhere else. The number should be in bytes. We can fully answer to your question only if you post the code that produce the above number. – terence hill Dec 23 '15 at 11:46
  • i added the code snippet above – Darshan Dec 23 '15 at 12:00
  • *Never* ignore the return value of a winapi function. At a minimum you must assert() it. Only way to find out that your code is borken, like it is here. Initializing info.cb is not optional. – Hans Passant Dec 23 '15 at 12:03
  • i don't think code is broken when i run this code i can see until PID 2088 values are same(big values) after that i am getting correct values for other processes(resembles with taskmanager values ) – Darshan Dec 23 '15 at 12:15
  • I think what @HansPassant is saying is that GetProcessMemoryInfo function returns an error which you don't check – o_weisman Dec 23 '15 at 12:35
  • 1
    14757395258967641292 is not an accidental value, it is 0xcccccccccccccccc in hex. The value used to initialize memory in the Debug build. So you are looking at uninitialized data, GPMI failed. Initializing info.cb really, *really* is not optional. – Hans Passant Dec 23 '15 at 12:50
  • Sorry i got your point when i tried in release mode it started working i will make necessary changes – Darshan Dec 23 '15 at 12:58

1 Answers1

1

Some windows processes require a lesser value than PROCESS_QUERY_INFORMATION (e.g. PROCESS_QUERY_LIMITED_INFORMATION).

The result is that OpenProcess may return NULL.

This is handled in your test, then however, you always call GetProcessMemoryInfo.

The result will be a failed call. With un-initialized memory for info resulting in some random value (0xccccccccccccd000).

mksteve
  • 12,614
  • 3
  • 28
  • 50