← 返回命令列表

Linux command

valgrind 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

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

valgrind --tool=cachegrind [./program]

Use callgrind

valgrind --tool=callgrind [./program]

Use helgrind

valgrind --tool=helgrind [./program]

Generate suppressions

valgrind --gen-suppressions=all [./program]

说明

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).

参数

--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

FAQ

What is the valgrind command used for?

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).

How do I run a basic valgrind example?

Run `valgrind --leak-check=full [./program]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does --tool=_name_ do in valgrind?

Select tool (memcheck, cachegrind, callgrind, helgrind, drd, massif)