Search code examples
c++cwindowsmemorywindows-ce

How to explain drastic jump in memory allocation latency?


Intel Celeron 847 with 8GB of RAM. (C/C++ not .NET)

This is the only thread/function running, on Windows EC7.

It just loops through allocating 1MB of memory, 1000 times.

However, after about the 122nd iteration through the loop, the time it takes to allocate jumps from around 47 microseconds to 327 microseconds.

What are some possible reasons that would explain this to my boss?

while ( i < ITERATIONS )
{
QueryPerformanceCounter(&li);
start = double(li.QuadPart) / PCFreq;

// allocate 1MB
ptr = new char [1048576]; // 1 byte * 1048576 = 1 MB

QueryPerformanceCounter(&li);
stop = double(li.QuadPart) / PCFreq;

delayAlloc[i] = stop - start;

}

enter image description here

EDIT

Just to be sure, I executed the test 3 more times, each of the results were very similar to this:

enter image description here


Solution

  • Lets simplify your code and remove the timing stuff.

    while ( i < ITERATIONS )
    {
        // allocate 1MB
        ptr = new char [1048576]; // 1 byte * 1048576 = 1 MB
    }
    

    The system is allocating 1Mb to you and returning you the address. Then you are asking for another one, and storing the new address in the same variable you were holding the previous address.

    new returns a pointer, and a pointer is literally just the address of the allocation. It's not a special electronic key, not an object with scope, it's just the numeric value of the location in memory where your allocation starts.

    It is your responsibility to tell the allocator when you no-longer want the memory. There's no way for it to know what you do with that address data, whether you store it or not.

    while ( i < ITERATIONS )
    {
        // allocate 1MB
        ptr = new char [1048576]; // 1 byte * 1048576 = 1 MB
    
        delete [] ptr;
    }
    

    If you don't do this, the allocated memory will never be returned to the pool until your application terminates.

    That means your application's memory footprint is growing. Under normal Windows that might not be a problem but you are running under the embedded flavor of Windows where one Mb of memory is already a significant amount. The first 122Mb are probably pre-reserved to your app. But once you allocate all of the heap your app started with, the allocator has to get it's heap resized to be able to provide you extra allocations, and since this is embedded Windows, the resizing is done pessimistically.