Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimize Node.js memory consumption

I'm writing a simple cms in Node.js, Express and MongoDB. I'm planning to run a different Node.js process for every site. The problem is that after startup the process takes about 90m of RAM and for me it's too big (eight site take all server RAM). This memory is taken after the first connection to the site and other connections don't affect the memory.

Is there a guideline or a list of "best practices" to optimize this memory usage? I'm trying to track where the memory is allocated with process.memoryUsage() or a similar function but it's not simple to do this.

Is not a problem of memory leaks or something similar because the memory usage doesn't grow up after the first connection, so probably the optimization could be in loading less modules or do something differently...

like image 989
Tiziano Rossi Avatar asked Feb 11 '14 06:02

Tiziano Rossi


People also ask

How much RAM does node js need?

At least 2GB of RAM. At least 4 vCPUs.

How do I increase my Node memory limit?

If you want to increase the max memory for Node you can use --max_old_space_size option. You should set this with NODE_OPTIONS environment variable.

How do you prevent memory leaks in node JS?

Avoid Accidental Globals This could be the result of a typo and could lead to a memory leak. Another way could be when assigning a variable to this within a function in the global scope. To avoid issues like this, always write JavaScript in strict mode using the 'use strict'; annotation at the top of your JS file.


1 Answers

The links below may help you to understand and detect memory leaks (if they do exist):

Debugging memory leaks in node.js

Detecting Memory Leaks in Node.js Applications

Tracking Down Memory Leaks in Node.js

These SO questions may also be useful:

How to monitor the memory usage of Node.js?

Node.js Memory Leak Hunting

like image 50
Leonel Machava Avatar answered Oct 11 '22 21:10

Leonel Machava