These are my personal notes for myself to help with future debugging. Every new
or malloc
in C++ requires a delete
/delete[]
or free
. Not deallocating allocated stack memory results in memory leaks.
- Build the project from the project root or using IDE:
make build
- Run the program from the command line:
make run
or./cmake-build-debug/app/my_app
- Open another
Tmux
pane and runTop
to see memory usage:top -p $(ps -aux | grep my_app | head -n 1 | awk '{print $2}')
- Press
E
to see memory usage in mb inTop
. - Run
Valgrind
to find the memory leak:valgrind --leak-check=full ./my_app
Valgrind
displays memory leaks in programs. Running valgrind FILE
shows how much memory, if any, was leaked:
Running Valgrind
with --leak-check=full
reveals the place where the memory leak may be happening and the stack trace:
After fixing the leak, running Valgrind
again displays the happy news:
Top
displays Linux processes, but it's not so helpful to run by itself - processes shift around and it's hard to keep track of the one you want:
We can use the -p
switch and pass Top
the PID to watch just our app running:
Finding the process each time is repetitive, so we can filter out the PID from the list of processes using:
ps -aux | grep my_app | head -n 1 | awk '{print $2}'
Together with the previous command we get:
top -p $(ps -aux | grep my_app | head -n 1 | awk '{print $2}')
Which displays the memory usage of the app with the new PID each time Top
is run. Now press E
to see the memory displayed in different formats, e.g. MB, GB, TB, etc.