Skip to content
adrsimonPublic

About

A tool to query V8 heap snapshots from the command line. Built for coding agents.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

heapd

Query V8 heap snapshots from the command line. Built for coding agents.

Node.js services can leak memory. heapd makes the object graph you'd normally dig through in Chrome DevTools queryable from the command line, in a single invocation.

$ heapd diff before.heapsnapshot after.heapsnapshot
$ heapd query after.heapsnapshot 'PATH TO gc_root FROM id = 6363'
[
  {"edge":null,"constructor":"QueryCache","id":6363,"retained_size":106710120,...},
  {"edge":"__cache","constructor":"global","id":6059,"retained_size":41896,...}
]

A 96-byte object retaining 106MB, pinned by global.__cache.

Install

cargo install --git https://github.com/adrsimon/heapd

Use it

heapd summary app.heapsnapshot            # top classes by retained size
heapd diff before.heapsnapshot after.heapsnapshot
heapd query app.heapsnapshot '<query>'
heapd schema                              # columns, operators, grammar

Every command takes --format table if you'd rather read columns than JSON.

Getting a snapshot out of Node:

require("v8").writeHeapSnapshot("/tmp/app.heapsnapshot")

The query language

FROM objects WHERE retained_size > 10MB ORDER BY retained_size DESC LIMIT 20

Columns are id, constructor, type, name, shallow_size, retained_size, edge_count, distance_to_root, with =, !=, <, <=, >, >=, LIKE, IN, AND, OR, NOT and KB/MB/GB literals.

Four graph forms answer the questions a filter can't:

PATH TO gc_root FROM id = 4821            -- why is this still alive?
RETAINED BY id = 4821                     -- direct retainers, one hop
REACHES id = 4821 LIMIT DEPTH 10          -- transitive retainers
DOMINATED BY constructor = "QueryCache"   -- everything that dies with it

heapd schema prints all of this as JSON, and a mistyped column tells you what you meant:

$ heapd query app.heapsnapshot 'FROM objects WHERE retained > 10MB'
parse: unknown column `retained`; did you mean `retained_size`? Valid columns: id, constructor, ...

How it works

The first call against a snapshot parses it into a SQLite cache under ~/.cache/heapd/<sha256>.sqlite. Later calls are warm.

Parsing streams the JSON envelope with serde_json and hands the two flat integer arrays to a byte scanner. Nodes are stored as struct-of-arrays, edges as CSR. Lengauer-Tarjan then gives dominators and retained sizes. Filters compile to SQL; graph queries walk an in-memory reverse CSR, because recursive CTEs over 20M edges are too slow.

Tests

The integration tests run against two generated snapshots. They aren't committed, because V8's byte-level output varies between runs, so generate them first:

npx -y tsx --expose-gc tests/fixtures/generate-leak.ts
cargo test

About

A tool to query V8 heap snapshots from the command line. Built for coding agents.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages