Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ delete does not free all memory (Windows)

I need help understanding problems with my memory allocation and deallocation on Windows. I'm using VS11 compiler (VS2012 IDE) with latest update at the moment (Update 3 RC).

Problem is: I'm allocating dynamically some memory for a 2-dimensional array and immediately deallocating it. Still, before memory allocation, my process memory usage is 0,3 MB before allocation, on allocation 259,6 MB (expected since 32768 arrays of 64 bit ints (8bytes) are allocated), 4106,8 MB during allocation, but after deallocation memory does not drop to expected 0,3 MB, but is stuck at 12,7 MB. Since I'm deallocating all heap memory I've taken, I've expected memory to be back to 0,3 MB.

This is the code in C++ I'm using:

#include <iostream>
#define SIZE 32768
int main( int argc, char* argv[] ) {
std::getchar();

int ** p_p_dynamic2d = new int*[SIZE];

for(int i=0; i<SIZE; i++){
    p_p_dynamic2d[i] = new int[SIZE];
}   
std::getchar();

for(int i=0; i<SIZE; i++){
    for(int j=0; j<SIZE; j++){
        p_p_dynamic2d[i][j] = j+i;
    }
}

std::getchar();

for(int i=0; i<SIZE; i++) {
    delete [] p_p_dynamic2d[i];
}
delete [] p_p_dynamic2d;

std::getchar();
return 0;
}
like image 934
user2467906 Avatar asked Jun 09 '13 09:06

user2467906


People also ask

What happens if I don't free memory in C?

If free() is not used in a program the memory allocated using malloc() will be de-allocated after completion of the execution of the program (included program execution time is relatively small and the program ends normally).

What happens if you don't delete dynamically allocated memory?

If you don't free/delete dynamically allocated arrays they don't get freed/deleted. That's all. Use vector<WebPage> it deletes itself automatically and has a push_back function to add new elements to the existing array.

What happens if you don't deallocate memory?

If you lose all pointers to a chunk of memory without deallocating that memory then you have a memory leak. Your program will continue to own that memory, but has no way of ever using it again. A very small memory leak is not a problem.


Video Answer


1 Answers

I'm sure this is a duplicate, but I'll answer it anyway:

If you are viewing Task Manager size, it will give you the size of the process. If there is no "pressure" (your system has plenty of memory available, and no process is being starved), it makes no sense to reduce a process' virtual memory usage - it's not unusual for a process to grow, shrink, grow, shrink in a cyclical pattern as it allocates when it processes data and then releases the data used in one processing cycle, allocating memory for the next cycle, then freeing it again. If the OS were to "regain" those pages of memory, only to need to give them back to your process again, that would be a waste of processing power (assigning and unassigning pages to a particular process isn't entirely trivial, especially if you can't know for sure who those pages belonged to in the first place, since they need to be "cleaned" [filled with zero or some other constant to ensure the "new owner" can't use the memory for "fishing for old data", such as finding my password stored in the memory]).

Even if the pages are still remaining in the ownership of this process, but not being used, the actual RAM can be used by another process. So it's not a big deal if the pages haven't been released for some time.

Further, in debug mode, the C++ runtime will store "this memory has been deleted" in all memory that goes through delete. This is to help identify "use after free". So, if your application is running in debug mode, then don't expect any freed memory to be released EVER. It will get reused tho'. So if you run your code three times over, it won't grow to three times the size.

like image 186
Mats Petersson Avatar answered Sep 28 '22 03:09

Mats Petersson