Skip to main content
skill CX Codex

valgrind — Claude Code skill: memory leaks and cache profiling

Walks the agent through Valgrind tools: Memcheck for leaks and use-after-free, Cachegrind and Callgrind for cache misses and call graphs, Massif for heap growth.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add mohitmishra786/low-level-dev-skills --skill valgrind -a claude-code
Open on GitHub

What valgrind does

The skill covers four Valgrind tools and how to read what they print. For Memcheck it gives a ready command with --leak-check=full, --show-leak-kinds=all and --track-origins=yes, plus a flag table: --error-exitcode=1 for CI, --log-file, --suppressions, --malloc-fill and --free-fill. There is a walkthrough of a typical report — how "Invalid read" differs from "Use of uninitialised value", and why "Possibly lost" is often a false positive with custom allocators.

Then come Cachegrind with cg_annotate and run-to-run diffs via cg_diff, Callgrind with callgrind_annotate and KCachegrind, Massif with ms_print. A separate section handles suppressions: generating a directive with --gen-suppressions=yes and loading a valgrind.supp file when errors come from code you cannot fix.

Compile with -g -O1; -O2 and above produce false positives. Memcheck runs roughly 10–50x slower than native, so the skill suggests a shorter representative workload and ASan for day-to-day checking. Valgrind stays useful where a sanitizer is not an option — old toolchains or production-like environments.

What it helps with

  • track down a memory leak in C without sanitizers
  • decode a Memcheck report about invalid read or double free
  • suppress false positives coming from third-party libraries
  • find which source lines cause L1 and LL cache misses
  • build a call graph and open it in KCachegrind

Example prompts for the agent

  • Run my program under Memcheck with full leak checking and show me where the memory goes
  • Collect a Cachegrind profile and tell me which lines miss L1 the most
  • Generate a suppression file for the errors coming out of libssl so they stop flooding the report

How to install valgrind

1Skills CLI (Vercel)

npx skills add mohitmishra786/low-level-dev-skills --skill valgrind -a claude-code

2Manually: put the files in place

~/.claude/skills/valgrind/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/valgrind/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about valgrind

How do I install the valgrind skill in Claude?

Run npx skills add mohitmishra786/low-level-dev-skills --skill valgrind -a claude-code. The skill file then lands at ~/.claude/skills/valgrind/SKILL.md.

What does the valgrind skill do?

It tells the agent how to run Memcheck, Cachegrind, Callgrind and Massif, and how to read their output: leak kinds, invalid reads, cache miss rates, call graphs.

How is Valgrind different from ASan?

ASan is faster and fits everyday development, while Memcheck slows a program down by 10–50x. In exchange, Valgrind needs no rebuild with a sanitizer and works on old toolchains.

Source text

SKILL.md · MIT
---
name: valgrind
description: Valgrind profiler skill for memory error detection and cache profiling. Use when running Memcheck to find heap corruption, use-after-free, memory leaks, or uninitialised reads; or Cachegrind/Callgrind for cache simulation and function-level profiling. Activates on queries about valgrind, memcheck, heap leaks, use-after-free without sanitizers, cachegrind, callgrind, KCachegrind, or massif memory profiling.
---

# Valgrind

## Purpose

Guide agents through Valgrind tools: Memcheck for memory errors, Cachegrind for cache simulation, Callgrind for call graphs, and Massif for heap profiling.

## Triggers

- "My program has a memory leak / use-after-free"
- "I can't use ASan — can I use Valgrind instead?"
- "How do I profile cache behaviour without perf?"
- "How do I visualize call graphs with Callgrind?"
- "How do I profile heap allocation patterns?"
- "Valgrind reports errors in third-party code I can't fix"

## Workflow

### 1. Memcheck — memory error detection

Compile with `-g -O1` for best results. `-O0` is also fine; avoid `-O2`+ which can produce false positives.

```bash
valgrind --tool=memcheck \
         --leak-check=full \
         --show-leak-kinds=all \
         --track-origins=yes \
         --error-exitcode=1 \
         ./prog [args]
```

Key flags:

| Flag | Default | Effect |
|------|---------|--------|
| `--leak-check=full` | summary | Full leak details |
| `--show-leak-kinds=all` | definite | Show all leak kinds |
| `--track-origins=yes` | no | Show where uninit values came from (slow) |
| `--error-exitcode=N` | 0 | Exit N if errors found (CI integration) |
| `--log-file=file` | stderr | Save report to file |
| `--suppressions=file` | none | Suppress known FPs |
| `--gen-suppressions=yes` | no | Print suppression directives for errors |
| `--max-stackframe=N` | 2000000 | Increase for deep stacks |
| `--malloc-fill=0xAB` | off | Fill allocated memory (detect uninit use) |
| `--free-fill=0xCD` | off | Fill freed memory (detect use-after-free) |

### 2. Understanding Memcheck output

```text
==12345== Invalid read of size 4
==12345==    at 0x4007A2: foo (main.c:15)
==12345==    by 0x400846: main (main.c:30)
==12345==  Address 0x5204040 is 0 bytes after a block of size 40 alloc'd
==12345==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck.so)
==12345==    by 0x40074B: main (main.c:25)
```

- **Invalid read/write**: out-of-bounds access; check array bounds
- **Use of uninitialised value**: read before write; use `--track-origins=yes`
- **Invalid free / double free**: mismatched malloc/free; check ownership
- **Definitely lost**: reachable via no pointers; clear leak
- **Indirectly lost**: lost through a chain; usually means one root leak
- **Possibly lost**: might be pointing into the middle of a block; often FP with custom allocators

### 3. Leak kinds

| Kind | Meaning |
|------|---------|
| Definitely lost | No pointer to block |
| Indirectly lost | Lost via another lost block |
| Possibly lost | Pointer into middle of block |
| Still reachable | Pointer exists at exit; not a leak but never freed |

For library code: `--show-leak-kinds=definite,indirect` reduces noise from still-reachable.

### 4. Suppressions

```bash
# Generate suppression for current error
valgrind --gen-suppressions=yes ./prog 2>&1 | grep -A20 '{'

# Example suppression file (valgrind.supp)
{
   openssl_uninit
   Memcheck:Cond
   fun:SHA256_Init
   ...
}

# Use suppression file
valgrind --suppressions=valgrind.supp ./prog
```

### 5. Cachegrind — cache simulation

```bash
valgrind --tool=cachegrind ./prog

# Output: cachegrind.out.PID
# Annotate source
cg_annotate cachegrind.out.12345 --auto=yes

# Diff two runs
cg_diff cachegrind.out.before cachegrind.out.after
```

Key metrics:

- `I1mr` / `ILmr`: L1/LL instruction cache miss rate
- `D1mr` / `DLmr`: L1/LL data read miss rate
- `D1mw` / `DLmw`: L1/LL data write miss rate

### 6. Callgrind — call graph profiling

```bash
valgrind --tool=callgrind --callgrind-out-file=callgrind.out ./prog

# Analyse
callgrind_annotate callgrind.out

# Visualise in KCachegrind (GUI)
kcachegrind callgrind.out
```

Callgrind is slower than `perf` but works without root and provides exact call counts.

### 7. Massif — heap profiling

```bash
valgrind --tool=massif ./prog

# Visualise
ms_print massif.out.PID | less

# GUI
massif-visualizer massif.out.PID
```

Massif shows heap usage over time; useful for finding peak allocation sites and tracking gradual leaks.

### 8. Performance considerations

Valgrind Memcheck runs ~10-50x slower than native. Mitigations:

- Use a shorter representative workload
- Use `--error-exitcode=1` to fail fast in CI
- Use ASan (`-fsanitize=address`) for faster memory checking during development
- Reserve Valgrind for cases where ASan can't be used (old toolchains, production-like environments)

For a comparison of Valgrind vs ASan, see [references/valgrind-vs-asan.md](references/valgrind-vs-asan.md).

## Related skills

- Use `skills/runtimes/sanitizers` for faster ASan/UBSan alternatives
- Use `skills/profilers/linux-perf` for CPU-level profiling (faster than Cachegrind)
- Use `skills/profilers/flamegraphs` to visualise Callgrind output

© mohitmishra786 · MIT · mohitmishra786/low-level-dev-skills

Similar skills

Everything for Testing and QA →

From the same set62

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor