The Dump type
- Core dump: process snapshot
- Heap dump: Heap memory snapshot
- Thread dump: Thread snapshot,Node is temporarily unavailable
See the link at the end of this article for more information
The Dump function
- Core dump: analyzes CPU problems, for example, the CPU stack overflow usage reaches or approaches 100%, or programs exit abnormally
- Heap dump: analyzes memory problems, such as memory leaks and large objects
The Dump to prepare
- Install llnode
npm install -g llnode
Copy the code
- Install the chrome
Dump analysis example
Heap dump analysis
- Simulate memory leaks and analyze heapdump to find the object
- Sample a.n ode. Js
// Simulate memory leaks, analyze heapdump, and find the object
require('heapdump');
const leaks = [];
function LeakingClass(){
this.name = Math.random().toString(36);
this.age = Math.floor(Math.random() * 100);
this.arr = new Array(10000);
}
setInterval(() = >{
for (let i = 0; i < 100; i++) {
leaks.push(new LeakingClass)
}
console.warn('Leaks: %d', leaks.length);
},1000);
// # test heapdump
// node.node.js # console run the program
/ / ps aux | grep a.n ode. Js # query process ID
// kill -31 [PID] # generate heapdump
Copy the code
Run the above program in a browser, you can passChrome Dev Tools > Performance monitor
Tools to visualize memory leaks
useChrome Dev Tools > Memory
The tool analyzes the generated Heapdump to find the large objects causing the memory leak
Core dump
- Simulate high CPU usage and analyze core dump to find program fragments that take time to execute
- Sample b.n ode. Js
// Analyze core dump to find program fragments that take time to execute
// In general, for Web applications, an infinite loop is the most likely to fill the CPU
const http = require('http')
const hostname = '127.0.0.1'
const port = 3000
const server = http.createServer((req, res) = > {
while(true) {// The CPU is fully loaded after the request is entered
}
res.statusCode = 200
res.setHeader('Content-Type'.'text/plain')
res.end('Hello world \n')
})
server.listen(port, () = > {
console.log(The server runs in http://${hostname}:${port}/ `)})// # test coredump
// node.node.js
Ulimit -c unlimited # Enable coredump
/ / ps aux | grep b.n ode. Js # query process ID
// sudo gcore [PID
// # Mac OSX coredump file can be saved at /cores
// llnode node -c path/to/core-dump-file #
Copy the code
After starting the program and accessing the URL, you can see that the CPU is fully loaded.Coredump analysis allows us to locate the problematic JS file and method — the location of the dead-loop.
Core dump
- Simulate program abnormal exit, analyze core dump to find program abnormal point
- The sample from the ode. Js
// Analyze core dump to find the exception point -- the exit position of the program
setTimeout(function delayedFailure(){
throw new Error('Fail not really fast');
}, 500);
// # test coredump
// ulimit -c unlimited # Enable coredump generation
// node --abort-on-uncaught-exception c. nodo.js # console runs the node program
// llnode node -c path/to/core-dump-file #
// go to the /cores directory and find the coredump file named core.
/// Abort -on-uncaught-exception is added when node is started
Copy the code
After starting the program, you can find the exit location of the program by analyzing coredump
prompt
If you are a Mac and already have Xcode, you don’t need to install LLVM. The domestic network environment and compilation process can not be completed in three hours. You can directly use LLNode to conduct Coredump analysis
conclusion
This paper demonstrates the process of Node dump analysis, which focuses on hands-on practice and lays a certain foundation for production service obstacle removal. Relevant fundamental knowledge can be found in the official instructions.
reference
- Node crime scene Revealed — Coredump restore line exception
- Node.js debug llNode
- git-llnode
- Chrome-memory analysis help documentation
- About the Heap dump
- Linux under the core dump
- Performance analysis – JAVA Thread Dump analysis
- Node uses flame map to optimize CPU surge