Valgrind is an instrumentation framework for building dynamic analysis tools. There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. You can also use Valgrind to build new tools.
The Valgrind distribution currently includes five production-quality tools: a memory error detector, a thread error detector, a cache and branch-prediction profiler, a call-graph generating cache profiler, and a heap profiler. It also includes two experimental tools: a data race detector, and an instant memory leak detector.The Valgrind tool suite provides a number of debugging and profiling tools. The most popular is Memcheck, a memory checking tool which can detect many common memory errors such as:
- Touching memory you shouldn’t (eg. overrunning heap block boundaries, or reading/writing freed memory).
- Using values before they have been initialized.
- Incorrect freeing of memory, such as double-freeing heap blocks.
- Memory leaks.
Note that programs running under Valgrind run significantly more slowly, and use much more memory – e.g. more than twice as much as normal under the Memcheck tool.
Please consult the Valgrind FAQ and the Valgrind User Manual, which have much more information. Note that the other tools in the Valgrind distribution can be invoked with the
UsageTo use valgrind, just type valgrind. Check the out the valgrind manpage for usage information. What follows below is a simple use case of valgrind. Compile your program with
-O0 is also a good idea, if you can tolerate the slowdown. With
-O1 line numbers in error messages can be inaccurate, although generally speaking Memchecking code compiled at
-O1 works fairly well. Use of
-O2 and above is not recommended as Memcheck occasionally reports uninitialised-value errors which don’t really exist.
If you normally run your program like this:
myprog arg1 arg2Use this command line:
valgrind --leak-check=yes myprog arg1 arg2Memcheck is the default tool. The –leak-check option turns on the detailed memory leak detector.