Memory Management checks with memcheck

Memcheck is a tool to help find and fix memory problems in your code. It can help find and diagnose a range of memory-related problems such as accessing memory which shouldn’t have been, finding memory leaks and double-freeing. When using these tools your programs will have a large overhead (time, memory usage). This is to be expected as there is a lot going on behind the scenes with valgrind. Be patient with execution times.

Memcheck is a tool to help find and fix memory problems in your code. It can help find and diagnose a range of memory-related problems such as accessing memory which shouldn’t have been, finding memory leaks and double-freeing.

When using these tools your programs will have a large overhead (time, memory usage). This is to be expected as there is a lot going on behind the scenes with valgrind. Be patient with execution times.

Running memcheck

To run memcheck, run valgrind passing in your program and the arguments needed to run it.

valgrind ./build/myprog arg1 arg2

Valgrind will report errors as they happen and print them to the console.

Track origins

Once problems have been identified it can be useful to know where the source of the allocation was. The flag to do this is --track-origins=yes.

valgrind --track-origins=yes ./build/myprog arg1 arg2

Memory leaks

Another common problem with languages that require you to do your own memory management is leaking memory. This can be quite an issue for long running applications or applications which reguarly allocate large chunks of memory.

To find memory leaks, you can use the --leak-check=yes flag:

valgrind --leak-check=yes ./build/myprog arg1 arg2
about 300 Words.