$linuxjunkies
>

valgrind(1)

Valgrind is a dynamic analysis tool that detects memory errors, memory leaks, and profiling issues in programs.

UbuntuDebianFedoraArch

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

FlagWhat it does
--leak-check=fullReport all possible memory leaks with full details of where allocations occurred
--leak-check=summaryShow only summaries of leaked memory, not individual leak details
--show-leak-kinds=allReport all kinds of leaks (reachable, indirectly lost, directly lost, and possibly lost)
--track-origins=yesTrack where uninitialized values come from, useful for finding source of bad data
--log-file=FILEWrite output to FILE instead of stderr; %p is replaced with process ID
--suppressions=FILESuppress errors defined in FILE; useful for ignoring known third-party library issues
--gen-suppressions=allAutomatically generate suppression rules for all detected errors
--db-attach=yesPause execution and request debugger attachment on memory errors
--verbosePrint extra diagnostic information during execution
--vgdb=fullEnable full debugging with valgrind's internal debugger for step-through analysis

Examples

Run myprogram under Memcheck and report memory errors to stderr

valgrind ./myprogram

Check for all types of memory leaks with comprehensive output

valgrind --leak-check=full --show-leak-kinds=all ./myprogram

Run program with arguments, saving detailed memory report to valgrind.log

valgrind --log-file=valgrind.log --leak-check=full ./myprogram arg1 arg2

Track uninitialized value sources with verbose output, pipe to pager

valgrind --track-origins=yes --verbose ./myprogram 2>&1 | less

Generate suppression rules for all errors to suppress known safe leaks

valgrind --gen-suppressions=all ./myprogram > suppressions.txt 2>&1

Profile CPU cache behavior instead of checking memory (use different tool)

valgrind --tool=cachegrind ./myprogram

Pause on errors and prompt for debugger attachment for live inspection

valgrind --db-attach=yes ./myprogram

Related commands