Wednesday, October 21, 2015

Memory usage per process in C++

Code:

#include <windows.h>
#include <stdio.h>
#include <psapi.h>

void PrintMemoryInfo( DWORD processID )
{
    HANDLE hProcess;
    PROCESS_MEMORY_COUNTERS pmc;
    printf( "\nProcess ID: %u\n", processID );
    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.QuotaPeakPagedPoolUsage );
        printf( "\tQuotaPagedPoolUsage: 0x%08X\n",
            pmc.QuotaPagedPoolUsage );
        printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n",
            pmc.QuotaPeakNonPagedPoolUsage );
        printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n",
            pmc.QuotaNonPagedPoolUsage );
        printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
        printf( "\tPeakPagefileUsage: 0x%08X\n",
            pmc.PeakPagefileUsage );
    }
    CloseHandle( hProcess );
}

int main( void )
{
    DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i;
    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
    {
        return 1;
    }
    cProcesses = cbNeeded / sizeof(DWORD);
    for ( i = 0; i < cProcesses; i++ )
    {
        PrintMemoryInfo( aProcesses[i] );
    }
    return 0;
}

Compile with g++:
g++ Source1.cpp -lpsapi -o s1

Save output to a text file:
g++ Source1.cpp -lpsapi -o s1

Output stored in text file:

Process ID: 0

Process ID: 4

Process ID: 420

Process ID: 600

Process ID: 656

Process ID: 664

Process ID: 704

Process ID: 756

Process ID: 764

Process ID: 772

Process ID: 872

Process ID: 952

Process ID: 304

Process ID: 520

Process ID: 568

Process ID: 592

Process ID: 1188

Process ID: 1284

Process ID: 1388

Process ID: 1540

Process ID: 1568

Process ID: 1600

Process ID: 1784

Process ID: 1856

Process ID: 1880

Process ID: 1912

Process ID: 1980

Process ID: 1140

Process ID: 1620

Process ID: 1812

Process ID: 1804

Process ID: 1988

Process ID: 2156

Process ID: 2260
    PageFaultCount: 0x0000B2DF
    PeakWorkingSetSize: 0x01335000
    WorkingSetSize: 0x0103A000
    QuotaPeakPagedPoolUsage: 0x0003B0C0
    QuotaPagedPoolUsage: 0x00039410
    QuotaPeakNonPagedPoolUsage: 0x0000A560
    QuotaNonPagedPoolUsage: 0x00008FD8
    PagefileUsage: 0x00EA2000
    PeakPagefileUsage: 0x00FA4000

Process ID: 2400
    PageFaultCount: 0x00000BFA
    PeakWorkingSetSize: 0x00AB5000
    WorkingSetSize: 0x004B9000
    QuotaPeakPagedPoolUsage: 0x000206A0
    QuotaPagedPoolUsage: 0x0001FA90
    QuotaPeakNonPagedPoolUsage: 0x000026A8
    QuotaNonPagedPoolUsage: 0x00001EB8
    PagefileUsage: 0x00211000
    PeakPagefileUsage: 0x006BC000

Process ID: 2428
    PageFaultCount: 0x0001BC29
    PeakWorkingSetSize: 0x05967000
    WorkingSetSize: 0x043F1000
    QuotaPeakPagedPoolUsage: 0x000BCF08
    QuotaPagedPoolUsage: 0x0008BD28
    QuotaPeakNonPagedPoolUsage: 0x0001A9F8
    QuotaNonPagedPoolUsage: 0x000162F8
    PagefileUsage: 0x03F4B000
    PeakPagefileUsage: 0x0501B000

Process ID: 2448

Process ID: 2744

Process ID: 2752

Process ID: 2864

Process ID: 2896

Process ID: 2956
    PageFaultCount: 0x00000FAD
    PeakWorkingSetSize: 0x009BE000
    WorkingSetSize: 0x00703000
    QuotaPeakPagedPoolUsage: 0x00029D08
    QuotaPagedPoolUsage: 0x00027B18
    QuotaPeakNonPagedPoolUsage: 0x00003A10
    QuotaNonPagedPoolUsage: 0x00002E70
    PagefileUsage: 0x00336000
    PeakPagefileUsage: 0x00376000

Process ID: 2980
    PageFaultCount: 0x0000033B
    PeakWorkingSetSize: 0x00316000
    WorkingSetSize: 0x00284000
    QuotaPeakPagedPoolUsage: 0x00014678
    QuotaPagedPoolUsage: 0x00014678
    QuotaPeakNonPagedPoolUsage: 0x00001E78
    QuotaNonPagedPoolUsage: 0x00001958
    PagefileUsage: 0x000F0000
    PeakPagefileUsage: 0x000F0000

Process ID: 3004
    PageFaultCount: 0x00000784
    PeakWorkingSetSize: 0x005B2000
    WorkingSetSize: 0x003F9000
    QuotaPeakPagedPoolUsage: 0x00021C68
    QuotaPagedPoolUsage: 0x000217D8
    QuotaPeakNonPagedPoolUsage: 0x00002500
    QuotaNonPagedPoolUsage: 0x00001EF0
    PagefileUsage: 0x00287000
    PeakPagefileUsage: 0x002A3000

Process ID: 3044
    PageFaultCount: 0x000004C9
    PeakWorkingSetSize: 0x004B7000
    WorkingSetSize: 0x0034D000
    QuotaPeakPagedPoolUsage: 0x00019470
    QuotaPagedPoolUsage: 0x000193F0
    QuotaPeakNonPagedPoolUsage: 0x00001D80
    QuotaNonPagedPoolUsage: 0x00001860
    PagefileUsage: 0x001DA000
    PeakPagefileUsage: 0x001F0000

Process ID: 3060
    PageFaultCount: 0x0000A58C
    PeakWorkingSetSize: 0x05FD1000
    WorkingSetSize: 0x04E68000
    QuotaPeakPagedPoolUsage: 0x000FE5D0
    QuotaPagedPoolUsage: 0x000FD6C0
    QuotaPeakNonPagedPoolUsage: 0x0001B91C
    QuotaNonPagedPoolUsage: 0x00012DE8
    PagefileUsage: 0x066FA000
    PeakPagefileUsage: 0x066FD000

Process ID: 2100
    PageFaultCount: 0x00001921
    PeakWorkingSetSize: 0x014ED000
    WorkingSetSize: 0x00BAB000
    QuotaPeakPagedPoolUsage: 0x0003B4A0
    QuotaPagedPoolUsage: 0x0003AC48
    QuotaPeakNonPagedPoolUsage: 0x000065A8
    QuotaNonPagedPoolUsage: 0x000055F8
    PagefileUsage: 0x00D3C000
    PeakPagefileUsage: 0x00E3F000

Process ID: 2324
    PageFaultCount: 0x0000063D
    PeakWorkingSetSize: 0x0057E000
    WorkingSetSize: 0x003EC000
    QuotaPeakPagedPoolUsage: 0x00020D98
    QuotaPagedPoolUsage: 0x00020908
    QuotaPeakNonPagedPoolUsage: 0x00002B88
    QuotaNonPagedPoolUsage: 0x00001FE8
    PagefileUsage: 0x00259000
    PeakPagefileUsage: 0x002FC000

Process ID: 2460
    PageFaultCount: 0x000004FE
    PeakWorkingSetSize: 0x004EA000
    WorkingSetSize: 0x003DC000
    QuotaPeakPagedPoolUsage: 0x0001F8F8
    QuotaPagedPoolUsage: 0x0001F468
    QuotaPeakNonPagedPoolUsage: 0x00001EE8
    QuotaNonPagedPoolUsage: 0x000018D8
    PagefileUsage: 0x001B4000
    PeakPagefileUsage: 0x001DE000

Process ID: 2568
    PageFaultCount: 0x000037A6
    PeakWorkingSetSize: 0x02CA0000
    WorkingSetSize: 0x0210B000
    QuotaPeakPagedPoolUsage: 0x0002BFE8
    QuotaPagedPoolUsage: 0x0002B5E8
    QuotaPeakNonPagedPoolUsage: 0x0000A060
    QuotaNonPagedPoolUsage: 0x00009998
    PagefileUsage: 0x02D52000
    PeakPagefileUsage: 0x02E68000

Process ID: 2312

Process ID: 2792
    PageFaultCount: 0x00000D97
    PeakWorkingSetSize: 0x008ED000
    WorkingSetSize: 0x00399000
    QuotaPeakPagedPoolUsage: 0x00029BD0
    QuotaPagedPoolUsage: 0x000293C0
    QuotaPeakNonPagedPoolUsage: 0x000047F0
    QuotaNonPagedPoolUsage: 0x00003E98
    PagefileUsage: 0x002F1000
    PeakPagefileUsage: 0x00313000

Process ID: 4088
    PageFaultCount: 0x000054DB
    PeakWorkingSetSize: 0x0124B000
    WorkingSetSize: 0x002F4000
    QuotaPeakPagedPoolUsage: 0x000377A0
    QuotaPagedPoolUsage: 0x00037190
    QuotaPeakNonPagedPoolUsage: 0x00009010
    QuotaNonPagedPoolUsage: 0x00008AF0
    PagefileUsage: 0x00B47000
    PeakPagefileUsage: 0x00BCE000

Process ID: 2720

Process ID: 3744
    PageFaultCount: 0x00023E63
    PeakWorkingSetSize: 0x0C0CE000
    WorkingSetSize: 0x06522000
    QuotaPeakPagedPoolUsage: 0x0010DBB0
    QuotaPagedPoolUsage: 0x001085E0
    QuotaPeakNonPagedPoolUsage: 0x0001ACF0
    QuotaNonPagedPoolUsage: 0x00014100
    PagefileUsage: 0x05EBD000
    PeakPagefileUsage: 0x27640000

Process ID: 3508

Process ID: 948

Process ID: 864

Process ID: 4188

Process ID: 4212

Process ID: 4300

Process ID: 4368

Process ID: 4444

Process ID: 4532

Process ID: 4552

Process ID: 4572

Process ID: 4864

Process ID: 3244

Process ID: 5156

Process ID: 5388

Process ID: 5536

Process ID: 5620

Process ID: 6584
    PageFaultCount: 0x000019FF
    PeakWorkingSetSize: 0x016AE000
    WorkingSetSize: 0x016A8000
    QuotaPeakPagedPoolUsage: 0x0009C118
    QuotaPagedPoolUsage: 0x0009B628
    QuotaPeakNonPagedPoolUsage: 0x00009F18
    QuotaNonPagedPoolUsage: 0x00009130
    PagefileUsage: 0x009A1000
    PeakPagefileUsage: 0x009E9000

Process ID: 6944

Process ID: 7024

Process ID: 7088

Process ID: 5692

Process ID: 7136

Process ID: 5572
    PageFaultCount: 0x00544F2F
    PeakWorkingSetSize: 0x19185000
    WorkingSetSize: 0x16724000
    QuotaPeakPagedPoolUsage: 0x00087008
    QuotaPagedPoolUsage: 0x0006EE68
    QuotaPeakNonPagedPoolUsage: 0x0005C99C
    QuotaNonPagedPoolUsage: 0x00019030
    PagefileUsage: 0x164C6000
    PeakPagefileUsage: 0x196DC000

Process ID: 5824
    PageFaultCount: 0x000030EC
    PeakWorkingSetSize: 0x029E2000
    WorkingSetSize: 0x02938000
    QuotaPeakPagedPoolUsage: 0x00076E20
    QuotaPagedPoolUsage: 0x000756F0
    QuotaPeakNonPagedPoolUsage: 0x00013C5C
    QuotaNonPagedPoolUsage: 0x00013644
    PagefileUsage: 0x01D75000
    PeakPagefileUsage: 0x02046000

Process ID: 5548
    PageFaultCount: 0x000027C5
    PeakWorkingSetSize: 0x01C92000
    WorkingSetSize: 0x01BA9000
    QuotaPeakPagedPoolUsage: 0x00042858
    QuotaPagedPoolUsage: 0x00041CB8
    QuotaPeakNonPagedPoolUsage: 0x00024A94
    QuotaNonPagedPoolUsage: 0x0000402C
    PagefileUsage: 0x0158B000
    PeakPagefileUsage: 0x01718000

Process ID: 7040
    PageFaultCount: 0x000003AC
    PeakWorkingSetSize: 0x003A7000
    WorkingSetSize: 0x003A0000
    QuotaPeakPagedPoolUsage: 0x000177A8
    QuotaPagedPoolUsage: 0x00017588
    QuotaPeakNonPagedPoolUsage: 0x000012C0
    QuotaNonPagedPoolUsage: 0x000012C0
    PagefileUsage: 0x00170000
    PeakPagefileUsage: 0x00170000

Process ID: 4000
    PageFaultCount: 0x0000088A
    PeakWorkingSetSize: 0x004B8000
    WorkingSetSize: 0x00467000
    QuotaPeakPagedPoolUsage: 0x00006850
    QuotaPagedPoolUsage: 0x00006830
    QuotaPeakNonPagedPoolUsage: 0x00001B40
    QuotaNonPagedPoolUsage: 0x00001688
    PagefileUsage: 0x00225000
    PeakPagefileUsage: 0x00298000

Process ID: 6556
    PageFaultCount: 0x000071F5
    PeakWorkingSetSize: 0x05C29000
    WorkingSetSize: 0x02EDB000
    QuotaPeakPagedPoolUsage: 0x0004D730
    QuotaPagedPoolUsage: 0x000436B0
    QuotaPeakNonPagedPoolUsage: 0x000080C8
    QuotaNonPagedPoolUsage: 0x00005F30
    PagefileUsage: 0x02399000
    PeakPagefileUsage: 0x050E1000

Process ID: 7056
    PageFaultCount: 0x00000FBC
    PeakWorkingSetSize: 0x00EE0000
    WorkingSetSize: 0x00EAB000
    QuotaPeakPagedPoolUsage: 0x000361C8
    QuotaPagedPoolUsage: 0x00036158
    QuotaPeakNonPagedPoolUsage: 0x00005048
    QuotaNonPagedPoolUsage: 0x00004B58
    PagefileUsage: 0x0096A000
    PeakPagefileUsage: 0x009AD000

Process ID: 2740
    PageFaultCount: 0x0000FB50
    PeakWorkingSetSize: 0x08D09000
    WorkingSetSize: 0x08630000
    QuotaPeakPagedPoolUsage: 0x000C5358
    QuotaPagedPoolUsage: 0x000C04C8
    QuotaPeakNonPagedPoolUsage: 0x000155A4
    QuotaNonPagedPoolUsage: 0x000135A4
    PagefileUsage: 0x05BCB000
    PeakPagefileUsage: 0x070D6000

Process ID: 4196
    PageFaultCount: 0x00004253
    PeakWorkingSetSize: 0x02477000
    WorkingSetSize: 0x022EE000
    QuotaPeakPagedPoolUsage: 0x00036DB0
    QuotaPagedPoolUsage: 0x00036620
    QuotaPeakNonPagedPoolUsage: 0x00017960
    QuotaNonPagedPoolUsage: 0x000172D0
    PagefileUsage: 0x03406000
    PeakPagefileUsage: 0x03538000

Process ID: 5896

Process ID: 2680
    PageFaultCount: 0x0000034D
    PeakWorkingSetSize: 0x00318000
    WorkingSetSize: 0x00317000
    QuotaPeakPagedPoolUsage: 0x00016968
    QuotaPagedPoolUsage: 0x000162F8
    QuotaPeakNonPagedPoolUsage: 0x00001478
    QuotaNonPagedPoolUsage: 0x00001248
    PagefileUsage: 0x00241000
    PeakPagefileUsage: 0x0024E000

Process ID: 3400
    PageFaultCount: 0x000005C9
    PeakWorkingSetSize: 0x00571000
    WorkingSetSize: 0x0056F000
    QuotaPeakPagedPoolUsage: 0x00020400
    QuotaPagedPoolUsage: 0x0001FC80
    QuotaPeakNonPagedPoolUsage: 0x00001B30
    QuotaNonPagedPoolUsage: 0x00001A40
    PagefileUsage: 0x001D0000
    PeakPagefileUsage: 0x001D0000

Process ID: 6856

Process ID: 192
    PageFaultCount: 0x00000835
    PeakWorkingSetSize: 0x006C0000
    WorkingSetSize: 0x006BD000
    QuotaPeakPagedPoolUsage: 0x0001DA00
    QuotaPagedPoolUsage: 0x0001D5C0
    QuotaPeakNonPagedPoolUsage: 0x00002290
    QuotaNonPagedPoolUsage: 0x00002120
    PagefileUsage: 0x002EF000
    PeakPagefileUsage: 0x002FA000

Process ID: 6212

Process ID: 5268
    PageFaultCount: 0x00000582
    PeakWorkingSetSize: 0x0055D000
    WorkingSetSize: 0x0055D000
    QuotaPeakPagedPoolUsage: 0x00014D28
    QuotaPagedPoolUsage: 0x00014CF8
    QuotaPeakNonPagedPoolUsage: 0x000021F8
    QuotaNonPagedPoolUsage: 0x000021F8
    PagefileUsage: 0x00242000
    PeakPagefileUsage: 0x00242000

Process ID: 6308
    PageFaultCount: 0x000003C4
    PeakWorkingSetSize: 0x003BE000
    WorkingSetSize: 0x003BE000
    QuotaPeakPagedPoolUsage: 0x00017DE0
    QuotaPagedPoolUsage: 0x00017588
    QuotaPeakNonPagedPoolUsage: 0x000012C0
    QuotaNonPagedPoolUsage: 0x000012C0
    PagefileUsage: 0x00173000
    PeakPagefileUsage: 0x00252000

Process ID: 1692
    PageFaultCount: 0x000001C0
    PeakWorkingSetSize: 0x001C1000
    WorkingSetSize: 0x001C1000
    QuotaPeakPagedPoolUsage: 0x00003DC0
    QuotaPagedPoolUsage: 0x00003DC0
    QuotaPeakNonPagedPoolUsage: 0x000009D8
    QuotaNonPagedPoolUsage: 0x000009D8
    PagefileUsage: 0x000CB000
    PeakPagefileUsage: 0x000CB000

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.