Build heapdump from source
masterIf you need to build the native addon manually, use node-gyp to configure and build the project.
node-gyp configure buildrepository·master·Indexed 25 days ago
https://github.com/bnoordhuis/node-heapdumpA native Node.js addon used to create V8 heap snapshots for memory profiling and debugging. It provides the writeSnapshot() function to save the current state of the heap to a file and supports triggering snapshots via the SIGUSR2 signal on UNIX platforms.
If you need to build the native addon manually, use node-gyp to configure and build the project.
node-gyp configure buildTo add heapdump to your project, use the standard npm installation command.
npm install heapdumpTo view the contents of a .heapsnapshot file:
Important: Chrome will only load files that have the .heapsnapshot extension. Large snapshots may take minutes or even hours to load.
On UNIX platforms, you can force a snapshot by sending the SIGUSR2 signal to the Node.js process using the kill command.
To disable the default SIGUSR2 signal handler, set the NODE_HEAPDUMP_OPTIONS environment variable to nosignal.
If your snapshot files are empty or truncated, it is likely due to resource exhaustion.
Key Considerations:
dmesg to see if the system's Out-Of-Memory (OOM) killer terminated the process.ulimit -u (max user processes) or ulimit -v (max virtual memory size).You can intercept the SIGUSR2 signal in your application code to control where the snapshot is saved. It is recommended to check process.env.NODE_HEAPDUMP_OPTIONS to ensure you don't conflict with the nosignal setting.
if (!/nosignal/.test(process.env.NODE_HEAPDUMP_OPTIONS)) {
process.on("SIGUSR2", function() {
heapdump.writeSnapshot('/var/local/' + Date.now() + '.heapsnapshot');
});
}You can configure the behavior of node-heapdump using the NODE_HEAPDUMP_OPTIONS environment variable. This is processed during the initial module load.
Supported options (comma-separated):
signal: Enables the use of signals to trigger heap dumps.nosignal: Disables the use of signals to trigger heap dumps.Example: NODE_HEAPDUMP_OPTIONS="signal" node my-app.js
The heapdump module exports a writeSnapshot([filename], [callback]) function.
heapdump-<sec>.<usec>.heapsnapshot.(err, filename).Note: The snapshot is written synchronously to disk. For large JS heaps, this may cause a noticeable performance
The writeSnapshot function allows you to trigger a V8 heap snapshot. It can be used to save the current state of the heap to a file for later inspection.
Behavior:
filename is provided, the snapshot is written to that path.filename is omitted, the library uses a default filename.true if the snapshot was successful, and false if it failed.Arguments:
filename (string|function): The path where the snapshot should be saved. If you pass a function as the first argument, it is treated as the callback, and the filename will be undefined (using the default).cb (function): A callback function that is called with (err, filename). On success, err is null and filename is the path to the snapshot. On failure, err is an Error object containing the error code or message.