--heapsnapshot-near-heap-limit=max_count


稳定性: 1 - 实验性

当 V8 堆使用量接近堆限制时,将 V8 堆快照写入磁盘。count 应该是一个非负整数(在这种情况下,Node.js 不会将超过 max_count 个快照写入磁盘)。

【Writes a V8 heap snapshot to disk when the V8 heap usage is approaching the heap limit. count should be a non-negative integer (in which case Node.js will write no more than max_count snapshots to disk).】

在生成快照时,可能会触发垃圾回收,从而降低堆的使用量。因此,在 Node.js 实例最终耗尽内存之前,可能会将多个快照写入磁盘。这些堆快照可以进行比较,以确定在连续快照拍摄期间正在分配哪些对象。不能保证 Node.js 会正好将 max_count 个快照写入磁盘,但它会尽力在 Node.js 实例耗尽内存之前生成至少一个、最多 max_count 个快照,当 max_count 大于 0 时。

【When generating snapshots, garbage collection may be triggered and bring the heap usage down. Therefore multiple snapshots may be written to disk before the Node.js instance finally runs out of memory. These heap snapshots can be compared to determine what objects are being allocated during the time consecutive snapshots are taken. It's not guaranteed that Node.js will write exactly max_count snapshots to disk, but it will try its best to generate at least one and up to max_count snapshots before the Node.js instance runs out of memory when max_count is greater than 0.】

生成 V8 快照需要时间和内存(包括 V8 堆管理的内存以及 V8 堆之外的本地内存)。堆越大,需要的资源就越多。Node.js 会调整 V8 堆以适应额外的 V8 堆内存开销,并尽量避免耗尽进程可用的所有内存。当进程使用的内存超过系统认为合适的范围时,根据系统配置,进程可能会被系统突然终止。

【Generating V8 snapshots takes time and memory (both memory managed by the V8 heap and native memory outside the V8 heap). The bigger the heap is, the more resources it needs. Node.js will adjust the V8 heap to accommodate the additional V8 heap memory overhead, and try its best to avoid using up all the memory available to the process. When the process uses more memory than the system deems appropriate, the process may be terminated abruptly by the system, depending on the system configuration.】

$ node --max-old-space-size=100 --heapsnapshot-near-heap-limit=3 index.js
Wrote snapshot to Heap.20200430.100036.49580.0.001.heapsnapshot
Wrote snapshot to Heap.20200430.100037.49580.0.002.heapsnapshot
Wrote snapshot to Heap.20200430.100038.49580.0.003.heapsnapshot

<--- Last few GCs --->

[49580:0x110000000]     4826 毫秒:标记-清除 130.6 (147.8) -> 130.5 (147.8) MB,27.4 / 0.0 毫秒(平均 mu = 0.126,当前 mu = 0.034)分配失败,清理可能不成功
[49580:0x110000000]     4845 毫秒:标记-清除 130.6 (147.8) -> 130.6 (147.8) MB,18.8 / 0.0 毫秒(平均 mu = 0.088,当前 mu = 0.031)分配失败,清理可能不成功



<--- JS stacktrace --->

致命错误:在堆边界附近的标记压缩无效,分配失败 - JavaScript 堆内存不足
....

【FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
....】