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?
Heap memory is such a place. Unlike stack memory, heap memory is allocated explicitly by programmers and it won't be deallocated until it is explicitly freed.
Only deallocate memory when you are truly finished with that memory. If you have more than one pointer to a chunk of memory, then deallocating that chunk makes all pointers to it stale; they are pointing to memory that you are no longer allowed to use. Those pointers are called dangling pointers.
If we don't deallocate the dynamic memory, it will reside in the heap section.It is also called memory leak. It will reduce the system performance by reducing the amount of available memory.
Object Scope - Objects stored in the Heap are globally accessible. Memory Access - The Heap is accessed via complex memory management techniques that include the Young, Old and Permanent Generations. Life - Heap Space exists as long as the application runs.
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 <stdlib.h>
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()
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With