realloc/HeapRealloc fails eventhough enough memory is available

好久不见. 提交于 2019-12-11 01:11:36

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/4385915/realloc-heaprealloc-fails-eventhough-enough-memory-is-available

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