Windows Process Status Helpers API 5

 

 

 

 

 

Collecting Memory Usage Information for a Process Program Example

 

To determine the efficiency of your application, you may want to examine its memory usage. The following sample code uses the GetProcessMemoryInfo() function to obtain information about the memory usage of a process.

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.

 

Collecting Memory Usage Information for a Process Program Example: Creating new Win32 C++ console application project in Visual C++ .NET

 

Then, add the source file and give it a suitable name.

 

Collecting Memory Usage Information for a Process Program Example: Adding new C++ source file for C++ source code to the existing C++ project

 

Next, add the following source code.

 

#include <windows.h>

#include <stdio.h>

#include <wchar.h>

#include <psapi.h>

 

// Link to Psapi.lib

#pragma comment(lib, "Psapi.lib")

 

void PrintMemoryInfo(DWORD processID)

{

    HANDLE hProcess;

    PROCESS_MEMORY_COUNTERS pmc;

 

    // Print the process identifier.

    wprintf(L"\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 (hProcess == NULL)

      {

        wprintf(L"OpenProcess() failed! Error %d\n", GetLastError());

        return;

      }

 

    if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc)))

    {

        wprintf(L"\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount);

        wprintf(L"\tPeakWorkingSetSize: 0x%08X\n",  pmc.PeakWorkingSetSize);

        wprintf(L"\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize);

        wprintf(L"\tQuotaPeakPagedPoolUsage: 0x%08X\n",  pmc.QuotaPeakPagedPoolUsage);

        wprintf(L"\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage);

        wprintf(L"\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage);

        wprintf(L"\tQuotaNonPagedPoolUsage: 0x%08X\n",  pmc.QuotaNonPagedPoolUsage);

        wprintf(L"\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage);

        wprintf(L"\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage);

    }

 

    CloseHandle(hProcess);

}

 

int wmain(int argc, WCHAR **argv)

{

    // Get the list of process identifiers.

    DWORD aProcesses[1024], cbNeeded, cProcesses;

    unsigned int i;

 

    if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))

        return 1;

 

    // Calculate how many process identifiers were returned.

    cProcesses = cbNeeded / sizeof(DWORD);

 

    // Print the memory usage for each process

    wprintf(L"Press any key for more...\n");

    for (i = 0; i < cProcesses; i++)

      {

        PrintMemoryInfo(aProcesses[i]);

            _getwch();

      }

 

      return 0;

}

 

Build and run the project. The following screenshot is a sample output.

 

 

Collecting Memory Usage Information for a Process Program Example: A sample console program output in action

 

The main function obtains a list of processes by using the EnumProcesses() function. For each process, main calls the PrintMemoryInfo() function, passing the process identifier. PrintMemoryInfo() in turn calls the OpenProcess() function to obtain the process handle. If OpenProcess() fails, the output shows only the process identifier. For example, OpenProcess() fails for the Idle and CSRSS processes because their access restrictions prevent user-level code from opening them. Finally, PrintMemoryInfo() calls the GetProcessMemoryInfo() function to obtain the memory usage information.

 

 

 

 

< Win32 Process Status Help APIs 4 | Process Status Help APIs Index | Win32 Programming | Win32 Process Status Help APIs 6 >