As others have said, it is the responsibility of a process to return memory to the kernel.
Usually there are 2 ways to allocate memory: if you malloc() / new memory block is above a certain size, the memory receives the allocation from the OS via mmap() and is called as soon as it is free. Smaller blocks are allocated by increasing the process data area, shifting the sbrk boundary up. This memory is freed only if a block of a certain size is free at the end of this segment.
For example: (pseudo code, I don't know C ++ very well)
a = new char[1000]; b = new char[1000];
Memory card:
---------------+---+---+ end of program | a | b | ---------------+---+---+
If you free a now, you have a hole in the middle. He is not released because he cannot be released. If you free b , the process memory may or may not be reduced; unused balance is returned to the system.
A test with such a simple program as
causes strace to exit, sort of
brk(0) = 0x804b000 brk(0x8084000) = 0x8084000 brk(0x80b5000) = 0x80b5000 brk(0x809c000) = 0x809c000 brk(0x8084000) = 0x8084000 brk(0x806c000) = 0x806c000
shows that the brk value first increases (for malloc() ) and then decreases again (for free() ).
source share