worker.startHeapProfile([options])


  • options <Object>
    • sampleInterval <number> 平均采样间隔(以字节为单位)。 默认值: 524288(512 KiB)。
    • stackDepth <integer> 样本的最大堆栈深度。 默认值: 16
    • forceGC <boolean> 在进行性能分析前强制进行垃圾回收。 默认值: false
    • includeObjectsCollectedByMajorGC <boolean> 包含主要 GC 收集的对象。默认值: false
    • includeObjectsCollectedByMinorGC <boolean> 包含由次级 GC 收集的对象。默认值: false
  • 返回:<Promise>

启动堆分析,然后返回一个 Promise,该 Promise 要么以错误完成,要么返回一个 HeapProfileHandle 对象。此 API 支持 await using 语法。

🌐 Starting a Heap profile then return a Promise that fulfills with an error or an HeapProfileHandle object. This API supports await using syntax.

const { Worker } = require('node:worker_threads');

const worker = new Worker(`
  const { parentPort } = require('worker_threads');
  parentPort.on('message', () => {});
  `, { eval: true });

worker.on('online', async () => {
  const handle = await worker.startHeapProfile();
  const profile = await handle.stop();
  console.log(profile);
  worker.terminate();
});import { Worker } from 'node:worker_threads';

const worker = new Worker(`
  const { parentPort } = require('node:worker_threads');
  parentPort.on('message', () => {});
  `, { eval: true });

worker.on('online', async () => {
  const handle = await worker.startHeapProfile();
  const profile = await handle.stop();
  console.log(profile);
  worker.terminate();
});

await using 示例。

const { Worker } = require('node:worker_threads');

const w = new Worker(`
  const { parentPort } = require('node:worker_threads');
  parentPort.on('message', () => {});
  `, { eval: true });

w.on('online', async () => {
  // Stop profile automatically when return and profile will be discarded
  await using handle = await w.startHeapProfile();
});import { Worker } from 'node:worker_threads';

const w = new Worker(`
  const { parentPort } = require('node:worker_threads');
  parentPort.on('message', () => {});
  `, { eval: true });

w.on('online', async () => {
  // Stop profile automatically when return and profile will be discarded
  await using handle = await w.startHeapProfile();
});