Search code examples
c++winapidynamic-memory-allocation

realloc()/HeapRealloc() fails even though enough memory is available


My machine has 8GB of RAM and is running Windows Server 2008. malloc()/realloc() fails to allocate more memory when my application has 1.5~1.7GB already allocated. I tried switching to HeapAlloc()/HeapRealloc() instead and the same situation happens.

Is there something I am missing here? What could be causing my application to be unable to allocate more memory when there is clearly available RAM?


Solution

  • What could be causing my application to be unable to allocate more memory when there is clearly available RAM?

    Heap fragmentation. Allocation doesn't simply ask for memory. It asks for a contiguous piece of memory.

    Of course, given the sparse data you provided (how much are you trying to re-alloc when that happens? is this a 32bit or a 64bit app?), there could be other problems as well.