v8.writeHeapSnapshot([filename])


  • filename <string> 要保存 V8 堆快照的文件路径。 如果未指定,则将会生成具有 'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot' 模式的文件名, 其中 {pid} 将会是 Node.js 进程的 PID, {thread_id} 将会为 0(当从主 Node.js 线程调用 writeHeapSnapshot() 时)或工作线程的 id。
  • 返回: <string> 保存快照的文件名。

生成当前 V8 堆的快照并将其写入 JSON 文件。 此文件旨在与 Chrome DevTools 等工具一起使用。 JSON 模式未记录且特定于V8引擎,并且可能从 V8 的一个版本更改为下一个版本。

堆快照特定于单个 V8 隔离。 使用工作线程时,从主线程生成的堆快照将不包含有关工作线程的任何信息,反之亦然。

const { writeHeapSnapshot } = require('v8');
const {
  Worker,
  isMainThread,
  parentPort
} = require('worker_threads');

if (isMainThread) {
  const worker = new Worker(__filename);

  worker.once('message', (filename) => {
    console.log(`工作线程的堆转储: ${filename}`);
    // 获取主线程的堆转储。
    console.log(`主线程的堆转储: ${writeHeapSnapshot()}`);
  });

  // 通知工作线程创建一个堆转储。
  worker.postMessage('heapdump');
} else {
  parentPort.once('message', (message) => {
    if (message === 'heapdump') {
      // 为工作线程生成一个堆转储,并返回文件名到父线程。
      parentPort.postMessage(writeHeapSnapshot());
    }
  });
}
  • filename <string> The file path where the V8 heap snapshot is to be saved. If not specified, a file name with the pattern 'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot' will be generated, where {pid} will be the PID of the Node.js process, {thread_id} will be 0 when writeHeapSnapshot() is called from the main Node.js thread or the id of a worker thread.
  • Returns: <string> The filename where the snapshot was saved.

Generates a snapshot of the current V8 heap and writes it to a JSON file. This file is intended to be used with tools such as Chrome DevTools. The JSON schema is undocumented and specific to the V8 engine, and may change from one version of V8 to the next.

A heap snapshot is specific to a single V8 isolate. When using worker threads, a heap snapshot generated from the main thread will not contain any information about the workers, and vice versa.

const { writeHeapSnapshot } = require('v8');
const {
  Worker,
  isMainThread,
  parentPort
} = require('worker_threads');

if (isMainThread) {
  const worker = new Worker(__filename);

  worker.once('message', (filename) => {
    console.log(`worker heapdump: ${filename}`);
    // Now get a heapdump for the main thread.
    console.log(`main thread heapdump: ${writeHeapSnapshot()}`);
  });

  // Tell the worker to create a heapdump.
  worker.postMessage('heapdump');
} else {
  parentPort.once('message', (message) => {
    if (message === 'heapdump') {
      // Generate a heapdump for the worker
      // and return the filename to the parent.
      parentPort.postMessage(writeHeapSnapshot());
    }
  });
}