valgrind(1)
Valgrind is a dynamic analysis tool that detects memory errors, memory leaks, and profiling issues in programs.
Synopsis
valgrind [OPTION]... [PROGRAM] [ARG]...Description
Valgrind is an instrumentation framework for building dynamic analysis tools. It runs programs under a virtual machine and tracks memory usage, detecting leaks, invalid reads/writes, and use-after-free bugs. The most commonly used tool is Memcheck, which is the default.
Valgrind significantly slows program execution (typically 5-20x) but provides detailed reports of memory errors with line numbers. It's essential for debugging C/C++ programs and finding hard-to-trace memory corruption issues.
Common options
| Flag | What it does |
|---|---|
--leak-check=full | Report all possible memory leaks with full details of where allocations occurred |
--leak-check=summary | Show only summaries of leaked memory, not individual leak details |
--show-leak-kinds=all | Report all kinds of leaks (reachable, indirectly lost, directly lost, and possibly lost) |
--track-origins=yes | Track where uninitialized values come from, useful for finding source of bad data |
--log-file=FILE | Write output to FILE instead of stderr; %p is replaced with process ID |
--suppressions=FILE | Suppress errors defined in FILE; useful for ignoring known third-party library issues |
--gen-suppressions=all | Automatically generate suppression rules for all detected errors |
--db-attach=yes | Pause execution and request debugger attachment on memory errors |
--verbose | Print extra diagnostic information during execution |
--vgdb=full | Enable full debugging with valgrind's internal debugger for step-through analysis |
Examples
Run myprogram under Memcheck and report memory errors to stderr
valgrind ./myprogramCheck for all types of memory leaks with comprehensive output
valgrind --leak-check=full --show-leak-kinds=all ./myprogramRun program with arguments, saving detailed memory report to valgrind.log
valgrind --log-file=valgrind.log --leak-check=full ./myprogram arg1 arg2Track uninitialized value sources with verbose output, pipe to pager
valgrind --track-origins=yes --verbose ./myprogram 2>&1 | lessGenerate suppression rules for all errors to suppress known safe leaks
valgrind --gen-suppressions=all ./myprogram > suppressions.txt 2>&1Profile CPU cache behavior instead of checking memory (use different tool)
valgrind --tool=cachegrind ./myprogramPause on errors and prompt for debugger attachment for live inspection
valgrind --db-attach=yes ./myprogram