Node-heap-dump is a project mainly written in JAVASCRIPT and C++, based on the MIT license.
Testing node heap dumps
This is a collection of programs for creating and viewing Node heap dumps using V8 snapshots. This was an experiment to see what run-time structures could be viewed post-mortem using heap snapshots.
There are the following components:
takeSnapshot
, that takes a new
heapsnapshot and serializes it as JSON to stdout (using the built-in V8
mechanism for this).The heap snapshot parser is pretty basic: it resolves references to strings, type names, etc. but does not resolve references to other nodes. This would be easy to add as a second pass.
Note that the heap snapshot is a relatively compact representation; the in-memory representation generated by this parser is substantially larger.
# install dependencies
npm install sprintf
# build the add-on
node-waf configure
node-waf build
node-waf install
# generate a heap snapshot
node sample.js > heapsnapshot
node hdb.js heapsnapshot text > heapsummary.txt
The result is a text file that resembles the snapshot but with various string and type references resolved and presented in more human-readable form.
You can use "explore" to explore a snapshot. It opens up a REPL with the following functions:
node(num)
: return the node with index num
.findstr(str)
: return the node corresponding to the string str
.findrefs(num)
: return nodes referencing the node with index num
.parents(num)
: returns array of nodes referencing the specified nodedump(depth)
: dumps out information from the roottree(num, depth)
: prints a tree of nodesroot(num)
: prints the shortest path from the specified node to the rootFor example:
node hdb.js heapsnapshot explore
> findstr('compost')
{ index: 503438,
type: 'string',
name: 'compost',
children: [] }
> findrefs(503438)
[ { node: '40615',
node_nchildren: 16385,
node_name: '',
type: 'hidden',
name_or_index: 7722,
to_node: 503438 },
{ node: '165335',
node_nchildren: 6,
node_name: 'Array',
type: 'element',
name_or_index: 0,
to_node: 503438 },
{ node: '226953',
node_nchildren: 6,
node_name: 'Array',
type: 'element',
name_or_index: 0,
to_node: 503438 },
{ node: '294910',
node_nchildren: 3,
node_name: '',
type: 'hidden',
name_or_index: 2,
to_node: 503438 } ]
> node(165335)
{ index: 165335,
type: 'object',
name: 'Array',
children:
[ { type: 'element', name_or_index: 0, to_node: 503438 },
{ type: 'element', name_or_index: 1, to_node: 566310 },
{ type: 'property',
name_or_index: '__proto__',
to_node: 40475 },
{ type: 'hidden', name_or_index: 1, to_node: 248392 },
{ type: 'hidden', name_or_index: 2, to_node: 570782 },
{ type: 'hidden', name_or_index: 3, to_node: 294910 } ] }
Though it's not obvious if you haven't spent time looking at V8 heap dumps, we've established that node 165335 is an array whose first element is the string 'compost'.
The snapshot format is documented in:
node/deps/v8/include/v8-profiler.h
This discussion contains additional useful information:
http://groups.google.com/group/google-chrome-developer-tools/browse_thread/thread/a5f86cb20fa1e9eb/?pli=1