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

I need help understanding problems with memory allocation and freeing memory in Windows. I am using the VS11 compiler (VS2012 IDE) with the latest update at the moment (Update 3 RC).

The problem is this: I dynamically allocate some memory for a two-dimensional array and immediately free it. However, before allocating memory, my process memory usage is 0.3 MB before allocation, when allocating 259.6 MB (it is expected that 32768 arrays of 64-bit ints (8 bytes) are allocated), 4106.8 MB when allocating, but after the release memory does not drop to the expected 0.3 MB, but is stuck at 12.7 MB . Since I free up all the heap memory that I took, I expected the memory to return to 0.3 MB.

This is the code in C ++ that I use:

#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; } 
+4
source share
1 answer

I'm sure this is a duplicate, but I will still answer:

If you are viewing the size of the task manager, this will give you the size of this process. If there is no โ€œpressureโ€ (there is a lot of available memory in your system and the process is not running), it makes no sense to reduce the use of virtual memory in the process - this is not unusual for the growth, compression, growth process, it is compressed in a cyclic template when it allocates when it processes the data, and then frees the data used in one processing cycle, allocating memory for the next cycle, and then freeing it again. If the OS needs to โ€œrestoreโ€ these memory pages, only to return them to your process again, this would be a waste of computing power (assigning and rendering pages to a specific process is not trivial, especially if you cannot know exactly who these pages belong to , first of all, since they need to be "cleared" [filled with zero or some other constant so that the "new owner" could not use the memory for "old data", for example, searching for my password stored in memory]).

Even if the pages still remain the property of this process but are not used, the actual RAM can be used by another process. So this is not a big problem if the pages have not been released for some time.

In addition, in debug mode, the C ++ runtime will save "this memory has been deleted" in all memory that passes through delete . This will help determine "use after free." So, if your application is running in debug mode, do not expect the release of freed EVER memory. It will be reused. Therefore, if you run your code three times, it will not grow three times as much.

+7
source

Source: https://habr.com/ru/post/1485270/


All Articles