saifnawaz
asked on
get memory usage in KB?
Hi ,
i am trying to get the memory usage of each running process in KB(similar to the task manager in windows NT)
for this i found the following code in previous post:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/collecting_memory_usage_information_for_a_process.asp
But the output i get is in the following format:
Process ID: 600
PageFaultCount: 0x000000F7
PeakWorkingSetSize: 0x000F9000
WorkingSetSize: 0x000F9000
QuotaPeakPagedPoolUsage: 0x00002BFC
QuotaPagedPoolUsage: 0x00002AC0
QuotaPeakNonPagedPoolUsage : 0x000006A4
QuotaNonPagedPoolUsage: 0x00000684
PagefileUsage: 0x0003B000
PeakPagefileUsage: 0x0003B000
How can i get memory usage in KB format?
The code is given below:
#include "stdafx.h"
#define _WIN32_WINNT 0x0400//0x0501
#include <windows.h>
#include <stdio.h>
#include "Psapi.h"
void PrintMemoryInfo( DWORD processID )
{
HANDLE hProcess;
PROCESS_MEMORY_COUNTERS pmc;
// Print the process identifier.
printf( "\nProcess ID: %u\n", processID );
// Print information about the memory usage of the process.
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
if (NULL == hProcess)
return;
if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
{
printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
printf( "\tPeakWorkingSetSize: 0x%08X\n",
pmc.PeakWorkingSetSize );
printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
printf( "\tQuotaPeakPagedPoolUsage : 0x%08X\n",
pmc.QuotaPeakPagedPoolUsag e );
printf( "\tQuotaPagedPoolUsage: 0x%08X\n",
pmc.QuotaPagedPoolUsage );
printf( "\tQuotaPeakNonPagedPoolUs age: 0x%08X\n",
pmc.QuotaPeakNonPagedPoolU sage );
printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n",
pmc.QuotaNonPagedPoolUsage );
printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
printf( "\tPeakPagefileUsage: 0x%08X\n",
pmc.PeakPagefileUsage );
}
CloseHandle( hProcess );
}
int _tmain(int argc, _TCHAR* argv[])
{
// Get the list of process identifiers.
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return 0;
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the memory usage for each process
for ( i = 0; i < cProcesses; i++ )
PrintMemoryInfo( aProcesses[i] );
return 0;
Thanks.
i am trying to get the memory usage of each running process in KB(similar to the task manager in windows NT)
for this i found the following code in previous post:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/collecting_memory_usage_information_for_a_process.asp
But the output i get is in the following format:
Process ID: 600
PageFaultCount: 0x000000F7
PeakWorkingSetSize: 0x000F9000
WorkingSetSize: 0x000F9000
QuotaPeakPagedPoolUsage: 0x00002BFC
QuotaPagedPoolUsage: 0x00002AC0
QuotaPeakNonPagedPoolUsage
QuotaNonPagedPoolUsage: 0x00000684
PagefileUsage: 0x0003B000
PeakPagefileUsage: 0x0003B000
How can i get memory usage in KB format?
The code is given below:
#include "stdafx.h"
#define _WIN32_WINNT 0x0400//0x0501
#include <windows.h>
#include <stdio.h>
#include "Psapi.h"
void PrintMemoryInfo( DWORD processID )
{
HANDLE hProcess;
PROCESS_MEMORY_COUNTERS pmc;
// Print the process identifier.
printf( "\nProcess ID: %u\n", processID );
// Print information about the memory usage of the process.
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
if (NULL == hProcess)
return;
if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
{
printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
printf( "\tPeakWorkingSetSize: 0x%08X\n",
pmc.PeakWorkingSetSize );
printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
printf( "\tQuotaPeakPagedPoolUsage
pmc.QuotaPeakPagedPoolUsag
printf( "\tQuotaPagedPoolUsage: 0x%08X\n",
pmc.QuotaPagedPoolUsage );
printf( "\tQuotaPeakNonPagedPoolUs
pmc.QuotaPeakNonPagedPoolU
printf( "\tQuotaNonPagedPoolUsage:
pmc.QuotaNonPagedPoolUsage
printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
printf( "\tPeakPagefileUsage: 0x%08X\n",
pmc.PeakPagefileUsage );
}
CloseHandle( hProcess );
}
int _tmain(int argc, _TCHAR* argv[])
{
// Get the list of process identifiers.
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return 0;
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the memory usage for each process
for ( i = 0; i < cProcesses; i++ )
PrintMemoryInfo( aProcesses[i] );
return 0;
Thanks.
ASKER
hi,
I did replace it with %d and the result is:
Process ID: 8
PageFaultCount: 2529
PeakWorkingSetSize: 651264
WorkingSetSize: 217088
QuotaPeakPagedPoolUsage: 0
QuotaPagedPoolUsage: 0
QuotaPeakNonPagedPoolUsage
QuotaNonPagedPoolUsage: 0
PagefileUsage: 24576
PeakPagefileUsage: 172032
how do i get the mem usage of the process form this which is equal to the current mem usage of the process in task manager.Can i calculate the memusage from the above values?
I did replace it with %d and the result is:
Process ID: 8
PageFaultCount: 2529
PeakWorkingSetSize: 651264
WorkingSetSize: 217088
QuotaPeakPagedPoolUsage: 0
QuotaPagedPoolUsage: 0
QuotaPeakNonPagedPoolUsage
QuotaNonPagedPoolUsage: 0
PagefileUsage: 24576
PeakPagefileUsage: 172032
how do i get the mem usage of the process form this which is equal to the current mem usage of the process in task manager.Can i calculate the memusage from the above values?
For that purpose, I wouldn't use the above - the following is a bit easier:
MEMORYSTATUS ms;
GlobalMemoryStatus ( &ms);
DWORD dwMemUsage = ms.dwTotalVirtual - ms.dwAvailVirtual;
From teh docs:
dwTotalVirtual
Indicates the total number of bytes that can be described in the user mode portion of the virtual address space of the calling process.
MEMORYSTATUS ms;
GlobalMemoryStatus ( &ms);
DWORD dwMemUsage = ms.dwTotalVirtual - ms.dwAvailVirtual;
From teh docs:
dwTotalVirtual
Indicates the total number of bytes that can be described in the user mode portion of the virtual address space of the calling process.
ASKER
But will this give me the memory used by all the running processes?
I need to get the memory usage of all the running processes from my code
I need to get the memory usage of all the running processes from my code
ASKER
By all the running processes, i mean the memory used by each process running
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hmm i seem to have got it
the values given are bytes and the memory used is basically the workings set size
i got the values in kb by (workings set size/1024)
the values given are bytes and the memory used is basically the workings set size
i got the values in kb by (workings set size/1024)
You need to replace the '0x%08X' format specifier with '%d' - the values are already given in KB.