node.js中的内存使用量没有减少?这是怎么回事? [英] Memory usage doesn't decrease in node.js? What's going on?

查看:111
本文介绍了node.js中的内存使用量没有减少?这是怎么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用任务管理器跟踪内存,我的应用程序是使用socket.io的webrtc应用程序。

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

所以当我跟踪内存并打开localhost时,连接两个浏览器窗口,显然会增加一些内存。我认为它从19.3 MB开始,然后为每个连接增加0.5MB。

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.

然而!当我关闭连接时,不再打开localhost窗口,它的内存使用量永远不会减少!因此,如果它达到20MB或其他东西,那么它将保持在那里,永远不会减少。

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?

推荐答案

您所看到的是您的应用程序从系统分配的内存总量。它是正常的,它可能会随着使用而上升,然后达到某种平稳状态,即使应用程序确实不需要那么多内存也不会再回落。

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.

这有很多原因,但主要原因是大多数应用程序(包括node.js)都保持堆。在那个内存堆中分配了块和空闲块。当您请求的内存多于堆包含的内存时,应用程序会从系统请求更多内存并增加堆。当释放了许多堆块时,它们不一定立即返回操作系统(或永远)。

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.

任何给定的堆管理器必须决定何时将内存返回到操作系统是有效的,以及什么时候最好挂在空闲内存上以备将来使用。大多数堆管理器都会有某种阈值,当堆中超过xx%的内存空闲时,它会判断它是否可以将任何内存返回给操作系统。但是,即便如此,将内存返回到操作系统并不总是容易或可行的,因为堆可能会碎片化,堆在整个堆中分配大量较小的堆块,中间有大量空闲块,但实际上并不是连续的大块回到操作系统。

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.

此外,许多应用程序使用各种形式的缓存。这些缓存可能都具有某种最大大小,但如果有大量可用的系统内存,这些缓存可能会变得相当大。例如,大多数模板系统可以使用node.js缓存从磁盘解析的模板。随着您访问越来越多的页面,缓存将变得越来越大。

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.

查看node.js实际使用的内存量以及使用它的唯一真实方法是使用各种内存分析工具查看node.js内部的内存使用情况。这可以为您提供堆中内容的完整快照,您甚至可以在堆中的两个时间点之间进行比较,以查看在两个时间点之间分配或释放的内容。根据我使用这些工具的经验,数据量很大,需要一些时间来理解如何读取数据并得出某种有用的结论。好的工具 - 只需要一些时间来学习如何使用它。

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.

请参阅 node.js堆快照上的命中集关于如何在实际内存使用情况下查看node.js的内部的大量讨论。

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.

这篇关于node.js中的内存使用量没有减少?这是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆