Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.?

like image 226
Paul Baumer Avatar asked Dec 06 '22 07:12

Paul Baumer


2 Answers

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.

like image 53
schnaader Avatar answered Dec 07 '22 21:12

schnaader


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.

like image 27
Diomidis Spinellis Avatar answered Dec 07 '22 20:12

Diomidis Spinellis