nodejs application memory consumption management - node.js

I have a nodejs application which receives and processes the REST API calls.
And I found out that its memory usage continually grow and never drop down.
Is that because my application creates obj and never recycle it in a loop?
Is there any VALGRIND equivalent tool for nodejs application?
How can I detect the memory leak on nodejs app?

One way to detect a memory leak would be to use Node's --inspect flag. You could also use memwatch and headdump modules for a more detailed analysis. This link is a great resource to start of. This one has a more in-depth explanation on what is actually happening under the hood.

Related

How to collect memory dump from meteor/nodejs application?

I'd like to learn to analyze my meteor and node services' performance and memory usage better than just trying to log various thing to console. I've read couple articles about memory management in Node and some baby steps about analyzing the memory dumps with Chrome developer tools.
The question is, how do I get those memory dumps from my apps in the first place?
This memory and performance analysis is done on the server side service. As far as I know, the memory dumps got from Chrome browser are client side memory dumps.
It seems that this node package
https://github.com/bnoordhuis/node-heapdump
can be used to collect heapdumps on server side. Still need to figure out how to properly use it and then time to analyze those dumps.
At the moment I am just writing a single heapdump every time I start my app, but maybe more sophisticated writing method is needed to actually get something done.

Is there a way to dump all the lived objects in heap memory in Node js?

As title, I have encountered a memory leak problem in loopback framework on Node js.
I can't find any problem from request API call.
So I wonder that is there any way I can dump all the objects and variables in heap memory in NodeJs when the memory usage is constantly arising, so that I can find any clue in my code.
Thanks.
Firstly, if you are running a node process in a memory restricted environment, you have to make sure that you restrict the amount of memory that is allocated to Node and to V8. What may seem like a memory leak could just be a lazy garbage collection process by the V8 engine. To supervise memory usage, I recommend the npm add-on memwatch-next.
You can force V8 to perform a garbage collection by executing your node js program with in the following manner: node --expose-gc test.js
Now, within the code, you are able to call global.gc() at set time intervals when you'd like V8 to perform an old-space cleanup.
Additional information can be found here: https://simonmcmanus.wordpress.com/2013/01/03/forcing-garbage-collection-with-node-js-and-v8/

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

How to monitor memory usage on heroku (node.js)

There is New Relic for Rails, but I could not find anything available for node.js. Is there any tool or an API to monitor memory usage of heroku dynos?
This may not be the full solution you're looking for, but you can get the current memory usage of a Node.js process via process.memoryUsage().
Use node-usage, additionally you can see the CPU usage too.
Although not as full-featured as New Relic, the nodetime npm package (and matching site) does provide memory usage, latency and similar monitoring.

Troubleshooting a Hanging Java Web App

I have a web application that hangs under high loads. I'm not going to go into the specifics of the code because I really just want some troubleshooting advice and tooling recommendations.
It's a web app, so each request get's a thread. Under a high load test, the app begins to consume all of the cpu, while becoming unresponsive. I suspect that the request threads are hanging in the new code that we are testing. Due to the fact of the cpu consumption, I'm assuming this must be on my app side. My understanding, which could be wrong, is that total cpu consumption indicated my first troubleshooting efforts should be in looking at the code that's consuming those cycles.
What are some tools and/or methods for inspecting which threads are hanging and on what lines of code? Again, I can easily force the app into the problematic behavior.
I've found and been trying out visualvm. Seems like the perfect tool. Still open for suggestions though. I looked at eclipse TPTP and it seems to be end-of-life-ing as well as requiring a more heavy weight deployment.
You can insert logging messages at starting a thread and closing a thread. Then you start the application and inspect the output while penetrating the code.
Another way is to look for memory leaks. If you are sure you haven't one, you can extend the virtual memory of your JVM.
#chad: do you have Database in whole picture...you may want to start by looking what is happening at DB side...you can very well look into DB locks, current sessions etc.

Resources