Common tools used to locate node memory leaks include:

  • v8-profiler
  • node-heapdump
  • node-memwatch

Before the DEMO, you can learn a little bit about the basics

Note about Node heap memory and off-heap memory.

  1. It is mainly heap memory that suffers from the V8 GC.
  2. Not all memory in Node is allocated by V8, as can be seen from the fact that heapUsed is always less than RSS. For memory that is not allocated by V8 we call it off-heap memory, such as buffers, which are C++ based and not V8(see the values for RSS and external in the demo below). External represents the memory usage of C++ objects bound to Javascript that are managed by V8.
  3. Therefore, it indicates that out-of-heap memory can break the memory limit problem.

Use Node-heapdump to eliminate memory leaks in the DEMO

The main demonstration here is node-heapdump. Node-memwatch has not been maintained for a long time.

Environmental installation

  • node.js
  • node-gyp
  • Xcode-select –install is required for MAC
  • MAC after install xcode sudo xcode – select – switch/Library/Developer/CommandLineTools /
  • node-heapdump

code


const heapdump = require('heapdump')
const http = require('http')

const leakArray = []
const leak = function() {
	leakArray.push(`leak: The ${Math.random()}`)
}

http.createServer(function(req, res) {
	// Access the Node service every time. Will add data to leakArray and will not be recycled.
	leak()
	res.writeHead(200, {'Content-Type': 'text/plain'})
	res.end('hello node')
}).listen(9999)

console.log('Server is running at: HTTP :127.0.0.1:9999/')
Copy the code

We then simulate user access by repeatedly accessing the server curl http://127.0.0.1:9999 at the end, at which point the leakArray array is growing and will not be recycled.

On UNIX platforms, you can force snapshots by sending SIGUSR2 signals to the server process.


$ kill -USR2 <pid>
Copy the code

Check the PID in MAC

$ lsof -i tcp:9999
Copy the code

A snapshot will be generated in your folder. The default file name is heapdump-.. Heapsnapshot.

Since Node relies on the V8 engine for JavaScript execution, as does Chrome, we can analyze these dumps with the help of the Memory module in Chrome developer Tools. First open Chrome’s developer tools, switch to Memory, and load the dump file in sequence.

Summary: Constructor, Distance, Shallow Size, and Retained Size. The Constructor column groups variables by class name; Distance indicates the Distance to the root object, and the smaller the Distance is, the closer the root object is. Shallow Size indicates the Size of the variable itself, excluding the Size of the variable it references. Retained Size includes not only its own Size, but also the Size of the referenced variable.

Retained Size (string) : Shallow Size (string) : Retained Size (string) : Retained Size (string) : Retained Size (string) : Retained Size (string) : Retained Size (string) : Retained Size (string) : Retained Size (string) : Retained Size

We look directly at the row farthest from distance(20).

Then let’s look at Leak () in detail.

When you take a closer look at leakArray, you discover that a large number of leak strings are stored there, which have never been retrieved.

The resources

  • Node.js is easy to understand

PS: Interested can pay attention to the public number.