使用堆分析器

¥Using Heap Profiler

堆分析器在 V8 之上运行,以捕获随时间推移的分配。在本文档中,我们将介绍使用以下方法进行内存分析:

¥The heap profiler acts on top of V8 to capture allocations over time. In this document, we will cover memory profiling using:

  1. 分配时间表

    ¥Allocation Timeline

  2. 采样堆分析器

    ¥Sampling Heap Profiler

使用堆快照 指南中介绍的堆转储不同,使用实时分析的想法是了解一段时间内的分配。

¥Unlike heap dumps which were covered in the Using Heap Snapshot guide, the idea of using real-time profiling is to understand allocations over a period of time.

堆分析器 - 分配时间表

¥Heap Profiler - Allocation Timeline

Heap Profiler 与 Sampling Heap Profiler 类似,不同之处在于它会跟踪每个分配。它的开销比采样堆分析器更高,因此不建议在生产中使用。

¥Heap Profiler is similar to the Sampling Heap Profiler, except it will trace every allocation. It has higher overhead than the Sampling Heap Profiler so it’s not recommended to use in production.

你可以使用 @mmarchini/observe 以编程方式启动和停止分析器。

¥You can use @mmarchini/observe to start and stop the profiler programmatically.

如何

¥How To

启动应用:

¥Start the application:

node --inspect index.js

--inspect-brk 是脚本的更好选择。

¥--inspect-brk is a better choice for scripts.

连接到 chrome 中的 dev-tools 实例,然后:

¥Connect to the dev-tools instance in chrome and then:

  • 选择 Memory 选项卡。

    ¥Select the Memory tab.

  • 选择 Allocation instrumentation timeline

    ¥Select Allocation instrumentation timeline.

  • 开始分析。

    ¥Start profiling.

heap profiler tutorial step 1

一旦堆分析开始运行,强烈建议运行样本以识别内存问题。例如,如果我们对 Web 应用进行堆分析,我们可以使用 Apache Benchmark 来产生负载:

¥Once the heap profiling is running, it is strongly recommended to run samples in order to identify memory issues. For example, if we were heap profiling a web application, we could use Apache Benchmark to produce load:

$ ab -n 1000 -c 5 http://localhost:3000

然后,在加载完成后按停止按钮:

¥Then, press stop button when the load is complete:

heap profiler tutorial step 2

最后,查看快照数据:

¥Finally, look at the snapshot data:

heap profiler tutorial step 3

查看 有用的链接 部分以获取有关内存术语的更多信息。

¥Check the useful links section for further information about memory terminology.

采样堆分析器

¥Sampling Heap Profiler

采样堆分析器会随时间跟踪内存分配模式和保留空间。由于它是基于采样的,因此其开销足够低,可以在生产系统中使用。

¥Sampling Heap Profiler tracks the memory allocation pattern and reserved space over time. Since it is sampling based its overhead is low enough to use in production systems.

你可以使用模块 heap-profiler 以编程方式启动和停止堆分析器。

¥You can use the module heap-profiler to start and stop the heap profiler programmatically.

如何

¥How To

启动应用:

¥Start the application:

$ node --inspect index.js

--inspect-brk 是脚本的更好选择。

¥--inspect-brk is an better choice for scripts.

连接到 dev-tools 实例,然后:

¥Connect to the dev-tools instance and then:

  1. 选择 Memory 选项卡。

    ¥Select the Memory tab.

  2. 选择 Allocation sampling

    ¥Select Allocation sampling.

  3. 开始分析。

    ¥Start profiling.

heap profiler tutorial 4

产生一些负载并停止分析器。它将根据它们的堆栈跟踪生成带有分配的摘要。你可以关注具有更多堆分配的函数,请参见以下示例:

¥Produce some load and stop the profiler. It will generate a summary with allocation based on their stacktraces. You can focus on the functions with more heap allocations, see the example below:

heap profiler tutorial 5

有用的链接

¥Useful Links