Observing increased memory with Node.js is completely normal behavior. Node.js constantly analyzes your current code, generates optimized code, returns to non-optimized code (if necessary), etc. This requires quite a bit of memory even for the simplest applications (Node.js itself is mostly written in JavaScript, which follows the same optimizations / deoptimizations as your own code).
In addition, a process may be given more memory when necessary, but many operating systems only remove allocated memory from a process when they decide that it is needed elsewhere (i.e., another process). Thus, the application can consume 1 GB of RAM at peak, then loading garbage, usage drops to 500 MB, but the process can still support 1 GB.
Memory Leak Detection
To properly analyze memory usage and memory leaks, you should use Node.js process.memoryUsage() .
You must configure an interval that resets the use of this memory to a file, that is, every second, and then apply "stress" to your application for several seconds (that is, for web servers, it produces several thousand requests). Then take a look at the results and see if the memory is increasing, or if it follows a robust increase / decrease scheme.
Memory Leak Detection
The best tool for this is most likely node-heapdump . You use it with a Chrome debugger.
- Start your application and apply the initial stress (this is to create optimized code and "warm up" your application).
- While the application is not working, create a heapdump file
- Perform one additional operation (i.e., another request) that you suspect is likely to cause a memory leak - this is probably the hardest part, especially for large applications
- Generate another heapdump file
- Download both heapdumps to the Chrome debugger and compare them - if there is a memory leak, you will see that there are some objects that were allocated during this single request, but were not subsequently released
- Examine the object to determine where the leak occurs.
I had the opportunity to investigate the reported memory leak in the Sails.js structure - you can see a detailed description of the analysis (including beautiful graphs, etc.) on this question .
There is also a detailed article about working with heapdumps from StrongLoop - I suggest taking a look at it.
source share