Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...