1- # Investigating memory leaks with valgrind
1+ # Investigating memory leaks with Valgrind
22
33A Node.js process may run out of memory due to excessive consumption of
44native memory. Native Memory is memory which is not managed by the
55V8 Garbage collector and is allocated either by the Node.js runtime, its
66dependencies or native [ addons] ( https://nodejs.org/docs/latest/api/n-api.html ) .
77
8- This guide provides information on how to use valgrind to investigate these
8+ This guide provides information on how to use Valgrind to investigate these
99issues on Linux platforms.
1010
11- ## valgrind
11+ ## Valgrind
1212
1313[ Valgrind] ( https://valgrind.org/docs/manual/quick-start.html ) is a
1414tool available on Linux distributions which can be used to investigate
1515memory usage including identifying memory leaks (memory which is
1616allocated and not freed) and other memory related problems
1717like double freeing memory.
1818
19- To use valgrind :
19+ To use Valgrind :
2020
21- * Be patient, running under valgrind slows execution significantly
21+ * Be patient, running under Valgrind slows execution significantly
2222 due to the checks being performed.
2323* Reduce your test case to the smallest reproduce. Due to the slowdown it is
2424 important to run the minimum test case in order to be able to do it in
@@ -35,7 +35,7 @@ apt-get install valgrind
3535
3636## Invocation
3737
38- The simplest invocation of valgrind is:
38+ The simplest invocation of Valgrind is:
3939
4040``` console
4141valgrind node test.js
@@ -133,7 +133,7 @@ it along with the allocations (since it is not used)
133133and Valgrind will not find any leaks since they
134134will no longer exist in the code being run.
135135
136- Running valgrind on this code shows the following:
136+ Running Valgrind on this code shows the following:
137137
138138```console
139139user1@minikube1:~/valgrind/node-addon-examples/1_hello_world/napi$ valgrind node hello.js
@@ -317,7 +317,7 @@ From the stack trace we can tell that the leak came from a native addon:
317317
318318What we can' t tell is where in the native addon the memory is being
319319allocated. This is because by default the addon is compiled without
320- the debug symbols which valgrind needs to be able to provide more
320+ the debug symbols which Valgrind needs to be able to provide more
321321information.
322322
323323# # Enabling debug symbols to get more information
@@ -345,7 +345,7 @@ npm install --debug
345345npm rebuild
346346` ` `
347347
348- The next step is to run valgrind after the rebuild. This time the information
348+ The next step is to run Valgrind after the rebuild. This time the information
349349for the leaking location includes the name of the source file and the
350350line number:
351351
@@ -385,7 +385,7 @@ This new output shows us exactly where the leak is occurring in the file `hello.
385385
386386If the leak is not in an addon and is instead in the Node.js binary itself,
387387you may need to compile node yourself and turn on debug symbols. Looking at
388- this entry reported by valgrind , with a release binary we see:
388+ this entry reported by Valgrind , with a release binary we see:
389389
390390` ` ` console
391391 ==4174== 304 bytes in 1 blocks are possibly lost in loss record 27 of 35
@@ -409,7 +409,7 @@ its symbols using `-rdynamic` so that they can be used by addons. If the stack
409409gives you enough information to track down where the leak is, that' s great,
410410otherwise the next step is to compile a debug build of Node.js.
411411
412- To get additional information with valgrind :
412+ To get additional information with Valgrind :
413413
414414* Check out the Node.js source corresponding to the release that you
415415 want to debug. For example:
@@ -432,7 +432,7 @@ make -j4
432432 `./configure --debug`, two binaries will have been built when `make` was run.
433433 You must use the one which is in `out/Debug`.
434434
435- Running valgrind using the debug build of Node.js shows:
435+ Running Valgrind using the debug build of Node.js shows:
436436
437437```console
438438==44112== 592 bytes in 1 blocks are possibly lost in loss record 26 of 27
@@ -450,3 +450,6 @@ Running valgrind using the debug build of Node.js shows:
450450
451451Now we can see the specific file name and line in the Node.js code which
452452caused the allocation (inspector\_agent.cc:140).
453+
454+ We can examine that line (and its surrounding code) to
455+ find a solution for the memory leak.
0 commit comments