v8.getHeapSnapshot([options])
options<Object>- 返回:<stream.Readable> 一个包含 V8 堆快照的可读流。
生成当前 V8 堆的快照,并返回一个可读取流,可用于读取 JSON 序列化表示形式。此 JSON 流格式旨在与诸如 Chrome DevTools 之类的工具一起使用。JSON 模式未记录,并且特定于 V8 引擎。因此,该模式可能会随着 V8 的不同版本而更改。
【Generates a snapshot of the current V8 heap and returns a Readable Stream that may be used to read the JSON serialized representation. This JSON stream format is intended to be used with tools such as Chrome DevTools. The JSON schema is undocumented and specific to the V8 engine. Therefore, the schema may change from one version of V8 to the next.】
创建堆快照时需要的内存大约是创建快照时堆大小的两倍。这会导致 OOM 杀手可能终止该进程的风险。
【Creating a heap snapshot requires memory about twice the size of the heap at the time the snapshot is created. This results in the risk of OOM killers terminating the process.】
生成快照是一个同步操作,会根据堆大小阻塞事件循环一段时间。
【Generating a snapshot is a synchronous operation which blocks the event loop for a duration depending on the heap size.】
// Print heap snapshot to the console
const v8 = require('node:v8');
const stream = v8.getHeapSnapshot();
stream.pipe(process.stdout);