Search code examples
c++memory-leaksvirtualvmmap

VMMap reports ~100 GB allocations


I have an application, I am afraid that there is a memory leak. Investigating it with VMMAP I see that most of allocation are reserved memory. It should not influence on performance....? Still the question is - what could be the reason for such amount of reserved memory (how can I investigate it?)Can it influence a performance at some stage? enter image description here

enter image description here


Solution

  • This Microsoft documentation page can explain what reserved memory means - https://learn.microsoft.com/en-us/windows/win32/memory/page-state:

    Reserved

    The page has been reserved for future use. The range of addresses cannot be used by other allocation functions. The page is not accessible and has no physical storage associated with it. It is available to be committed.

    A process can use the VirtualAlloc or VirtualAllocEx function to reserve pages of its address space and later to commit the reserved pages. It can use VirtualFree or VirtualFreeEx to decommit committed pages and return them to the reserved state.

    You can safely reserve large amount of memory, without any issues (in x64 build, which looks like the case for you)

    I successfully reserved 30000 gb of memory with next code

    #include "Windows.h"
    
    int main()
    {
        static const size_t allocationSize = 512 * 1024 * 1024;
        size_t allocated = 0;
        while (true)
        {
            void* data = ::VirtualAlloc(NULL, allocationSize, MEM_RESERVE, PAGE_READWRITE);
            if (data == nullptr)
            {
                HRESULT hr = GetLastError();
                ::DebugBreak();
            }
            allocated += allocationSize;
        }
    }