1

I am experimenting with memory allocation and deletion and had a question about how to properly delete/free memory. Below is a very small and working bit of code:

#include <windows.h>
#include <vector>
#include <iostream>

using namespace std;

int main() {
    cout << "Initial" << endl;
    system("Pause");

    double* Array = new double[50000];
    for(int i = 0; i < 50000; i++)
    {
        Array[i] = rand();
    }

    cout << "Array created" << endl;
    system("Pause");

    delete[] Array;

    cout << "Array deleted" << endl;
    system("Pause");

    return 1;
}

During each system pause, I used Windows Task Manager to check how much memory my application was using. Below are the numbers from my results:

  • Initial 744 KB
  • Array Created 1120 KB
  • Array Deleted 1124 KB

So have I missed something in my C++ education? Should the memory allocated for the array not be freed after delete[] is called?

LucasS
  • 689
  • 3
  • 14
  • 24
  • Read http://stackoverflow.com/a/8611456/726361 and http://stackoverflow.com/a/13039464/726361 – Seth Carnegie Jan 07 '13 at 04:12
  • 2
    This has been asked a thousand times. The base premise of your question that `delete[]` should immediately result in your process memory in Windows decreasing, and that there are no intermediate layers of caching and whatnot, is flawed. In fact there are several -- your array is de-allocated in the process's virtual memory space, but this will pretty much never result in an immediate return of this memory physically to the OS. – Lightness Races in Orbit Jan 07 '13 at 04:12
  • The OS is better at managing memory than you are. `delete` frees up the block of virtual memory, it doesn't mean that task manager is going to immediately report it as returned. Look at the spec to see what `delete` is *specified` to do (as opposed to what you think it *should* do). – Ed S. Jan 07 '13 at 04:13
  • To study memory management you want to check memory used for data, not code. Task manager shows you the latter not the former.You need to use a tool like valgrind to check the former. – Alok Save Jan 07 '13 at 04:14
  • 2
    I recommend reading [this question](http://stackoverflow.com/q/1170654/440558) and its answers. – Some programmer dude Jan 07 '13 at 04:14

2 Answers2

4

No, you did not miss anything. It is just that the Task Manager is not telling you the whole truth (well, it does not tell you the truth from the perspective that you need). When you program calls delete[], the memory is released for reuse by the program, but it is not returned back to the operating system. From your program's point of view, the memory is freed: your next call of new will claim the same memory chunk. But from the OS's (and Task Manager's) point of view, the program still holds on to the memory.

To see what's going on, run your allocations several times, and deallocations in a loop, and see that the total amount of memory in the Task Manager does not go up from the "high water mark" that you get after the first allocation.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

It depends on compiler you use. For some compiler, memory deleted is hold by application, not returning to OS immediately. Sorry that I cannot find an official link about it now.

Steel
  • 125
  • 2
  • 7