Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,971 questions

51,913 answers

573 users

How to find the total memory used by a program in C Win32 API

1 Answer

0 votes
#include <Windows.h> 
#include <psapi.h>
#include <sstream>

// Use to convert bytes to KB
#define KB 1024

void PrintMemoryUsage()
{
    PROCESS_MEMORY_COUNTERS memory;
    HANDLE hProcess = GetCurrentProcess();

    /*
        BOOL GetProcessMemoryInfo(
            [in]  HANDLE                   Process,
            [out] PPROCESS_MEMORY_COUNTERS ppsmemCounters,
            [in]  DWORD                    cb
        );
    */

    std::ostringstream oss;

    if (GetProcessMemoryInfo(hProcess, &memory, sizeof(memory)))
    {
        
        oss << "Working Set Size: " << memory.WorkingSetSize / KB << " KB\n";
        oss << "Peak Working Set Size: " << memory.PeakWorkingSetSize / KB << " KB\n";
        oss << "Pagefile Usage: " << memory.PagefileUsage / KB << " KB\n";
        oss << "Peak Pagefile Usage: " << memory.PeakPagefileUsage / KB << " KB\n";

        std::string s = oss.str();
        MessageBoxA(0, s.c_str(), "", MB_OK); 
    }

    CloseHandle(hProcess);
}


int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {

    PrintMemoryUsage();

    return 0;
}



/*
run:

Working Set Size: 4728 KB
Peak Working Set Size: 4732 KB
Pagefile Usage: 1072 KB
Peak Pagefile Usage: 1072 KB

*/

 



answered Jan 4, 2025 by avibootz
...