@@ -13,13 +13,13 @@ For the proposal of this guide, we will use a simple web server empowered with [
13131 - Create a new project
1414
1515``` bash
16- $ npm init -y
16+ npm init -y
1717```
1818
19192 - Install dependencies
2020
2121``` bash
22- $ npm i fastify
22+ npm i fastify
2323```
2424
25253 - Create a simple server (server.js)
@@ -34,7 +34,12 @@ server.get('/write', () => {
3434 const date = new Date ().toString ();
3535
3636 // don't do this at home
37- entries .add ({ date, arch: os .arch (), platform: os .platform (), cpus: os .cpus () });
37+ entries .add ({
38+ date,
39+ arch: os .arch (),
40+ platform: os .platform (),
41+ cpus: os .cpus ()
42+ });
3843
3944 return true ;
4045});
@@ -58,7 +63,7 @@ server.listen(9999, (err, address) => {
58634 - Run the application server
5964
6065``` bash
61- $ node --trace-gc server.js
66+ node --trace-gc server.js
6267```
6368
6469It should output something like:
@@ -131,8 +136,8 @@ The `--trace-gc` flag outputs all garbage collection events in the console. Firs
131136</table >
132137
133138We'll only focus on two events here:
134- - Scavenge
135- - Mark-sweep
139+ * Scavenge
140+ * Mark-sweep
136141
137142The heap is divided into "spaces." Amongst these, we have a space called the "new" space and another one called the "old" space.
138143
@@ -183,19 +188,19 @@ This algorithm is composed of two phases:
183188
184189Now we can come back to the output of the ` --trace-gc ` flag and see how we could interpret the console's output.
185190
186- - First, install (` autocannon ` )[ https://www.npmjs.com/package/autocannon ] :
191+ * First, install (` autocannon ` )[ https://www.npmjs.com/package/autocannon ] :
187192``` bash
188- $ npm i -g autocannon
193+ npm i -g autocannon
189194```
190195
191- - Then, restart the server:
196+ * Then, restart the server:
192197``` bash
193- $ node --trace-gc server.js
198+ node --trace-gc server.js
194199```
195200
196- - Open a new terminal and run the following command:
201+ * Open a new terminal and run the following command:
197202``` bash
198- $ autocannon http://localhost:9999/write
203+ autocannon http://localhost:9999/write
199204```
200205
201206Now, if you come back quickly to the previous terminal window: you'll see that there are a lot of ` Mark-sweep ` events in the console. We also see that the amount of memory collected after the event is insignificant.
@@ -213,7 +218,7 @@ An excellent way to inspect memory is to make heap dumps and compare them. So le
213218import v8 from ' v8' ;
214219
215220// then add:
216- server .get (' /heap-dump/:id' , (req ) => {
221+ server .get (' /heap-dump/:id' , (req ) => {
217222 const id = req .params .id ;
218223 return v8 .writeHeapSnapshot (` heap-dump-${ id} .heapsnapshot` );
219224});
@@ -224,27 +229,27 @@ We exposed an endpoint that will perform a heap dump for us. We'll use it to gen
2242291 . It's time to rerun our server (without any flag this time):
225230
226231``` bash
227- $ node server.js
232+ node server.js
228233```
229234
2302352 . Open a new terminal, and generate a first dump:
231236
232237``` bash
233- $ curl http://localhost:9999/heap-dump/start
238+ curl http://localhost:9999/heap-dump/start
234239```
235240
236241You should see a ` heap-dump-first.heapsnapshot ` file in your current directory.
237242
2382433 . Now run the test command:
239244
240245``` bash
241- $ autocannon http://localhost:9999/write
246+ autocannon http://localhost:9999/write
242247```
243248
2442494 . Once it is finished, generate a second dump:
245250
246251``` bash
247- $ curl http://localhost:9999/heap-dump/end
252+ curl http://localhost:9999/heap-dump/end
248253```
249254
250255You should see a ` heap-dump-end.heapsnapshot ` file in your current directory.
@@ -271,11 +276,11 @@ You should see a `heap-dump-end.heapsnapshot` file in your current directory.
271276 <img style =" text-align :center " src =" ../../../../static/guides/diagnosis-memory-leak/setup-cdt-3.png " alt =" setup-cdt-3 " width =" 400 " />
272277</div >
273278
274- * Click on the "end" snapshot and compare them:
275- - Click on "Objects allocated between heap-dump-start and heap-dump-end - It will help us see what's happened between our states (start and end).
276- - Sort object by "Retained size" (size freed if the object and dependents ones are deleted)
277- - Then you can expand the first line recursively until you find the most significant object
278- - Once you find it, click on it, and you'll be able to find the name in the description below
279+ * Click on the "end" snapshot and compare them:
280+ * Click on "Objects allocated between heap-dump-start and heap-dump-end - It will help us see what's happened between our states (start and end).
281+ * Sort object by "Retained size" (size freed if the object and dependents ones are deleted)
282+ * Then you can expand the first line recursively until you find the most significant object
283+ * Once you find it, click on it, and you'll be able to find the name in the description below
279284
280285<div align =" center " >
281286 <img style =" text-align :center " src =" ../../../../static/guides/diagnosis-memory-leak/setup-cdt-4.gif " alt =" setup-cdt-4 " />
@@ -295,6 +300,6 @@ What do you think about fixing this leak, making a new snapshot, and observing t
295300Hope you appreciate playing with diagnostic tools. In case, did you know that Node.Js documentation contains a list of tools for diagnostics purposes? Feel free to [ check it] ( https://github.com/nodejs/node/blob/master/doc/contributing/diagnostic-tooling-support-tiers.md ) out.
296301
297302## References:
298- - https://v8.dev/blog/trash-talk
299- - https://github.com/thlorenz/v8-perf/blob/master/gc.md
300- - https://developer.chrome.com/docs/devtools/memory-problems/memory-101/
303+ * https://v8.dev/blog/trash-talk
304+ * https://github.com/thlorenz/v8-perf/blob/master/gc.md
305+ * https://developer.chrome.com/docs/devtools/memory-problems/memory-101/
0 commit comments