c++windowswinapimemoryram

Get memory usage of this process C++ Win32


Please point me to the easiest way to find out how much RAM current process is used.

Platform: Windows 10 Language: С++ Library: Win API

  1. I know about HeapWalk/HeapSize but may be something more simple exists?
  2. Calling the external process (wmi or tasklist) is very slow.

Solution

  • With win API GetProcessMemoryInfo you can obtain a process memory information,for example:

    #include <windows.h>
    #include <psapi.h>
    
    #include <iostream>
    
    int main(int argc, const char** argv)
    {
        ::PROCESS_MEMORY_COUNTERS pmc;
        if ( ::GetProcessMemoryInfo( ::GetCurrentProcess(), &pmc, sizeof(pmc)) ) {
            std::cout << "Current process working set size " << (pmc.WorkingSetSize / 1024) << " KiB" << std::endl;
            std::cout << "Current process peak working set size " << (pmc.PeakWorkingSetSize / 1024) << " KiB" << std::endl;
        }
    
        return 0;
    }
    

    As well as you can profile your app without adding any additional code profiling with Visual Studio

    And with Process Explorer you can obtains any system process detailed information.