LinuxCommandLibrary

valgrind

Memory debugging and profiling framework

TLDR

Check for memory leaks

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

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

TOOLS

memcheck: Memory error detector (default)
cachegrind: Cache and branch profiler
callgrind: Call-graph profiler
helgrind: Thread error detector
drd: Thread error detector (different algorithm)
massif: Heap profiler

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.

SEE ALSO

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

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community