1
- # Investigating memory leaks with valgrind
1
+ # Investigating memory leaks with Valgrind
2
2
3
3
A Node.js process may run out of memory due to excessive consumption of
4
4
native memory. Native Memory is memory which is not managed by the
5
5
V8 Garbage collector and is allocated either by the Node.js runtime, its
6
6
dependencies or native [ addons] ( https://nodejs.org/docs/latest/api/n-api.html ) .
7
7
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
9
9
issues on Linux platforms.
10
10
11
- ## valgrind
11
+ ## Valgrind
12
12
13
13
[ Valgrind] ( https://valgrind.org/docs/manual/quick-start.html ) is a
14
14
tool available on Linux distributions which can be used to investigate
15
15
memory usage including identifying memory leaks (memory which is
16
16
allocated and not freed) and other memory related problems
17
17
like double freeing memory.
18
18
19
- To use valgrind :
19
+ To use Valgrind :
20
20
21
- * Be patient, running under valgrind slows execution significantly
21
+ * Be patient, running under Valgrind slows execution significantly
22
22
due to the checks being performed.
23
23
* Reduce your test case to the smallest reproduce. Due to the slowdown it is
24
24
important to run the minimum test case in order to be able to do it in
@@ -35,7 +35,7 @@ apt-get install valgrind
35
35
36
36
## Invocation
37
37
38
- The simplest invocation of valgrind is:
38
+ The simplest invocation of Valgrind is:
39
39
40
40
``` console
41
41
valgrind node test.js
@@ -133,7 +133,7 @@ it along with the allocations (since it is not used)
133
133
and Valgrind will not find any leaks since they
134
134
will no longer exist in the code being run.
135
135
136
- Running valgrind on this code shows the following:
136
+ Running Valgrind on this code shows the following:
137
137
138
138
```console
139
139
user1@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:
317
317
318
318
What we can' t tell is where in the native addon the memory is being
319
319
allocated. 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
321
321
information.
322
322
323
323
# # Enabling debug symbols to get more information
@@ -345,7 +345,7 @@ npm install --debug
345
345
npm rebuild
346
346
` ` `
347
347
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
349
349
for the leaking location includes the name of the source file and the
350
350
line number:
351
351
@@ -385,7 +385,7 @@ This new output shows us exactly where the leak is occurring in the file `hello.
385
385
386
386
If the leak is not in an addon and is instead in the Node.js binary itself,
387
387
you 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:
389
389
390
390
` ` ` console
391
391
==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
409
409
gives you enough information to track down where the leak is, that' s great,
410
410
otherwise the next step is to compile a debug build of Node.js.
411
411
412
- To get additional information with valgrind :
412
+ To get additional information with Valgrind :
413
413
414
414
* Check out the Node.js source corresponding to the release that you
415
415
want to debug. For example:
@@ -432,7 +432,7 @@ make -j4
432
432
`./configure --debug`, two binaries will have been built when `make` was run.
433
433
You must use the one which is in `out/Debug`.
434
434
435
- Running valgrind using the debug build of Node.js shows:
435
+ Running Valgrind using the debug build of Node.js shows:
436
436
437
437
```console
438
438
==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:
450
450
451
451
Now we can see the specific file name and line in the Node.js code which
452
452
caused 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