Keywords: NodeJS, memory leaks, node-inspector,chrome
Os:windows 10
This article describes how to use Node-inspector+chrome to find Nodejs memory leaks.
1. Create an express app, refer to http://www.cnblogs.com/ldlchina/p/4054974.html.
Modify the App.js content as follows:
//App.jsvarApp = require (' Express ')();varHTTP = require (' http '). Server (app);varLEAKOBJS = [];functionLeakclass () { This. x = 1;} App.get (‘/‘,function(req, res) {Console.log (' Get/'); for(vari = 0; i < 1000; i++) {Leakobjs.push (NewLeakclass ()); } res.send (' );}); Http.listen (3000,function() {Console.log (' Listening on Port 3000 ');});
Run cmd: "Node App.js"
Open the page in Chrome: Http://localhost:3000/. Constantly refresh to the page, Nodejs memory will continue to grow.
2. Install use Node-inspector to debug background programs. Please refer to http://www.cnblogs.com/ldlchina/p/3551277.html.
Run cmd: "Node--debug app.js".
Run cmd: "Node-inspector".
3. Open http://127.0.0.1:8080/debug?port=5858 in Chrome, such as:
4. Select the "Profiles" tab, select "Take Heap Snapshot" and click "Take Snapshot". Such as
5. Open a new chrome page and run Http://localhost:3000/.
6. Select the "Profiles" tab again, select "Take Heap Snapshot" and click "Take Snapshot".
Select comparison and you will see "Leakclass" causing the memory to grow, such as. So we can examine the part of the code about Leakclass, and experience tells us to avoid unlimited growth arrays, otherwise the memory will be consumed in the case of large traffic.
Memory Leak Locator Tool
There are now many useful and enhanced tools for locating memory leaks in node. JS applications. Here are some of them:
- Https://github.com/lloyd/node-memwatch. Note: If the "Node install Memwatch" installation fails, then try "node install Memwatch-next"
- Jimb Esser's Node-mtrace, it uses the GCC mtrace tool to analyze the use of the heap.
- Dave Pacheco's Node-heap-dump grabbed a snapshot of the V8 heap and serialized everything into a huge JSON file. It also contains some JavaScript tools for analyzing the results of a snapshot.
- Danny Coates's V8-profiler and Node-inspector provide a V8 parser bound to node and a debug interface based on WebKit WEB inspector.
For more information, please refer to:
Memory leak patterns in JavaScript.
The node. JS Profiling Guide that hasn ' t existed-finding the cause of a Memory Leak Using Heap snapshots.
Find Nodejs memory leaks using chrome+node-inspector