(Ubuntu) Using Valgrind to Find Memory Leaks

Valgrind is a memory debugging tool available to Linux based machines. It runs your programs and keeps track of memory usage from calls such as malloc or free. If the program uses uninitialized memory or some chunk of memory is not freed, Valgrind will detect it and let you know. Easier debugging is just a few steps away.

First, we need to install Valgrind. The command for that is:
sudo apt-get install valgrind

To check whether there are memory leaks from a missing free call:
valgrind –leak-check=full -v ./[program name]

You should get a heap summary that will tell you how the number of memory allocations compare to the number of memory frees. If the two values are the same and you get the message, “All heap blocks were freed — no leaks are possible,” then congratulations! If not, it’s time to fix those memory leaks… (if you want to)

NOTE: There are 2 dashes in front of leak-check=full.


One thought on “(Ubuntu) Using Valgrind to Find Memory Leaks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s