memory

Reasons to focus on memory:

  • Prevent pages occupied too much content, resulting in client lag, Shenzhen no response
  • Node uses V8, and the backend is prone to memory overruns due to server persistence

Size of memory

  • The memory size of the 64-bit operating system is 1.4g, 64MB for the new generation of 64-bit, and 1400MB for the old generation
  • The 32-bit operating system is 0.7GB, the 32-bit new generation is 16MB, and the old generation is 700MBWhy doesn’t the memory expand? First of all, the characteristics of the front end is non-persistence, execute once all recycle, generally 1.4g is enough, secondly, JS will be suspended when the memory recycle, which will lead to the lag, the larger the memory, the longer the pause.

The meaning and relationship between Cenozoic and old generation?

The new generation is simply understood to be copying, while the old generation is to delete and arrange marks.

The new generation is used to store newly generated variables, and the old generation is used to put the variables of the new generation into the old generation after meeting certain conditions. (More on the conditions later)

Small and new variables are first placed in the FROM space, and recycled once. The surviving variables are placed in the TO space, and then the FROM space is emptied. When the collection occurs again, the FROM and to functions are switched.

Why divide from and to? It’s a trade of space for time.

In the old generation, those marked black need to be deleted. After recycling, memory needs to be organized into contiguous, so that it is easy to use. (For example arrays must have contiguous memory space)What conditions should the new generation be promoted to the old generation?

There are two ways:

  1. Variables that survive two cycles of Cenozoic recycling can be put into the old generation
  2. If the memory usage is too high, or 25% of the To space is already used (8MB on 64-bit systems), the old generation will be directly entered.

How do I check memory

Method 1: Enter a value on the browser consolewindow.performanceOr to summarize using the Memory browser:orMethod 2: It can pass in Node environmentprocess.messoryUsage()Viewing Memory

External is additional memory, which is C++ memory. Since node is written in C++ and has the right to allocate C++ memory, it can be expanded, but only in node environment.

Method three: Through THE JS code

function getMe({

    var men = process.memoryUsage()

    return men

}

console.log(getMe())

Copy the code

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() 

Copy the code

Variable processing

Memory is basically local variables that store data such as variables that can be cleaned up when the program finishes execution and there are no references, but it is not immediately recycled and needs to wait. Global objects are always run until the end of the program and can be freed by assigning a global variable to undefined or null.

Memory optimization Tips

  1. Do not define global variables, destroy them immediately (assign undefined or NULL)
  2. Use anonymous self-executing functions(function(){})()
  3. Try to avoid references to heap closures

Preventing memory leaks

  1. Preventing memory abuse
  2. Pay attention to large memory operations

The cache is usually global, because to keep the program alive during execution, it is recommended that a lock be placed before the V8 cache. This is to determine the size of the array to store data. If the size reaches the limit, shift the data out, delete the previous data in a first-in, first-out fashion, and store the later data in a push fashion.

Fs.readfile () is used for node large file processing. CreateReadStream ().pipe(write) js is used for uploading large files to slice.

The 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.

The above is a summary note after a learning video on V8 engine memory reclamation