What will you learn after reading this article
- V8 heap memory viewing, limiting, setting
- There are representations of different Node versions in the V8 heap
- V8 heap memory parameters
About the v8
About V8’s role in browsers and Node
V8 heap memory limits
The amount of memory used by javascript in Node applications is limited. Note that this refers to the heap memory. In V8, all JS objects are stored in the heap. In practice, accidentally touching this boundary will cause the process to exit. This is 1.4GB for a 64-bit system and 0.7GB for a 32-bit system. Node is built on the V8 engine, so V8 allocates and manages memory in its own way. So here’s the question:
- Why limit v8 heap memory?
- Why is the size of memory controlled at 1.4GB or 0.7GB?
V8 object allocation
Memory query
Node provides the process.memoryusage () method, which returns an object describing the memoryUsage(in Bytes) of the node.js process. To check memory usage, execute the following code:
$ node
> process.memoryUsage()
{
rss: 29196288.heapTotal: 5685248.heapUsed: 4549120.external: 926086.arrayBuffers: 10077
}
Copy the code
Explain these fields:
RSS: resident set size, which is the size of the physical memory allocated to the process (a subset of the total allocated memory), including all C++ and JavaScript objects and code.
HeapTotal: indicates the allocated heap memory size of V8.
HeapUsed: used size of heap memory.
External: represents the memory usage of C++ objects bound to Javascript objects managed by V8.
ArrayBuffers: Represents the memory allocated to arrayBuffers and SharedarrayBuffers, including all Node.js buffers.
In the real business, each variable object we declare is allocated in the heap, and if there is not enough free memory in the applied heap to allocate new objects, the heap will continue to be allocated until the v8 limit is exceeded.
So why does V8 limit the size of the heap? 🤔 ️
The reason is limited by v8’s garbage collection mechanism. In Node, garbage collection is done automatically by V8, and Node is single-threaded, so the JS thread blocks during collection. Taking 1.5 gigabytes of garbage collection heap memory as an example, v8 takes more than 50ms to do a small garbage collection. A non-incremental garbage collection can take more than a second, and this time loss can seriously affect program performance and responsiveness. Therefore, v8’s heap memory limit and default threshold for its size are understandable for performance reasons.
Default memory upper limit
As explained in Nodejs, it is about 1.4GB for 64-bit systems and 0.7GB for 32-bit systems. Create memorysie.js (memorysie.js);
const v8 = require('v8')
console.log('HeapStatistics:',v8.getHeapStatistics()) // Query the upper limit of heap memory
Copy the code
In Node 10.12.0, the following output is displayed:
HeapStatistics: { total_heap_size: 6537216.total_heap_size_executable: 1048576.total_physical_size: 5230576.total_available_size: 1520889432.used_heap_size: 3865464.heap_size_limit: 1526909922./ / 1.42 G
malloced_memory: 8192.peak_malloced_memory: 416320.does_zap_garbage: 0 }
Copy the code
In Node 12.14.1, the following output is displayed:
HeapStatistics: {
total_heap_size: 4472832.total_heap_size_executable: 524288.total_physical_size: 3317184.total_available_size: 2194939360.used_heap_size: 2277872.heap_size_limit: 2197815296./ / 2.04 G
malloced_memory: 8192.peak_malloced_memory: 127176.does_zap_garbage: 0.number_of_native_contexts: 1.number_of_detached_contexts: 0
}
Copy the code
In Node 15.14.0, the following information is displayed
HeapStatistics: {
total_heap_size: 4284416.total_heap_size_executable: 524288.total_physical_size: 3122176.total_available_size: 4342823264.used_heap_size: 3384776.heap_size_limit: 4345298944./ / 4.04 G
malloced_memory: 254120.peak_malloced_memory: 90736.does_zap_garbage: 0.number_of_native_contexts: 1.number_of_detached_contexts: 0
}
Copy the code
This means that the default Settings are different for different versions of the V8 heap as Node evolves.
Change the heap memory default value
V8’s default heap upper limit can be modified (in two ways) :
- Node can be passed when it starts
--max-old-space-size
或--max-new-space-size
Parameters. - If node8.x or later, it can still pass
NODE_OPTIONS
This system environment variable to configureexport NODE_OPTIONS=--max_old_space_size=2048
–max-old-space-size –max-new-space-size –max-new-space-size
- –max-old-space-size: set heap memory old generation space;
- –max-old-space-size: set the heap memory size for the new generation; (More on memory allocation and garbage collection below)
The above parameters take effect when V8 is initialized and cannot be changed dynamically.
Create memorysie.js for example:
const v8 = require('v8')
console.log('memoryUsage:',process.memoryUsage())
console.log('HeapStatistics:',v8.getHeapStatistics()) // Query the upper limit of heap memory
Copy the code
Start Node and set the maximum heap size to 5000MB:
$ node
$ node --max-old-space-size=5000 memorySize.js
Copy the code
Print the following:
V8.getheapstatistics () results in heap_size_limit 5365510286 bytes, or 5116 MB.
Because Node itself is in the stage of rapid development, a lot of information and the performance of the current Node there are partial differences, for rigor, query a lot of information, liver night! Everyone see officer understanding can leave a message to communicate, do not understand after watching a thumbs-up 😭.
Conclusion:
There’s a lot more to know about heap memory, but this article focuses on viewing and setting up V8 heap memory, and how it behaves in different versions of Node. Other parameters involved in the middle you can see for yourself. Welcome to communicate with us. The next section introduces memory allocation and reclamation modes.
Resources: Nodejs in Plain English, Community Practice