I have been running overnight memory tests on an embedded Linux system. Using vmstat I have observed that the free memory steadily decreases over time. According to some
As others said, it is the process's duty to return memory to the kernel.
Usually there are 2 ways to allocate memory: if you malloc()
/new
a memory block above a certain size, the memory gets allocated from the OS via mmap()
and eturned as soon as it is free. Smaller blocks are allocated by increasing the process's data area by shifting the sbrk
border upwards. This memory is only freed if a block over a certain size is free at the end of that segment.
E.g.: (pseudo code, I don't know C++ very well)
a = new char[1000];
b = new char[1000];
Memory map:
---------------+---+---+
end of program | a | b |
---------------+---+---+
If you free a
now, you have a hole in the middle. It is not freed because it cannot be freed.
If you free b
, the process's memory may or may not be reduced; the unused remainder is returned to the system.
A test with a program as simple as
#include
int main()
{
char * a = malloc(100000);
char * b = malloc(100000);
char * c = malloc(100000);
free(c);
free(b);
free(a);
}
leads to a strace
output like
brk(0) = 0x804b000
brk(0x8084000) = 0x8084000
brk(0x80b5000) = 0x80b5000
brk(0x809c000) = 0x809c000
brk(0x8084000) = 0x8084000
brk(0x806c000) = 0x806c000
is shows that the brk
value is first increased (for malloc()
) and then decreased again (for free()
).