Pm2 monit shows heap usage is almost 100%. Is this ok? - node.js

I was watching pm2 monit.
My server process Mem coloured red.
I've checked my metrix
I need help understanding heap usage..
Almost 100%?
While heap size is only 30Mib ?
I allowed 2GB of memory to be used when running node.js.
Also, I checked the memory resources of the server, there was enough free memory.
What does this Usage mean?
And why is my mem painted red?.

Related

NodeJs - RSS and external memory increases significantly, heap memory increases slowly. What can be the causes?

In my NodeJS application:
1.Is it correct that RSS and external memory increases significantly, but heap memory increases slowly?
Is it correct that the external memory increases more than the heapTotal?
I tried using the chrome inspect, but as soon as i launch the snapshots the application crashes.
Also, i don't think analyzing the heap memory i find the problem.
see the graph here
Any suggestions?

Node process memory usage exceeds --max_old_space_size

I'm having trouble limiting a node processes memory usage.
I am running an application on AWS and the servers crash when the memory usage exceeds what is available. I have tried using the --max_old_space_size flag but the process seems to still use more memory than the value I specify in the flag and the server crashes.
I'm fine with the process failing I just really need the server not to crash.
I know that the flag is working because if I specify --max_old_space_size=1 the node script is killed immediately.
My question is, are there other ways to limit the memory usage of a node process (and its sub processes?) I'm running on ubuntu and have heard cgroups could maybe achieve this? Or am I doing something wrong? Are there other flags for node processes that would limit overall memory usage?

Does it mean memory leaks in node.js?

I am using node.js 0.10.18 (on Amazon EC2) as our ios game http server.
I use process.memoryUsage() to print memory usage.
I find the memory usages of our nodes are abnormal.
After running for two days:
node on machine1:
2014-10-13T02:35:04.782Z - vital: Process: heapTotal 119.70 MB heapUsed 84.62 MB rss 441.57 MB
node on machine2:
2014-10-13T02:36:01.057Z - vital: Process: heapTotal 744.72 MB heapUsed 108.19 MB rss 1045.53 MB
The results are:
Both the heapUsages are very small, it has nothing to do with how long the node process runs.
The heapTotal on machine2 is much larger than heapUsed, and it will never get small until I restart the process. But heapTotal on machine1 seems normal.
machine1 is Amazon EC2 m3.xlarge, machine2 is Amazon EC2 m3.medium. From Amazon CloudWatch I know that the performance of machine2 is insufficient, sometimes the CPU usage of machine2 goes to 100%. So does the abnormal heapTotal usage have something to do with the insufficiency of the hardware? The 100% cpu usage is not a result of our node processes because using the node-usage module I see that the cpu usage of our node process is never higher than 50%. I think the usage is stolen by the neighboring virtual machines (you know there is shared cpu time on Amazon EC2s).
I know that the buffer memory usage = (rss - heapTotal). I find that the buffer memory usage on both machines will increase gradually. You see, both of the buffer memory usages are more than 300MB after running for two days.
My questions are:
Why heapTotal usage will not get released even if the heapUsed is very small? Is it a problem with node itself or else some bug of my own code? Is the only way to fix it to upgrade the hardware?
Why is the buffer usage increasing gradually? Does it mean there are memory leaks? Is it a problem with node itself or else some bug of my own code? Or just ignore it?
Thanks!
From this post https://www.joyent.com/blog/walmart-node-js-memory-leak. I found that there was a memory leak bug in the version <= 0.10.21. And the bug was fixed in version 0.10.22
I upgraded node to the latest version 0.10.32 and also enhanced the hardware of machine2.
Both the two memory problems never appear again. The memory use of both will only increase by several MBs each day, and I think it is normal, because my node process will cache some player data.
So maybe the two problems are caused by the same reason and it has been fixed.

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...
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
Here is a quick fix, a node.js lib that will restart the any node process once it reaches a certain size.
https://github.com/DoryZi/memory_limiter
Set the --max_old_space_size CLI flag to control the maximum heap size. There's a post that describes Running a node.js app in a low-memory environment
tl;dr; Try setting this value, in megabytes, to about 80% of the maximum memory footprint you want node to try to remain under. e.g. to run app.js and keep it under 500MB RAM used
node --max_old_space_size=400 app.js
This setting is also described in the Node JS CLI documentation

Memory leak in Node.js outside of heap?

I've just fixed a memory leak in my node application which was in the heap of node.
I've profiled that with Google's Profiler and managed to fix the memory leak.
Now my app is running again for some time, and I've seen that the heap size is pretty constant. No memory leak anymore. But when I check my server's free RAM, I see a decrease...
When I restart my node server the RAM is up to it's normal free RAM.
Now I've heard about that Node.js can save objects and stuff outside of the heap. I think that is what's causing the memory leak here.
How can I see what's taking up the memory? Can't really profile anything, or can I?
I'm using:
node.js: v0.8.18 and
socket.io: v0.9.13
Some other node modules that I'm using are: nodetime, heapdump (will delete this, though), jquery, crypto, request and querystring.
Some graphs:
Free OS memory and
Node RSS and Heap used

Resources