views:

249

answers:

4

As I know in win32 every program receives say 4GB of virtual memory. Memory manager is responsible for offloading chunks of memory from physical memory to disk.

Does it imply that malloc or any other memory allocation API will throw OUT_OF_MEMORY exception only when virtual limit is hit? I mean is it possible for malloc to fail even if program is far from its virtual size limit e.g. none of physical memory can be offloaded to disk. Assume disk have unlimited capacity and no specific limitation is set.

+10  A: 

Yes, it's possible. Remember that memory can be fragmented and that malloc won't be able to find a sufficiently large chunk to serve the size you requested. This can easily be way before you hit your 4 GiB limit.

Joey
Hi, memory fragmentation throws an OOM? It's a bit severe error for a not so uncommon situation...
ATorras
When the allocater can't find enough (continous) memory, what else should it do?
gnud
+6  A: 

The virtual memory limit on Win 32 is 2Gb. On Win 64, it's much bigger.

malloc doesn't throw an exception - it returns NULL. NULL return, or exception, the memory manager can fail well before the 2Gb limit is reached if

  • The paging file isn't big enough. If the paging file is limited either by policy, or by lack of room to expand: If memory allocations can't be met by page file availability then they will fail.

  • Fragmentation. The underlying memory manager allocates memory in 4Kb chunks. Its quite possible, through patterns of allocations and deallocations to end up with a small amount of allocated memory, but a fragmented virtual memory meaning that there is no contiguous area large enough to meet a particular request.

Chris Becke
On Win32 memory limit can be extended to 3.5 GB using a patch from Microsoft
Patrice Bernassola
Window's address space can be extended to 3Gb/process with a compile and boot time flag. 3.5Gb is typical usable memory limit for PCs that map the PCI bus onto the 4Gb address range of the CPU (without PAE)
Martin Beckett
Using the /3Gb switch is such a terribly bad idea I didn't think it worth mentioning. But was IS worth mentioning is, On windows 64, a 32 bit app CAN be given a full 4Gb address space (As long as its linked with the /largeaddressaware flag).
Chris Becke
A: 

how about allocating a few smaller areas, if a huge one isn't available?

What good would that do? If the program is asking for a large contiguous chunk of memory, it's presumably intending to use it as a large contiguous chunk of memory, and several smaller chunks would require extra computation built in to use like that. In any case, a program that runs low on memory is likely to run out shortly, so extra measures to reclaim memory are likely wasted.
David Thornley
What good it would do? It might be possible to adapt the algorithm to work with several smaller chunks of memory. So it might result in a working program. Seems pretty fair to me.
jalf
+1  A: 

For full chapter and verse on Windows virtual memory check out this post on Mark Russinovich's Blog (lots of other great stuff here too):

Pushing the Limits of Windows: Virtual Memory

If memory fragmentation is your problem and writing custom allocators isn't your thing you could consider enabling the low fragmentation heap:

Low Fragmentation Heap (Windows)

This is on by default these days mind you.

Paul Arnold