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

前端 未结 1 1206
Happy的楠姐
Happy的楠姐 2021-01-14 16:50

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,

1条回答
  •  伪装坚强ぢ
    2021-01-14 17:39

    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.

    0 讨论(0)
提交回复
热议问题