Identifying memory leaks
A memory leak occurs when memory is allocated but not freed when it is no longer needed. Memory leakage is by no means unique to embedded systems, but it becomes an issue partly because targets don't have much memory in the first place and partly because they often run for long periods of time without rebooting, allowing the leaks to become
a large puddle.
You will realize that there is a leak when you run free
or top
and see that free memory is continually going down even if you drop caches, as shown in the preceding section. You will be able to identify the culprit (or culprits) by looking at the USS and RSS per process.
There are several tools to identify memory leaks in a program. I will look at two: mtrace
and valgrind
.
mtrace
mtrace is a component of glibc
that traces calls to malloc
, free
, and related functions, and identifies areas of memory not freed when the program exits. You need to call the mtrace()
function from within the program...