Reputation: 2733
I am calling a c function which allocates around 900 mb large buffer. If I call this function at the very beginning of the wpf app, the allocation succeeds but if it is called after creating a few other objects, the function throws an out of memory exception. At any time, my wpf app does not use more than 200 mb of memory(excluding the 900 mb).
I have also tried compacting the LOH using
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
GC.Collect();
to make sure enough contiguous memory is available for 900 mb allocation but it didn't help as well. Its a 32 bit wpf app. I have also tried using the /3gb switch and it didn't help as well.
What could possibly be the problem?
Upvotes: 1
Views: 108
Reputation: 111860
That the memory becomes fragmented :-) You can't control it. With an address space of 3 gb, you only need 4 pages of memory (4kb * 4 = 16kb) to make a request of 900mb impossible.
Solutions can be:
going 64 bits
not allocating a single block of 900 mb (if it is a memory mapped file, the file could be mapped not completely but though a sliding window)
at the beginning of the app, preallocating the 900mb and giving the C method this block of memory that has already been allocated (note that I don't think there is a strong guarantee that 900mb of address space are accessible at the start of an app)
similar to the previous: at the beginning of the program, with VirtualAlloc
you allocate only the address space for 900 mb of memory. You fill this address space with "real" memory when needed, and you free the memory (without freeing the address space) when you don't need it. In this way the address space won't become fragmented.
Upvotes: 3