Find the maximum possible memory allocation

In 32-bit mode, the application has access to 2 GB of virtual address space. How do you know the maximum size of memory that you can allocate for this virtual address space without malloc or a new crash?

For example, let's say you want to tackle a whole 2 GB of virtual address space, but you just allocated 2 MB of data in the middle of the 2 GB address space. Is there a Windows API call where you can find out the maximum parallel address space that you can allocate? So when you call malloc or new, the call is not interrupted?

Thank you for your time.

+3
source share
3 answers

: http://www.voyce.com/index.php/2009/08/21/largest-free-block-of-address-space/

DWORD FindLargestSizeOfMemory()
{

    MEMORY_BASIC_INFORMATION mbi;
    DWORD start = 0;
    bool recording = false;
    DWORD freestart = 0, largestFreestart = 0;
    __int64 free = 0, largestFree = 0;

    while (true)
    {
        SIZE_T s = VirtualQuery((LPCVOID)start, &mbi, sizeof(mbi));
        if (s != sizeof(mbi)) break;

    if (mbi.State == MEM_FREE)
        {
            if (!recording) freestart = start;

            free += mbi.RegionSize;
            recording = true;
        }
        else
        {
            if (recording)
            {
                if (free > largestFree)
                {
                    largestFree = free;
                    largestFreestart = freestart;
                }
            }
            free = 0;
            recording = false;
        }
        start += mbi.RegionSize;
    }

  return largestFree;
}
+5

, , . , , (,/3GB ) .

, " malloc ", malloc? var = malloc (...) , var null.

, , , . , , , , . , , Windows , , .

+1

VirtualQuery , MEM_FREE.

0

Source: https://habr.com/ru/post/1785171/


All Articles