Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have a memory leak? (nodejs)

I have an app written in nodejs that runs on a server. I have noticed (by manually checking the server load from time to time) that my app is starting with around 50MB total allocated RAM (from which aprox. 10 MB are HEAP) but after 6 days it is using a total of 100 MB (although the HEAP remains more or less constant).

First I've tried to debug it with Chrome but the debugger only monitors the HEAP portion of the allocated RAM (and I have not noticed any problems there). On the other hand I watched the debugger for a limited amount of time.

So, to further monitor the RAM usage, I've written the following code inside my app

var maxRecordedRam = 0;
ramCheck = setInterval(() => {
    let ram = process.memoryUsage();
    let ramRss = Math.round(ram.rss / 1024 / 1024 * 100) / 100;
    let ramHeap = Math.round(ram.heapUsed / 1024 / 1024 * 100) / 100;
    if ( maxRecordedRam < ramRss ) {
        // memory load is higher then the last recorded value
        maxRecordedRam = ramRss;
        modulUtile.log(
                "RAM: " + ramRss + " MB (Heap: " + ramHeap + " MB)", 
                "INFO"
        );
    }
}, 30000);

Please note that modulUtile.log() method is a kind of wrapper for console.log, that logs messages to a file.

After 6 days I have the following output in the log file (the file is larger, I've only picked some values):

[2019-08-23 07:10:19]    RAM: 44.27 MB (Heap: 10.03 MB)
.....
[2019-08-23 07:25:19]    RAM: 56.81 MB (Heap: 11.57 MB)
.....
[2019-08-23 09:13:19]    RAM: 65.85 MB (Heap: 15.72 MB)
.....
[2019-08-23 14:47:49]    RAM: 90.97 MB (Heap: 19.14 MB)
.....
[2019-08-25 09:49:52]    RAM: 93.9 MB (Heap: 10.66 MB)
.....
[2019-08-29 12:39:30]    RAM: 97.02 MB (Heap: 17.23 MB)

The HEAP allocation is up to 20MB during work hours and around 10 mb during the night and in the morning (when the app is kind of idle). But although the HEAP is going up and down, the total RAM is only climbing.

My question: Is it possible to have a memory leak outside the HEAP area (because as far as I know, only HEAP area is used for storing variables)?

like image 914
Cosmin Staicu Avatar asked Feb 02 '26 09:02

Cosmin Staicu


1 Answers

It's not possible for heap memory to "leak" into stack memory.

It might help to also print out the heapTotal and external values of memoryUsage().

If the heapTotal accounts for most of the spare memory, then the JVM is making more and more heap available to your program without relinquishing it back to the system. This is not a memory leak in your code, and the memory may be returned to the system when the unused memory gets large enough to bother with.

If the external portion of the memory is growing, then some unmanaged .dll is consuming more and more memory, possibly due to a memory leak within it, or possibly because of thread mismanagement. Debugging external libraries, especially those you have no control over, can be a pain.

If neither of those is growing, then your own code's stack is growing. This could be caused by two things: reiteration and thread mismanagement.

Are the number of threads used by your code increasing? Then threads are hanging at some point, and new ones are spawning. Each thread contains a small amount of stack that isn't returned until the thread properly exits its lifetime.

Reiteration should not be the culprit unless it's obviously possible, e.g., some functions can loop back into the main work flow through function calls instead of returning.

It's also possible, though unlikely, that the JVM is not properly tracking used heap. The way you'd detect such a bug in the JVM is to run the program on a different JVM and see whether the numbers are roughly the same, or whether the new JVM starts reporting much larger heapUsed values.

Without further information, my suspicion would first fall on "the JVM is holding unused memory to avoid releasing and reacquiring it," followed by "an external .dll has a memory leak."

like image 172
user1167758 Avatar answered Feb 05 '26 06:02

user1167758



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!