My machine h开发者_开发百科as 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?
It just doesn't matter how much RAM you are having in your machine. Each 32 bit process on Windows gets a 4GB address space out of which 2GB is available in user-address space. So the memory for your program (including its code, dlls loaded, stack etc) will be allocated from this space only. Since you are nearing the limits of the virtual address space the memory allocating is failing.
By default on Windows 32-bit OS you can use 2DB for one process. If you use MSVC compiler you have to set LARGEADDRESSAWARE option.
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.
精彩评论