Skip to content

Instantly share code, notes, and snippets.

@bobetocalo
Last active August 29, 2015 14:05
Show Gist options
  • Save bobetocalo/27229b3572c44c3d0bd0 to your computer and use it in GitHub Desktop.
Save bobetocalo/27229b3572c44c3d0bd0 to your computer and use it in GitHub Desktop.
Profile C++ application with Callgrind and Memcheck

Profiling the application with Valgrind

Quick course in using tools for Valgrind. These tools will analyze your program's execution and tell you what parts are running slow. The results are accurate because of the way Valgrind works and help you spot problems such as lines of code that execute too much, hot spots, memory access problems, and even CPU cache misses.

[Callgrind documentation] (http://valgrind.org/docs/manual/cl-manual.html).

Callgrind is a tool in part of the Valgrind toolchain. It is running in Valgrind framework. The principle is not the same. When you use Callgrind to profile an application, your application is transformed in an intermediate language and then ran in a virtual processor emulated by valgrind. This has a huge run-time overhead, but the precision is really good and your profiling data is complete. An application running in Callgrind can be 10 to 50 times slower than normally.

valgrind --tool=callgrind ./HelloC++
kcachegrind

Image of kcachegrind

[Memcheck documentation] (http://valgrind.org/docs/manual/mc-manual.html).

Memcheck is a memory error detector. It can detect the following problems that are common in C and C++ programs:

  • Accessing memory you shouldn't
  • Using undefined values
  • Incorrect freeing of heap memory
  • Memory leaks
valgrind --tool=memcheck --leak-check=yes --log-file=example.log ./HelloC++
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment