This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

V8 Memory Allocation

This article follows NodeJs: Memory Control (Part 1)

When a variable is declared in code and assigned a value, the memory of the object used is allocated in the heap. If there is not enough free heap memory allocated for new objects, heap memory will continue to be allocated until the heap size exceeds the V8 limit.

Node memoryUsage: this command returns an object describing the memoryUsage (in bytes) of the node.js process, which may vary from computer to computer for the same project.

// Check the available memory size
let mU = process.memoryUsage();
console.log('Memory size :');
console.log(mU);
Copy the code

Description of each memory:

The property name instructions
rss Resident set size is the amount of space occupied by a process on the main memory device (that is, a subset of the total allocated memory), including allC++JavaScriptObjects and code.
heapTotal V8The amount of memory that has been applied for
heapUsed V8The amount of memory that is currently used
external Bound to theV8The management ofJavaScriptThe object’sC++Object memory usage.
arrayBuffers forArrayBufferSharedArrayBufferAllocated memory, including allNode.js Buffer. This is also included inexternalIn the value. whenNode.jsWhen used as an embedded library, this value may be0Because in this case there might not be a traceArrayBufferThe distribution of

Tip: Type in your browser’s consolewindow.performanceCommand can also view memory

There are three values, which are:

jsHeapSizeLimit: 2172649472
totalJSHeapSize: 25233728
usedJSHeapSize: 22200444
Copy the code
  • jsHeapSizeLimitRepresents the memory size limit, 2172649472/1024/1024 ≈ 2072M, i.e. 2G, which also confirms that the new V8 has raised the memory limit from 1.4GB.
  • totalJSHeapSizeRepresents available memory
  • usedJSHeapSizeIs the memory used by JavaScript objects and cannot be greater thantotalJSHeapSizeIf yes, a memory leak may occur

Memory mainly stores variables and other data:

  • Local variables disappear when the program ends execution with no references
  • The global object will always survive until the end of the program

Manual memory allocation

Next, let’s try manual memory allocation using –max-old-space-size

Start by writing simple JavaScript code that increases the amount of code memory.

const os = require('os');
function getMemory() {
    let memory = process.memoryUsage();

    // console.log(' system total memory: ${(totalmem/1024/1024).tofixed (1)}MB ');
    console.log('Apply to memory:${(memory.heapTotal/ 1024 /1024).toFixed(1)}MB`);
    
    console.log('Used memory:${(memory.heapUsed/ 1024 /1024).toFixed(1)}MB`);
    console.log('-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --')}// Test memory out of code
let count = 0;

// A large array is accepted each time
let useMem = function(){
    let size = 20 * 1024 * 1024;
    let arr = new Array(size);
    return arr;
}
Copy the code

Next, write the runtime method:

// Global variables
let total = [];
for (let j = 0; j < 10; j++) {
    getMemory();
    total.push(useMem());
}
console.log('success');
Copy the code

The above loop 10 times will not exceed, and success can be normally output

If it loops 15 times, it will exceed, and the garbage collection trace log appears below

The Last few GCs shows the Last few garbage collections, and the JS stacktrace represents the JavaScript stacktrace.

The JavaScript heap is out of memory. The JavaScript heap is out of memory

In this case, you can use the –max-old-space-size command to increase the size of the heap. Generally speaking, it is not necessary to increase the size of the heap in the new generation.

–max-old-space-size=2698 –max-old-space-size=2698 –max-old-space-size=2698

Node-max-old-space-size =2698.\ getmemory.js

The configuration of this command can also be added to the package.json of the project. You can run the Node command using YARN or NPM

Note: When allocating the memory, pay attention to the free memory of the system and cannot exceed the free memory of the system. And generally only 75% of free memory can be accepted. You can view this by importing the OS.

const os = require('os');
let totalmem = os.totalmem();  // Returns the total amount of system memory in bytes as an integer
let freemem = os.freemem();  // Returns the amount of free system memory, in bytes, as an integer.
console.log('Total system memory:${ (totalmem/ 1024 /1024).toFixed(1)}, system free memory:${ (freemem/ 1024 /1024).toFixed(1)}`);
Copy the code



View the garbage collection log

The main way to view the garbage collection log is to add the — trace_GC parameter at startup. When garbage collection occurs, garbage collection logs are printed from the standard output

Change it in the node run command above: node –trace_gc getMemory.js

In this case, garbage collection logs are displayed on the terminal. By analyzing garbage collection logs, you can know the running status of garbage collection and find out which phases are time-consuming.


Log file is generated by adding the > gc.log command. Command: node –trace_gc getmemory.js > gc.log

After the command is run, the terminal will not display logs, but garbage collection logs will appear in the folder, so that we can view more convenient.


Information on garbage collection GC can also be viewed by generating V8 parser output with –prof: node –prof getmemory.js. Of course, the v8. Log generated at this point is not readable





For this v8.log log file, you can use the –prof-process command to achieve similar results to the Linux-tick-processor tool.

Generate the command: node –prof-process v8.log > processed. TXT

This command generates an analysis TXT file:

I’m not going to follow the path of Node.js, which uses V8linux-tick-processorTool V8 is also providedlinux-tick-processorThe log statistics tool is located in the source directory of Nodedeps/v8/toolsCan be found below, address:Github.com/nodejs/node… Clone the Node source locally: in the source directorydeps/v8/toolsfindlinux-tick-processorIn Windows, the corresponding command file iswindows-tick-processor.bat, add the directory to the environment variable Path and you can call it directly.