valgrind

Memory debugging and profiling framework

TLDR

Check for memory leaks
$ valgrind --leak-check=full [./program]
Run with detailed leak info
$ valgrind --leak-check=full --show-leak-kinds=all [./program]
Track memory origins
$ valgrind --track-origins=yes [./program]
Use cachegrind for cache profiling
$ valgrind --tool=cachegrind [./program]
Use callgrind for call profiling
$ valgrind --tool=callgrind [./program]
Use helgrind for thread error detection
$ valgrind --tool=helgrind [./program]
Generate suppressions for known issues
$ valgrind --gen-suppressions=all [./program]

SYNOPSIS

valgrind [--tool=toolname] [options] program [args]

DESCRIPTION

valgrind is an instrumentation framework for dynamic analysis tools. The default tool, memcheck, detects memory management problems: leaks, use of uninitialized memory, buffer overflows, and invalid frees.Programs run significantly slower under Valgrind (10-50x) as every memory access is instrumented. This is normal and expected.Output indicates error type, location (with line numbers if compiled with -g), and call stack. "Definitely lost" memory is a real leak; "still reachable" may be acceptable cleanup deferred to exit.Other tools profile performance (cachegrind, callgrind), detect threading issues (helgrind, drd), or analyze heap usage (massif).

PARAMETERS

--tool=name

Select tool (memcheck, cachegrind, callgrind, helgrind, drd, massif)
--leak-check=level
Check for memory leaks (no, summary, full)
--show-leak-kinds=kinds
Which leaks to show (definite, indirect, possible, reachable, all)
--track-origins=yes|no
Track origins of uninitialized values
--log-file=file
Write output to file
--xml=yes
Output in XML format
--gen-suppressions=level
Generate suppression entries (no, yes, all)
--suppressions=file
Use suppressions from file
-v, --verbose
More verbose output
-q, --quiet
Less verbose output
--num-callers=N
Maximum stack depth for error reports (default: 12)
--vgdb=yes|no|full
Enable gdb server for debugging under Valgrind

TOOLS

memcheck: Memory error detector (default)cachegrind: Cache and branch profilercallgrind: Call-graph profilerhelgrind: Thread error detectordrd: Thread error detector (different algorithm)massif: Heap profiler

INSTALL

sudo apt install valgrind
sudo dnf install valgrind
sudo pacman -S valgrind
sudo apk add valgrind
sudo zypper install valgrind
brew install valgrind
nix profile install nixpkgs#valgrind

CAVEATS

Compile programs with -g for line numbers and -O0 or -O1 for accurate debugging (high optimization confuses source mapping).False positives occur, especially with system libraries. Use suppression files to ignore known issues.Valgrind doesn't work well with JIT compilers or custom memory allocators without additional configuration.

HISTORY

Valgrind was created by Julian Seward and first released in 2002. The name comes from the entrance to Valhalla in Norse mythology. It has become the standard memory debugging tool for C/C++ on Linux.

SEE ALSO

gdb(1), strace(1), ltrace(1), asan(7)