In-depth introduction to Node.js-memory management

In-depth introduction to Node.js-memory management

"A bit of awe-inspiring spirit, a thousand miles away!" **

Everyone is welcome to pay attention to my public account "front-end hours", articles are sent synchronously!

Insert picture description here

01 Preface

With the development of Node, the operation of JavaScript is no longer limited to running in the browser. The application of Node on the server makes more and more problems appear. For developers who are new to JavaScript, they rarely think of memory allocation or memory leaks. In order to keep up with the trend, explain how Node uses memory reasonably and efficiently to prevent memory leaks.

02 V8 garbage collection and memory limitation

JavaScript programming does not need to care about the allocation and release of memory, because it has a garbage collection mechanism to manage. But our deeper understanding of memory management is very little. How is memory allocated and how does garbage collection work?

01 Node YV8

Let's first understand what is the relationship between the two. First of all, V8 is a browser engine developed by Google. Its performance is very excellent, making it possible to write server programs in JavaScript. The origin of V8 was developed by virtual machine expert Lars Bak. The previous work of developers has been focused on the development of high-performance virtual machines.

Node is a JavaScript runtime platform built on Chrome. From this, Node's high performance is like holding the thigh of V8, and you can enjoy better performance as V8 upgrades.

02 V8 memory limit

Node has restrictions on using memory through JavaScript, and Node cannot directly manipulate large memory objects. It is about 1.4GB under 64-bit system, and about 0.7GB under 32-bit system. The reason is that Node is built on V8, and its memory allocation and management are controlled by V8. Although there is no problem with this allocation mechanism under the browser, there are problems with Node.

03 Object assignment of V8

In V8, all JavaScript objects are allocated through the heap, and the amount of memory space occupied by the objects is uncertain.
Insert picture description here

When we write some variables in the code, we will apply to the memory space in the heap. We said above that V8 has a memory allocation limit, which is due to the impact of V8's garbage collection mechanism. Let's imagine that if there is 1.5GB of garbage waiting to be collected, and the V8 garbage collection mechanism needs to run every 50 milliseconds or more, during this time, the program will not run.

If it is said that I have to turn on this restriction, we can change the parameters when Node starts:

node --max-old-space-size=1700   xxx.js    //单位是MB
node --max-new-space-size=1024  xxx.js    //单位是KB

The meaning of the above code is the change of the Cenozoic and Cenozoic space, and later I will explain what the Cenozoic and Cenozoic space is.

04 V8 garbage collection mechanism

In V8, we know that the memory space can be divided into new generation and old generation. The Cenozoic space mainly stores some objects with short survival time, while the Cenozoic space mainly stores some objects with long survival time.

I said that 64-bit systems can only use about 1.4GB, 32-bit systems can only use 0.7GB. This space contains the space of the young and old generations, and the old generation has more space than the new generation.
Insert picture description here

Rank 32 64Place
Cenozoic 16MB 32MB
Mesozoic 700MB 1400MB

V8 mainly uses two different algorithms, acting on the young and old generations, because the relationship between the two is different. There are mainly class algorithms, the first is the Scavenge algorithm, and the second is Mark-Sweep & Mark-Compact. The characteristics of the new and old generations are as follows:

New generation: fewer surviving objects

Old generation: more surviving objects

①Scavenge algorithm

  • Divide the new generation space into two, from space and to space
  • The memory is allocated to the From space first, and garbage collection checks the survival of the From space
  • Move the surviving object from the From space to the To space and release the From space
  • Swap From and To spaces

This completes a cleanup process for garbage collection.
Insert picture description here

②Mark-Sweep algorithm

  • Traverse all the objects in the heap, mark the alive objects
  • Clear unmarked objects
    Insert picture description here

Because there are many surviving objects in the old generation space, the traversal time will be relatively long when using the Scavenge algorithm, and half of the space will be wasted to store objects, which is relatively inefficient. In general, the Scavenge algorithm copies live objects, while the Mark-Sweep algorithm clears dead objects.

③Mark-Compact algorithm

We see that the above algorithm generates fragmented memory space after one recovery . If a large memory object comes in at this time, the allocation cannot be completed, and a new round of garbage collection mechanism is triggered in advance. Therefore, based on this problem, on the basis of the original algorithm, the compression method is adopted, and during the process of marking the live object, the object will move to the side. Then clear the boundary memory directly after the marking is completed.
Insert picture description here

④Incremental Marking algorithm

Unlike the previous three, this algorithm ** adopts a "stepping" method **, because the garbage collection mechanism runs once, and for the garbage collection mechanism running time is too long, then the page The application logic must stop and wait, this impact is also relatively large.

There is no need to worry about this problem for the young generation, because the young generation has fewer surviving objects and the memory footprint is relatively small. However, for the old generation, there are more and larger objects , and the impact of the pause is large, so it is necessary to make the application logic less affected by garbage collection.

Therefore, the “stepping” method is adopted, and the garbage collection mechanism and logic code ** segmented execution ** are used to alleviate the problem that the application logic cannot be executed due to the long pause time.
Insert picture description here

03 Summary

Node extends the main application of JavaScript to the server side, so the details we consider are also different from browsers. It is more about the allocation of memory resources. A little carelessness may write some memory leaks, making The garbage collection mechanism cannot clean up and release memory, which may seriously cause the server to crash.

So we usually have to be more careful when writing code, do not write global variables or frequently use closures, these will also cause memory leaks if the memory is not properly released.

Reference article:

  • A simple explanation of Node.js
    Insert picture description here
Published 57 original articles · won praise 6 · views 6419

Guess you like

Origin blog.csdn.net/weixin_42724176/article/details/104811148