How V8 engine reclaims memory and how to optimize

RAM

Reasons to pay attention to memory:

  • To prevent the page from occupying too much content, causing the client to freeze, Shenzhen does not respond
  • Node uses v8. Due to the persistence of the server, the backend can easily cause memory overflow

Memory size

  • The operating system is 64-bit memory size is 1.4G, 64-bit new generation space is 64MB, the old generation is 1400MB
  • The operating system is 0.7G for 32-bit, the new generation space of 32-bit is 16MB, and the old generation is 700MB.
    Insert picture description here
    Why not expand the memory?
    First of all, the characteristic of the front end is that it is not persistent, and it is fully recycled once it is executed. Generally speaking, 1.4G is enough. Secondly, JS will suspend execution when the memory is recycled. This will cause a pause.

What is the meaning and relationship of the new generation and the old generation?

The simple understanding of the new generation is to copy, and the old generation is to delete and organize.

The young generation is used to store the newly generated variables. The old generation is the new generation. The variables of the new generation will be placed in the old generation only when they meet certain conditions. (The conditions will be discussed later)

The small and new variables are first placed in the from space for a recovery, and the variables that survive after recovery are placed in the to space, and then all of the from are emptied; when the recovery occurs again, the from and to functions are swapped.

Why divide from and to?
This is to sacrifice space in exchange for time.

In the old generation, the ones marked in black need to be deleted, and after recycling, the memory needs to be organized into continuous ones for easy use. (For example, the array must have continuous memory space)
Insert picture description hereWhat are the requirements for the promotion of the new generation to the old generation?

There are two methods:

  1. Variables that have been recovered twice after being recovered from the young generation can be placed in the old generation
  2. The memory occupied is too large, or To has used 25% of the space (64-bit system is 8MB), then it will directly enter the old generation.
    Insert picture description here

How to check the memory

Method 1:
Enter window.performanceor summarize using the Memory
browser in the browser console :
Insert picture description hereor
Insert picture description hereMethod 2:
You can process.messoryUsage()check the memory status in the Node environment
Insert picture description here

external is extra memory, which is C ++ memory. Since node is written in C ++, it has the power to allocate C ++ memory, so it can be expanded, but only in Node environment.

Method three: through js code

function getMe() {
    var men = process.memoryUsage()
    return men
}
console.log(getMe())

Converting to MB format is more intuitive

function getMe() {
    var men = process.memoryUsage();
    var format = function(bytes){
        return (bytes/1024/1024).toFixed(2)+"MB"
    }
    console.log(format(men.heapTotal))
}
getMe() 

Variable handling

The memory is mainly the
local variables that store variables and other data. When the program execution is finished and there is no reference, it can be cleaned up, but it is not immediately recovered and needs to wait.
Global objects will always run to the end of the program, and can be released by assigning undefined or null to global variables.

Memory optimization tips

  1. Try not to define global variables, and destroy them in time (assigned undefined or null)
  2. Anonymous self-executing function(function(){})()
  3. Try to avoid references to heap closures

Prevent memory leaks

  1. Prevent memory abuse
  2. Pay attention to the operation of large memory

The cache is usually global, because to ensure that the program is alive during the operation, it is recommended to add a lock before the V8 cache, that is, to determine the length of the array of stored data, if the length has reached the limit, shift the data out, according to the first-in first-out Way to delete previous data, push to store later data.

Large memory operations: for example, file processing
node small files can use fs.readFile ()
node large file processing can use createReadStream (). Pipe (write)
js can use the slice upload method to upload large files (slice)

Final summary

To learn how the V8 engine reclaims memory, you need to understand how the V8 engine allocates memory, understand variable handling, memory optimization techniques, and prevent memory leaks.

Insert picture description here

The above content is a summary note after a learning video about V8 engine memory recovery

Published 128 original articles · 52 praises · 20,000+ views

Guess you like

Origin blog.csdn.net/weixin_44523860/article/details/105351255