Out of memory issue in a Node.js app based on Docker
Problem
- When running a node.js app based on Docker, the server encountered an
out of memory
issue while it was running.
- For the v8 engine of node.js, the limit of heap memory is set by default. It has the following limit capacity of heap memory.
- Below v12: 1.35GB
- Below v14: 2GB
- v14 and above: 4GB
- The app continued to use memory and the service died because we didn't know about this memory limit.
- The instance of this service had 12GB of RAM, and when a lot of memory was being used, 4GB was used, leaving more than 7GB of free space.
- We didn't fully understand the structure of the service, resulting in memory waste and reduced availability of the service.
Solution
- When running node, add
--max-old-space-size=8192 start.js
to increase the maximum heap of the program.
- For Docker, add the environment variable
NODE_OPTIONS=--max-old-space-size=8192
.
- By adding the above options, the maximum heap of the program increases, improving the availability of the service.