Paul Baumer
Paul Baumer

Reputation: 321

Deallocation doesn't free memory in Windows/C++ Application

My Windows/C++ application allocates ~1Gb of data in memory with the operator new and processes this data. After processing the data is deleted.

I noticed that if I run the processing again without exiting the application, the second call to the operatornew to allocate ~1Gb of data fails.

I would expect Windows to deliver the memory back. Could this be managed in a better way with some other Win32 calls etc.?

Upvotes: 4

Views: 2027

Answers (5)

Stephen Kellett
Stephen Kellett

Reputation: 3236

This problem is almost certainly memory fragmentation. On 32 bit Windows the largest contiguous region you can allocate is about 1.1GB (because various DLLs in your EXE preventa larger contiguous range being available). If after deallocating a memory allocation (or a DLL load, or a memory mapped file) ends up in the middle of your previous 1GB region then there will no longer be a 1GB region available for your next call to new to allocate 1GB. Thus it will fail.

You can visualize this process using VM Validator.

Upvotes: 1

Matthew Xavier
Matthew Xavier

Reputation: 2128

Since you are using very large memory blocks, you should consider using VirtualAlloc() and VirtualFree(), as they allow you to allocate and free pages directly, without the overhead (in memory and time) of interacting with a heap manager.

Since you are using C++, it is worth noting that you can construct C++ objects in the memory you allocate this way by using placement new.

Upvotes: 1

Diomidis Spinellis
Diomidis Spinellis

Reputation: 19325

In most runtime environments memory allocated to an application from the operating system remains in the application, and is seldom returned back to the operating system. Freeing a memory block allows you to reuse the block from within the application, but does not free it to the operating system to make it available to other applications.

Microsoft's C runtime library tries to return memory back to the operating system by having _heapmin_region call _heap_free_region or _free_partial_region which call VirtualFree to release data to the operating system. However, if whole pages in the corresponding region are not empty, then they will not be freed. A common cause of this is the bookkeeping information and storage caching of C++ containers.

Upvotes: 5

Adam Wright
Adam Wright

Reputation: 49376

This could be due to memory fragmentation (in reality, address space fragmentation), where various factors have contributed to your program address space not having a 1gb contiguous hole available. In reality, I suspect a bug in your memory management (sorry) - have you run your code through leak detection?

Upvotes: 3

schnaader
schnaader

Reputation: 49719

I don't think this is a Windows problem. Check if you used delete or delete[] correctly. Perhaps it would help if you post the code that is allocating/freeing the memory.

Upvotes: 6

Related Questions