Can't obtain accurate information of available memory in the heap

半腔热情 提交于 2019-12-12 01:35:18

问题


all

I use the following function to obtain information about available memory and largest contiguous block in the heap.

int GetLargestContiguousMemory()
{
    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 = VirtualQueryEx(hproc, reinterpret_cast<lpvoid>(start), &mbi, sizeof(mbi));
        SIZE_T s = VirtualQueryEx(GetCurrentProcess(), (void*)(start), &mbi, sizeof(mbi));
        if (s != sizeof(mbi))
        {
            if (GetLastError() != ERROR_INVALID_PARAMETER)
                //return ReportError(GetLastError(), _T("Failed to VirtualQueryEx at %08x"), start);
                printf("Failed to VirtualQueryEx at %08x", start);
            else
                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;
}

int GetHeapAvailableMemory()
{
    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 = VirtualQueryEx(hproc, reinterpret_cast<lpvoid>(start), &mbi, sizeof(mbi));
        SIZE_T s = VirtualQueryEx(GetCurrentProcess(), (void*)(start), &mbi, sizeof(mbi));
        if (s != sizeof(mbi))
        {
            if (GetLastError() != ERROR_INVALID_PARAMETER)
                //return ReportError(GetLastError(), _T("Failed to VirtualQueryEx at %08x"), start);
                printf("Failed to VirtualQueryEx at %08x", start);
            else
                break;
        }
        if (mbi.State == MEM_FREE)
        {
            if (!recording)
                freestart = start;
            free += mbi.RegionSize;
        }
        start += mbi.RegionSize;
    }

    return free;
}

In my program, I use operator new to allocate memory in a loop, but the above function return same value for several iterations, and the returned value changes suddenly. It shows the size of the available memory in the heap is reduced by 8M bytes. I can't understand that. Is the function correct to return the size of available memory in the heap?

Thanks. Jogging

来源:https://stackoverflow.com/questions/10681015/cant-obtain-accurate-information-of-available-memory-in-the-heap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!