10

When I use the top terminal program at Linux, I can't see the result of free.

My expectation is:

  1. free map and list.

  2. The memory usage that I can see at the top(Linux function) or /proc/meminfo get smaller than past.

  3. sleep is start.

  4. program exit.

But The usage of memory only gets smaller when the program ends.

Would you explain the logic of free function?

Below is my code.

for(mapIter = bufMap->begin(); mapIter != bufMap -> end();mapIter++)
{
    list<buff> *buffList = mapIter->second;
    list<buff>::iterator listIter;
    for(listIter = buffList->begin(); listIter != buffList->end();listIter++)
    {
        free(listIter->argu1);
        free(listIter->argu2);
        free(listIter->argu3);
    }
    delete buffList;
}
delete bufMap;

printf("Free Complete!\n");

sleep(10);
printf("endend\n");

Thanks you.

Kingsley
  • 14,398
  • 5
  • 31
  • 53
Sukhwan yoon
  • 123
  • 1
  • 6
  • 1
    Here's a relevant question about the same behavior on Windows: https://stackoverflow.com/questions/17008180/c-delete-does-not-free-all-memory-windows – alter_igel Sep 20 '18 at 03:55
  • 3
    Termionology note (because it sucked me in to this question under false pretenses): [What is the meaning of the term “free function” in C++?](https://stackoverflow.com/questions/4861914/what-is-the-meaning-of-the-term-free-function-in-c) – user4581301 Sep 20 '18 at 03:55
  • 1
    [Will malloc implementations return free-ed memory back to the system?](https://stackoverflow.com/q/2215259/608639), [Force free() to return malloc memory back to OS](https://stackoverflow.com/q/27945855/608639), etc. – jww Oct 14 '19 at 14:03

2 Answers2

19

Memory is allocated onto a heap.

When you request some memory in your program (with a new() or malloc() etc.) Your program requests some memory from its heap, which in turn requests it from the operating system{1}. Since this is an expensive operation, it gets a chunk of memory from the OS, not just what you ask for. The memory manager puts everything it gets into the heap, just returning to you the perhaps small amount you asked for. When you free() or delete() this memory, it simply gets returned to the heap, not the OS.

It's absolutely normal for that memory to not be returned to the operating system until your program exits, as you may request further memory later on.

If your program design relies on this memory be recycled, it may be achievable using multiple copies of your program (by fork()~ing) which run and exit.

{1} The heap is probably non-empty on program start, but assuming it's not illustrates my point.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
Kingsley
  • 14,398
  • 5
  • 31
  • 53
2

The heap typically uses operating system functions to manage its memory. The heap’s size may be fixed when the program is created, or it may be allowed to grow. However, the heap manager does not necessarily return memory to the operating system when the free function is called. The deallocated memory is simply made available for subsequent use by the application. Thus, when a program allocates and then frees up memory, the deallocation of memory is not normally reflected in the application’s memory usage as seen from the operating system perspective.