Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the free() function not return memory to the operating system?

Tags:

c++

linux

redhat

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.

like image 232
Sukhwan yoon Avatar asked Sep 20 '18 03:09

Sukhwan yoon


People also ask

What happens if heap memory is not freed?

It becomes a problem when you write functions (other than main) that allocate memory without freeing it, and without making it available to the rest of your program. Then your program continues running with that memory allocated to it, but no way of using it.

What does Malloc_trim do?

The malloc_trim() function attempts to release free memory from the heap (by calling sbrk(2) or madvise(2) with suitable arguments). The pad argument specifies the amount of free space to leave untrimmed at the top of the heap.


2 Answers

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.

like image 176
Kingsley Avatar answered Sep 18 '22 17:09

Kingsley


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.

like image 38
BsHr Алвани Avatar answered Sep 20 '22 17:09

BsHr Алвани