From 2d562a9a9e907687003fdef00a90d4bda681e7d3 Mon Sep 17 00:00:00 2001 From: Rafael Gonzaga Date: Sun, 10 Oct 2021 22:14:18 -0300 Subject: [PATCH 1/4] docs: add heap profiling tutorial --- .../memory/step1/using_heap_profiler.md | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/documentation/memory/step1/using_heap_profiler.md b/documentation/memory/step1/using_heap_profiler.md index 4492c5c..bafd469 100644 --- a/documentation/memory/step1/using_heap_profiler.md +++ b/documentation/memory/step1/using_heap_profiler.md @@ -1,13 +1,39 @@ # Using Heap Profiler -## Heap Profiler +## Heap Profiler - Allocation Timeline Heap Profiler is similar to the Sampling Heap Profiler, except it will track every allocation. It has higher overhead than the Sampling Heap Profiler so it’s not recommended to use in production. ### How To -// TODO +Start the application: + +```console +node --inspect index.js +``` + +> `--inspect-brk` is an better choice for scripts. + +Connect to the dev-tools instance and then: + +- Select `memory` tab +- Select `Allocation instrumentation timeline` +- Start profiling + +![image](https://user-images.githubusercontent.com/26234614/136712329-ac9fc581-af2b-4a94-8849-b959ebea0a59.png) + +After it, the heap profiling is running, it is strongly recommended to run samples in order to identify memory issues, for this example, we will use `Apache Benchmark` to produce load in the application. + +> In this example, we are assuming the heap profiling under web application. + +```console +ab -n 1000 -c 5 http://localhost:3000 +``` + +Hence, press stop button when the load expected is complete + +![image](https://user-images.githubusercontent.com/26234614/136714198-867632e0-2417-4336-9e6c-828fcf5be6b7.png) ## Sampling Heap Profiler @@ -18,7 +44,11 @@ sampling based it has a low enough overhead to use it in production systems. // TODO +## Reading Profiling + +// TODO + ## Useful Links -- https://developers.google.com/web/tools/chrome-devtools/memory-problems/allocation-profiler -_ https://github.com/v8/sampling-heap-profiler \ No newline at end of file +- https://github.com/v8/sampling-heap-profiler +- https://developer.chrome.com/docs/devtools/memory-problems/allocation-profiler/ From db5a3de6a537d36f10a4e13e55ed160b9f5bb167 Mon Sep 17 00:00:00 2001 From: Rafael Gonzaga Date: Mon, 11 Oct 2021 17:45:25 -0300 Subject: [PATCH 2/4] Update using_heap_profiler.md --- .../memory/step1/using_heap_profiler.md | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/documentation/memory/step1/using_heap_profiler.md b/documentation/memory/step1/using_heap_profiler.md index bafd469..522ea63 100644 --- a/documentation/memory/step1/using_heap_profiler.md +++ b/documentation/memory/step1/using_heap_profiler.md @@ -35,6 +35,12 @@ Hence, press stop button when the load expected is complete ![image](https://user-images.githubusercontent.com/26234614/136714198-867632e0-2417-4336-9e6c-828fcf5be6b7.png) +Then look at the snapshot data towards to memory allocation. + +![image](https://user-images.githubusercontent.com/26234614/136846720-65bf7073-eddc-4afd-9753-e21ef75e0243.png) + +Check the [usefull links](#usefull-links) section for futher information about memory terminology. + ## Sampling Heap Profiler Sampling Heap Profiler tracks memory allocation pattern and reserved space over time. As it’s @@ -42,13 +48,28 @@ sampling based it has a low enough overhead to use it in production systems. ### How To -// TODO +Start the application: + +```console +node --inspect index.js +``` + +> `--inspect-brk` is an better choice for scripts. + +Connect to the dev-tools instance and then: + +- Select `memory` tab +- Select `Allocation sampling` +- Start profiling + +![image](https://user-images.githubusercontent.com/26234614/136847038-1cb6dfd4-26d4-4e2a-8dd1-8d74c2151360.png) -## Reading Profiling +Produce some load and stop the profiler. It will generate a summary with allocation based in the stacktrace, you can lookup to the functions with more heap allocations in a timespan, see the example below: -// TODO +![image](https://user-images.githubusercontent.com/26234614/136849337-1dd4c46e-b479-48a8-a995-422bb3f17f56.png) ## Useful Links +- https://developer.chrome.com/docs/devtools/memory-problems/memory-101/ - https://github.com/v8/sampling-heap-profiler - https://developer.chrome.com/docs/devtools/memory-problems/allocation-profiler/ From 3a7405ec9d53d55d88fa8efd5a53c41ee767875d Mon Sep 17 00:00:00 2001 From: Rafael Gonzaga Date: Thu, 14 Oct 2021 15:32:15 -0300 Subject: [PATCH 3/4] docs: add introduction to heap profling --- documentation/memory/step1/using_heap_profiler.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/documentation/memory/step1/using_heap_profiler.md b/documentation/memory/step1/using_heap_profiler.md index 522ea63..1f80e60 100644 --- a/documentation/memory/step1/using_heap_profiler.md +++ b/documentation/memory/step1/using_heap_profiler.md @@ -1,5 +1,14 @@ # Using Heap Profiler +To debug a memory issue we need to be able to see how much space our specific type of objects take, and what variables retain them to get garbage collected. For the effective debugging we also need to know the allocation pattern of our variables over time. + +The heap profiler acts on top of V8 towards to bring snapshots of memory over time. In this document, we will cover the memory profiling using: + +1. Allocation Timeline +2. Sampling Heap Profiler + +Unlike heap dump that was cover in the [step 2](../step2/using_heap_snapshot.md), the idea of using real-time profiling is to understand allocations in a given time frame. + ## Heap Profiler - Allocation Timeline Heap Profiler is similar to the Sampling Heap Profiler, except it will track every allocation. It has From 24cf835e1c322c69c67776d171542c5a4942e537 Mon Sep 17 00:00:00 2001 From: Rafael Gonzaga Date: Tue, 19 Oct 2021 09:22:15 -0300 Subject: [PATCH 4/4] docs: add references to external modules with APIs --- documentation/memory/step1/using_heap_profiler.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/documentation/memory/step1/using_heap_profiler.md b/documentation/memory/step1/using_heap_profiler.md index 1f80e60..7aa6ebf 100644 --- a/documentation/memory/step1/using_heap_profiler.md +++ b/documentation/memory/step1/using_heap_profiler.md @@ -14,6 +14,8 @@ Unlike heap dump that was cover in the [step 2](../step2/using_heap_snapshot.md) Heap Profiler is similar to the Sampling Heap Profiler, except it will track every allocation. It has higher overhead than the Sampling Heap Profiler so it’s not recommended to use in production. +> You can use [@mmarchini/observe](https://www.npmjs.com/package/@mmarchini/observe) to do it programmatically. + ### How To Start the application: @@ -55,6 +57,8 @@ Check the [usefull links](#usefull-links) section for futher information about m Sampling Heap Profiler tracks memory allocation pattern and reserved space over time. As it’s sampling based it has a low enough overhead to use it in production systems. +> You can use the module [`heap-profiler`](https://www.npmjs.com/package/heap-profile) to do it programmatically. + ### How To Start the application: