|
| 1 | +--- |
| 2 | +title: Memory Diagnostics - Using GC Trace |
| 3 | +layout: docs.hbs |
| 4 | +--- |
| 5 | + |
| 6 | +# 追踪垃圾回收 |
| 7 | + |
| 8 | +垃圾回收是如何工作的,实在有太多的东西需要学习。但有一点必须清楚:那便是当 GC 运行 |
| 9 | +的时候,你的代码是不工作的。 |
| 10 | + |
| 11 | +或许你想知道垃圾回收运行的频率,以及需要运行多久,以及结果是什么。 |
| 12 | + |
| 13 | +## 如何运行垃圾回收追踪 |
| 14 | + |
| 15 | +你可以借助 `--trace_gc` 在控制台输出中看到垃圾回收追踪的信息情况。 |
| 16 | + |
| 17 | +```console |
| 18 | +$ node --trace_gc app.js |
| 19 | +``` |
| 20 | + |
| 21 | +或许你不想追踪运行在服务器上的整个进程生命周期里的那些信息,如果是这样的话,请从进程 |
| 22 | +内部设定把它关闭,这样就不会再追踪了。 |
| 23 | + |
| 24 | +以下是如何追踪并打印持续一分钟的 GC 信息示例: |
| 25 | + |
| 26 | +```js |
| 27 | +const v8 = require('v8'); |
| 28 | +v8.setFlagsFromString('--trace_gc'); |
| 29 | +setTimeout(() => { v8.setFlagsFromString('--notrace_gc'); }, 60e3); |
| 30 | +``` |
| 31 | + |
| 32 | +### 使用 `--trace_gc` 检查追踪 |
| 33 | + |
| 34 | +你得到的垃圾收集器追踪信息看上去如以下样子: |
| 35 | + |
| 36 | +``` |
| 37 | +[19278:0x5408db0] 44 ms: Scavenge 2.3 (3.0) -> 1.9 (4.0) MB, 1.2 / 0.0 ms (average mu = 1.000, current mu = 1.000) allocation failure |
| 38 | +
|
| 39 | +[23521:0x10268b000] 120 ms: Mark-sweep 100.7 (122.7) -> 100.6 (122.7) MB, 0.15 / 0.0 ms (average mu = 0.132, current mu = 0.137) deserialize GC in old space requested |
| 40 | +``` |
| 41 | + |
| 42 | +这里给出如何解析这些信息(以第二行数据举例): |
| 43 | + |
| 44 | +<table> |
| 45 | + <tr> |
| 46 | + <th>得到的数据</th> |
| 47 | + <th>对应解析含义</th> |
| 48 | + </tr> |
| 49 | + <tr> |
| 50 | + <td>23521</td> |
| 51 | + <td>正在运行的进程号</td> |
| 52 | + </tr> |
| 53 | + <tr> |
| 54 | + <td>0x10268db0</td> |
| 55 | + <td>独立内存地址 (JS 堆实例)</td> |
| 56 | + </tr> |
| 57 | + <tr> |
| 58 | + <td>120</td> |
| 59 | + <td>自开始运行的时间(毫秒)</td> |
| 60 | + </tr> |
| 61 | + <tr> |
| 62 | + <td>Mark-sweep</td> |
| 63 | + <td>类型 / GC 阶段</td> |
| 64 | + </tr> |
| 65 | + <tr> |
| 66 | + <td>100.7</td> |
| 67 | + <td>GC 运行前占有内存(MB)</td> |
| 68 | + </tr> |
| 69 | + <tr> |
| 70 | + <td>122.7</td> |
| 71 | + <td>GC 运行前总占有内存(MB)</td> |
| 72 | + </tr> |
| 73 | + <tr> |
| 74 | + <td>100.6</td> |
| 75 | + <td>GC 运行后占有内存(MB)</td> |
| 76 | + </tr> |
| 77 | + <tr> |
| 78 | + <td>122.7</td> |
| 79 | + <td>GC 运行后总占有内存(MB)</td> |
| 80 | + </tr> |
| 81 | + <tr> |
| 82 | + <td>0.15 / 0.0 <br/> |
| 83 | + (average mu = 0.132, current mu = 0.137)</td> |
| 84 | + <td>GC 所耗费时间(毫秒)</td> |
| 85 | + </tr> |
| 86 | + <tr> |
| 87 | + <td>deserialize GC in old space requested</td> |
| 88 | + <td>GC 原因</td> |
| 89 | + </tr> |
| 90 | +</table> |
| 91 | + |
| 92 | +## 使用“performance hooks” 追踪你的垃圾回收信息 |
| 93 | + |
| 94 | +在 Node.js,你可以使用[performance hooks][] 来跟踪你的垃圾回收信息。 |
| 95 | + |
| 96 | +```js |
| 97 | +const { PerformanceObserver } = require('perf_hooks'); |
| 98 | + |
| 99 | +// Create a performance observer |
| 100 | +const obs = new PerformanceObserver((list) => { |
| 101 | + const entry = list.getEntries()[0]; |
| 102 | + /* |
| 103 | + The entry would be an instance of PerformanceEntry containing |
| 104 | + metrics of garbage collection. |
| 105 | + For example: |
| 106 | + PerformanceEntry { |
| 107 | + name: 'gc', |
| 108 | + entryType: 'gc', |
| 109 | + startTime: 2820.567669, |
| 110 | + duration: 1.315709, |
| 111 | + kind: 1 |
| 112 | + } |
| 113 | + */ |
| 114 | +}); |
| 115 | + |
| 116 | +// Subscribe notifications of GCs |
| 117 | +obs.observe({ entryTypes: ['gc'] }); |
| 118 | + |
| 119 | +// Stop subscription |
| 120 | +obs.disconnect(); |
| 121 | +``` |
| 122 | + |
| 123 | +### 借助“performance hooks”检查追踪信息 |
| 124 | + |
| 125 | +你可以在 [PerformanceObserver][] 的回调函数里得到诸如 [PerformanceEntry][] 的 GC |
| 126 | +数据。举例说明: |
| 127 | + |
| 128 | +```ts |
| 129 | +PerformanceEntry { |
| 130 | + name: 'gc', |
| 131 | + entryType: 'gc', |
| 132 | + startTime: 2820.567669, |
| 133 | + duration: 1.315709, |
| 134 | + kind: 1 |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +<table> |
| 139 | + <tr> |
| 140 | + <th>属性名称</th> |
| 141 | + <th>对应解释</th> |
| 142 | + </tr> |
| 143 | + <tr> |
| 144 | + <td>name</td> |
| 145 | + <td>进程名称。</td> |
| 146 | + </tr> |
| 147 | + <tr> |
| 148 | + <td>entryType</td> |
| 149 | + <td>类型。</td> |
| 150 | + </tr> |
| 151 | + <tr> |
| 152 | + <td>startTime</td> |
| 153 | + <td>进程的启动时间(高精度毫秒表示)。</td> |
| 154 | + </tr> |
| 155 | + <tr> |
| 156 | + <td>duration</td> |
| 157 | + <td>持续运行时间(毫秒)。</td> |
| 158 | + </tr> |
| 159 | + <tr> |
| 160 | + <td>kind</td> |
| 161 | + <td>垃圾回收的类型。</td> |
| 162 | + </tr> |
| 163 | + <tr> |
| 164 | + <td>flags</td> |
| 165 | + <td>垃圾回收的其余信息。</td> |
| 166 | + </tr> |
| 167 | +</table> |
| 168 | + |
| 169 | +欲知更多详情,请查阅 |
| 170 | +[performance hooks API文档][performance hooks]. |
| 171 | + |
| 172 | +## 使用追踪选项诊断内存问题的示例: |
| 173 | + |
| 174 | +A. 如何获取糟糕的内存分配的上下文信息? |
| 175 | +1. 假定我们观察到旧内存持续增长。 |
| 176 | +2. 但根据 GC 的负重来看,堆的最大值并未达到,但进程慢。 |
| 177 | +3. 回看跟踪的数据,找出在 GC 前后总的堆占用量。 |
| 178 | +4. 使用 `--max-old-space-size` 降低内存,使得总的内存堆更接近于极限值。 |
| 179 | +5. 再次运行程序,达到内存耗尽。 |
| 180 | +6. 该过程的日志将显示失败的上下文信息。 |
| 181 | + |
| 182 | +B. 如何确定在堆增长之时,存在内存泄露现象? |
| 183 | +1. 假定我们观察到旧内存持续增长。 |
| 184 | +2. 但根据 GC 的负重来看,堆的最大值并未达到,但进程慢。 |
| 185 | +3. 回看跟踪的数据,找出在 GC 前后总的堆占用量。 |
| 186 | +4. 使用 `--max-old-space-size` 降低内存,使得总的内存堆更接近于极限值。 |
| 187 | +5. 再次运行程序,观察是否内存耗尽。 |
| 188 | +6. 如果发生内存耗尽,尝试每次提升 10% 的堆内存,反复数次。 |
| 189 | +如果之前的现象复现被观察到,这就能证明存在内存泄露。 |
| 190 | +7. 如果不存在内存耗尽,就把内存堆固定在那个值——紧凑的堆减少了内存占用以及对内存压缩的延迟。 |
| 191 | + |
| 192 | +C. 如何断定是否存在太多次的垃圾回收,或者因为太多次垃圾回收造成一定的开销? |
| 193 | +1. 回顾跟踪数据,尤其关注持续不断的 GC 发生时间隔的一系列数据。 |
| 194 | +2. 回顾跟踪数据,尤其关注持续不断的 GC 发生时时间消耗的数据。 |
| 195 | +3. 如果两次 GC 间隙时间小于 GC 所话费的时间,证明程序正处于严重缺乏内存。 |
| 196 | +4. 如果两次 GC 间隙时间和所话费的时间都非常高,证明该程序应该用一个更小点的堆。 |
| 197 | +5. 如果两次 GC 的时间远大于 GC 运行的时间,应用程序则相对比较健康。 |
| 198 | + |
| 199 | +[performance hooks]: https://nodejs.org/api/perf_hooks.html |
| 200 | +[PerformanceEntry]: https://nodejs.org/api/perf_hooks.html#perf_hooks_class_performanceentry |
| 201 | +[PerformanceObserver]: https://nodejs.org/api/perf_hooks.html#perf_hooks_class_performanceobserver |
0 commit comments