Go deep into NodeJS-memory control

Source: Internet
Author: User

Go deep into NodeJS-memory control

A Node service based on non-blocking and event-driven features low memory consumption and is suitable for handling massive network requests.

V8's garbage collection mechanism and memory restrictions

Similar to Java, Javascript is implemented by the Garbage Collector for automatic memory management. Node is built on the V8 virtual machine, so its memory collection is closely related to the V8 operating mechanism.

V8 memory limit: The 64-bit system is about 1.4 GB, and the 32-bit system is about 0.7 GB.

Process. memoryUsage (). The returned values include heapTotal, heapUsed, heapUsed, and resident set size.

V8 garbage collection mechanism

V8 adopts the generational garbage collection mechanism. The heap memory structure is as follows, which is divided into the new generation and the old generation. The corresponding size can be set through parameters, but once set, it cannot be automatically expanded based on usage.

JavaScript has the following scopes: function scope, with scope, and global scope.

The identifier search will first start from the current scope. If it is not found, it will search for the upper-level scope.

View process memory usage: process. memoryUsage ()

View system memory usage: OS. totalmem () and OS. freemem (), total system memory and idle memory, in bytes

Off-heap memory: Buffer, etc.

Memory leakage

The essence of Memory leakage is that the objects to be recycled are accidentally not recycled and become resident objects in the old generation.

Memory leakage is usually caused

(1) Cache

Javascript objects are in the key-value format and can be used as cache, but there are many defects and problems due to the lack of efficient elimination mechanism.

Due to the caching mechanism of the module, the module is resident in the old generation. During the module design, the memory leakage is very careful.

Solution: the cache outside the process is not stored by the process itself, such as Redis and memcached.

(2) Delayed queue consumption

(3) Scope not released

Memory leakage exclusion Tool

Node-heapdump

Node-memwatch

Large memory usage

The stream module provided by Node is used to process large files. It can be divided into two types: readable and writable. Most modules in Node have stream applications, for example:

Fs. createReadStream (), fs. createWriteStream () can avoid the failure to operate large files through fs. readFile () or fs. writeFile () due to V8 memory restrictions.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.