Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory usage doesn't decrease in node.js? What's going on?

I am tracking the memory using task manager and my app is a webrtc app using socket.io.

So when I track the memory and open localhost, connect two browser windows, it obviously adds a little bit of memory. I think it starts at like 19.3 MB and then adds 0.5MB for each connection.

HOWEVER! When I close out of the connection, so no more localhost windows are open, it never decreases in memory usage! So if it goes up to 20MB or something, then it will stay there and never ever decrease.

Why is this? Is there some kind of memory leak?

By the way, is this the right way to track memory usage? Or should I not be using task manager?

like image 420
Hellothere Avatar asked Dec 14 '22 14:12

Hellothere


1 Answers

What you are seeing is the total amount of memory allocated from the system by your application. It is normal that it may go up with usage and then reach some sort of plateau and not go back down even though the application doesn't really need that much memory.

There are a number of reasons for this, but the main one is that most applications (including node.js) maintain a heap. In that heap of memory are allocated blocks and free blocks. When you request more memory than the heap contains, then the application requests more memory from the system and grows the heap. When a number of those heap blocks are freed, they are not necessarily given back to the OS right away (or ever).

This doesn't mean the app is leaking memory because if more memory is requested from the heap, those free blocks in the heap will then get reused.

Any given heap manager has to decide when it is efficient to return memory back to the OS and when it is better to just hang onto the free memory for future use. Most heap managers will have some sort of threshold that when more than xx% of memory in the heap is free, it will then figure out if it can give any memory back to the OS. But, even then, giving memory back to the OS is not always easy or feasible because heaps can get fragmented with lots of smaller heap blocks allocated all over the heap with lots of free blocks in between, but not contiguous large block that could actually be given back to the OS.

In addition, many applications use various forms of caches. These caches will likely all have some sort of maximum size, but if there is lots of free system memory available, these caches may grow fairly large. For example, most template systems one might use with node.js cache parsed templates from disk. As you visit more and more pages, the cache will grow larger and larger.

When looking from the outside like you are looking, the only real way to see if you actually have a problem is to just let the server run over thousands of requests and see if memory usage continues to grow and even grows beyond what the real system memory is (causing swapping). If you see that happen, then you truly have some sort of memory leak.

Keep in mind that in a large memory system with an advanced memory manager, you actually WANT an application to use memory to improve performance as long as there is unused system memory (for things like caches) so seeing it take advantage of free system memory is a good thing. You also want it to be smart enough to recognize when it's running out of free system memory and perhaps dial back what it uses to avoid paging to disk.


The only real way to see how much memory node.js is actually using and what it is using it for is to look at memory usage from inside of node.js using various memory analysis tools. This can give you entire snapshots of what is in the heap and you can even do comparisons between two points in time in the heap to see what was allocated or freed between two points in time. In my experience with the tools, the data is voluminous and it takes some time to understand how to read the data and draw some sort of useful conclusion. Great tools - just takes some time to learn how to use it.

See this set of hits on node.js heap snapshot for lots of discussion about how to look "inside" of node.js at actual memory usage.

like image 126
jfriend00 Avatar answered Dec 31 '22 00:12

jfriend00