node. JS Memory Leak analysis

Source: Internet
Author: User

In geek education, a video was published on "node. JS memory leak Analysis," This article focuses on how to handle node. JS memory anomaly issues from the content. If you wish to study, you can go to Geek College:
Keywords in this article
- Memory Leaks
- Memory Leak detection
- GC Analysis
- Memwatch

Article overview

Because memory leaks are very common in node. js, you may not be particularly sensitive to memory leaks when you apply JavaScript in a browser, but you will have to consider these issues when running as a server language. Because a small logic may cause the server to run for a day or one weeks or even one months to let you find that memory is rising, and finally to the day you have to restart the service to protect the performance of the server, then this problem is necessary before the launch of a system detection, At the same time, on-line can have an effective monitoring procedures to ensure the safety of operation.

What is a memory leak

Before we introduce the node. js memory leak, we should first know what a memory leak is, and what kind of memory leaks it contains.

Memory Leak Concepts

Memory leaks, also known as "storage leaks", use dynamic storage allocation functions, dynamically open spaces that are not released after use, resulting in the memory unit being occupied until the end of the program.
The above definition from the Baidu Encyclopedia, of course, from the definition above we can learn that the memory leak simply said, is occupied by the system resources and has not been released, resulting in less system resources, resulting in a more serious system anomalies, you can use the following to explain the memory leak.
The node. JS Service is assumed to be a "class sweep," and the system memory resource is assumed to be the class's resource "Five Broom", and the "student" that uses the resources to work, here we assume the process. The school to carry out a big cleaning, each class only five broom, everyone needs to complete a part of the sweeping work, the students to complete the automatic to other people, when everyone completed sweeping work, the end of the sweep, the teacher will be the first broom assigned to five people, but there are a few students in the five who do not work, even if done, The broom will not be assigned to other people, causing others to sweep the work has been unable to go on, or because the broom limited caused the cleaning work is very long.

Memory leak type

Memory leaks include the following types: Normal, occasional, one-time, implicit.

    • Often-made sex
      The code that occurs in memory leaks is executed multiple times, causing a memory leak each time it is executed.

    • Sporadic nature
      Code that occurs with a memory leak occurs only under certain circumstances or during operation. The occurrence and the incidental sex are opposite. For a given environment, the occasional may become a frequent occurrence.

    • Disposable
      The code that occurs with a memory leak is only executed once, or because of an algorithmic flaw, there is always a block and only one piece of memory leaks. For example, allocating memory in the class's constructor does not release the memory in the destructor, so a memory leak occurs only once.

    • Implicit
      It is mainly in the Call function or module, when the parameter or input does not reach the defined value, there will be no leakage, when the parameters or input value reached a certain time, only to find the memory leak, we call this implicit. The program keeps allocating memory while it is running, but it does not release memory until the end. Strictly speaking, there is no memory leak, because the final program frees up all the requested memory. However, for a server program that needs to run for several days, weeks, or months, not releasing memory in a timely manner can result in the eventual exhaustion of all of the system's memory. So, we call this kind of memory leak as an implicit memory leak. Implicit is what we need to explore in this article, to find and solve the problem of anomalies.

The damage caused by node. JS Memory leaks

What are the dangers of node. js memory leaks, and since we want to discover and detect memory leaks, then we have to first know what the node. js memory leak will affect.

User Service exception

In general, the user is unaware of the impact of a memory leak, but in some cases, because a memory leak can cause a user to respond slowly, in which case the user is not able to feel the exception, but can generally feel the service response is slow, and this situation may lead to the loss of new registered users and other issues.

Server Performance exception

In general, the direct impact of memory leakage is the server, the server will be due to the continuous increase in memory, so that system resources can be used more and more space, this will slowly cause the service to affect the other basic services in the server running, resulting in slower servers, while resulting in less resources to use, Direct results in server resource exhaustion, server exceptions, data loss and so on, more serious problems.

Common node. JS Memory leak issues

Here are two main types of code logic for memory leaks, mainly the memory leaks caused by circular references and uncontrolled loops.

Circular references

This part may be more common in JavaScript, which is mainly about saying that the A object contains a pointer to B, and object B also contains a reference to a. This can result in large amounts of memory not being recycled (memory leaks), because their references can never be 0, so a and B are not recycled as a recovery mechanism.

varfunction () {}varfunction () {}el.func = func;func.element = el;

For example, El and Func reference each other in the above code. And this type of memory leak can be said to be regular.

Uncontrolled circulation

There are no restrictions on the array, and there is no effective recycling mechanism when the arrays are too large.

private methods and properties in a module
In any module file that is written, a string is added to the header and tail to form a closure, which is then called once during require, and the exports object is stored in memory until the process exits. This is strictly not a memory leak, but it requires careful attention to the use of private variables and methods in the module, to avoid excessive private variables taking up too much system memory.
Suppose we have a module leak.js

var leakArray = [];   function () {    leakArray.push("leak"Math.random());  };

So if we create a test.js to reference the module, the runtime will see that the leakarray is always getting bigger.

varModrequire(‘./leak‘);Mod.leak();Mod.leak();Mod.leak();Mod.leak();Mod.leak();

If we call the method leak here, it can cause the array to be too large, which leads to some problems.

over-Large Array loops
Let's look at the following code:

fori0i100000000i{    var user       = {};    user.name  ‘outmem‘;    user.pass  ‘123456‘;    user.‘outmem[@outmem](/user/outmem).com‘;}

The main reason for this code is that the loop is too large and the direct memory is allocated to more than the V8 memory limit. Because of the execution mechanism of the JavaScript event loop, this code has no chance to enter the next event loop. Use SetInterval and settimeout to enter the next loop. But it is not recommended to use SetInterval and settimeout. For cycle Code, it is best to split it, then process it, and process it in segments. Because it doesn't make good use of one cycle at a time. One event loop, not more than 10ms.

node. JS Memory Leak tool usage practices

Here are some of the most common node. js memory leak detection tools, and the Memwatch and Heapdump for detailed hands-on learning.

node. JS Memory Leak Tool

Node-inspector provides a V8 parser bound to node and a debug interface based on WebKit Web inspector, and you can look at this blog post, which shows you how to apply the tool to detect memory leaks
Http://www.cnblogs.com/ldlchina/p/4762036.html

Node-mtrace, which uses the GCC mtrace tool to analyze the use of the heap.
Https://github.com/Jimbly/node-mtrace
But the tool provides a point-to-point, if there is an asynchronous function, it will be more cumbersome, this can be used in conjunction with other tools will be more convenient, you can see the GitHub sample code

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. Here in Memwatch we will apply the tool's corresponding functionality to locate the leak code logic.

Memwatch
is a tool designed to detect and monitor memory leaks, not only for scanning before they go online, but also for effective memory leak detection on-line.
Next, we practice applying memwatch to detect memory leaks and to perform GC memory analysis practices through HEAPDUMP fetching GC.

The practice of Memwatch

Before learning Memwatch, the first need to install the corresponding module, the specific operation can use NPM install Memwatch, download the module need to compile, so need to python2.6 above and need VS2012 above. If you have problems with the above configuration, you can find a link in my profile about how to solve these two issues http://blog.csdn.net/dan_blog/article/details/50707278
So let's start with the application of the module, let's take a look at the simple example code:

var  http = require  ( ' http ' ); var  server = http.createserver ( function   (req, res)  { for  (var  i= 0 ; I<1000 ; i++) {Server.on (, function  leakyfunc   ()  {}); } res.end ( ' Hello world\n ' );}). Listen (1337 ,  ' 127.0.0.1 ' );  

From the code itself to see that there is a certain problem, as the user every request, its memory consumption will be provided, we stop the request for a period of time, its memory will not be reduced, indicating that the code has some problems.
You can go to the Geek College video address to download the code source, which also includes other information
You can run the next code, if you are using http://127.0.0.1:1337 to access it continuously, if you can view the process memory usage in the process under Windows

If you are on Linux, you can first view the process ID by command and then use the TOP-P process ID

-ef|-p12202

Anytime you view the memory occupied by the process, you will see changes in its memory, while discovering that its memory is not slowly released back.
Since there is a memory leak above, we use Memwatch and heapdump to do the detection and analysis, the improved code is as follows.

varHTTP =require(' http ');varMemwatch =require(' Memwatch ');varHD =NewMemwatch. Heapdiff ();varHeapdump =require(' Heapdump ');varServer = Http.createserver ( function (req, res) {     for(varI=0; i< +; i++) {Server.on (' request ', function leakyfunc() {}); } res.end (' Hello world\n ');}). Listen1337,' 127.0.0.1 '); Memwatch.on (' leak ', function(info) {    vardiff = hd.end (); Console.log (JSON. Stringify (diff));varFile ='/tmp/myapp-'+ Process.pid +'-'+Date. Now () +'. Heapsnapshot '; Heapdump.writesnapshot (file, function(err){        if(err) Console.error (err);ElseConsole.error (' wrote snapshot: '+ file); });}); Server.setmaxlisteners (0); Console.log (' Server running at http://127.0.0.1:1337/. Process PID: ', process.pid);

The above logic contains the use of Memwatch to detect memory leaks, but also contains the use of Heapdump to capture the memory of the real-time situation, by running the code, and then using the pressure measurement tool to the http://127.0.0.1:1337, when the pressure measurement to a certain situation, In the Run window you can see a reminder of its memory leaks, and at this point it will be in the file directory (if it is best to modify the file path in Windows, where the sample code is relative to the Linux environment) note the current memory leak when the memory GC situation.

GC Memory Analysis

Now that we get to the file, we'll use Google Chrome's tools to analyze the GC's memory and see why it's causing the memory leak.

After you open the tool, use load to load the file that we just generated.
After loading in the analysis method, you can refer to the article
Http://itindex.net/detail/52929-chrome-%E5%BC%80%E5%8F%91-%E5%B7%A5%E5%85%B7
There are details of the introduction, of course, if you want to know more specific analysis methods, you can go to the Geek College to view the course of video learning materials.

Summarize

This is the knowledge described in this article, after reading this article, everyone at least understand what is a memory leak, What problems can be caused by memory leaks in node. js and how to apply Memwatch and heapdump to detect and analyze memory leaks, and to have a simple understanding of the use of memory analysis tools in chrome.

node. JS Memory Leak analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.