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 smaps analysis in procfs, the heap of one process grows at roughly the same rate. I suspected a memory leak and found a few spots in the code where new and delete are regularly used. However, I did not see a new calls without matching delete calls.
I ran the memory test again and this morning cleared the memory caches with the following call
echo 3 > /proc/sys/vm/drop_caches
The free memory listed in vmstat went down to a value close to when the test was started.
Does the kernel regularly reclaim unused heap pages? If so, are there other times besides the one above that this is done? Probably when free memory gets below a certain threshold?
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()/newa memory block above a certain size, the memory gets allocated from the OS viammap()and eturned as soon as it is free. Smaller blocks are allocated by increasing the process’s data area by shifting thesbrkborder 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)
Memory map:
If you free
anow, 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
leads to a
straceoutput likeis shows that the
brkvalue is first increased (formalloc()) and then decreased again (forfree()).