问题描述
我注意到我的 node.js 应用程序的 RSS(常驻集大小)随着时间的推移而增长,并且考虑到我的服务器上出现JS 对象分配失败 - 内存不足"错误,这似乎是一个可能的原因.
I've noticed that RSS (Resident Set Size) of my node.js app is growing over time, and considering I'm having a "JS Object Allocation Failed - Out of Memory" error on my server, it seems a likely cause.
我设置了以下非常简单的 Node 应用程序:
I set up the following very simple Node app:
var express = require('express');
var app = express();
app.get('/',function(req,res,next){
res.end(JSON.stringify(process.memoryUsage()));
});
app.listen(8888);
只需按住刷新"热键@ http://localhost:8888/我就可以观看 RSS/heap/etc.增长,直到 RSS 远高于 50mb(在我感到无聊之前).等了几分钟再回来,RSS 下降了 - 大概 GC 已经运行了.
By simply holding down the "refresh" hotkey @ http:// localhost:8888/ I can watch the RSS/heap/etc. grow, until RSS gets well above 50mb (before I get bored). Waiting a few minutes and coming back, the RSS drops - presumably the GC has run.
我想弄清楚这是否能解释我的实际节点应用程序崩溃的原因……我的生产应用程序很快达到大约 100Mb RSS 大小,当它崩溃时,它通常在 200Mb-300Mb 之间.据我所知,这应该不会太大(我相信节点应该能够处理 1.7Gb 左右),但尽管如此,我还是担心我的生产服务器上的 RSS 大小呈上升趋势(衰减代表崩溃):
I'm trying to figure out if this explains why my actual node app is crashing... my production app quickly hits about 100Mb RSS size, when it crashes it is generally between 200Mb-300Mb. As best as I can tell, this should not be too big (node should be able to handle 1.7Gb or so, I believe), but nonetheless I'm concerned by the fact that the RSS size on my production server trends upwards (falloffs represent crashes):
推荐答案
这个问题已经很老了,还没有答案,所以我会抛出我的,它引用了一个 博文 2013 年至 2014 年由 Jay Conrod 撰写,他曾致力于优化 V8 JavaScript手机引擎".
This question is quite old already and yet has no answer, so I'll throw in mine, which references a blog post from 2013-2014 by Jay Conrod who has "worked on optimizing the V8 JavaScript engine for mobile phones".
V8 在收集垃圾时尽量提高效率,为此它使用了增量标记和懒惰清除.
V8 tries to be efficient when collecting garbage and for that it uses Incremental marking and lazy sweeping.
基本上增量标记负责跟踪您的对象是否可以被收集.
Basically incremental marking is responsible for tracking whether your objects can be collected.
当堆达到特定阈值大小时开始增量标记.
懒惰清理负责收集增量标记时标记为垃圾的对象并执行其他耗时任务.
Lazy sweeping is responsible for collecting the objects marked as garbage during incremental marking and performing other time consuming tasks.
增量标记完成后,开始延迟扫描.所有对象都被标记为存活或死亡,并且堆确切地知道通过清除可以释放多少内存.尽管如此,所有这些记忆不一定必须立即释放,延迟清扫不会真正伤害任何东西.因此,垃圾收集器不是同时清扫所有页面,而是根据需要清扫页面,直到所有页面都被清扫完毕.至此,垃圾回收周期完成,增量标记可以重新开始.
我认为这解释了为什么您的服务器分配如此多的内存直到达到某个上限.为了更好地理解,我建议阅读 Jay Conrod 的博客文章 "A tour ofV8:垃圾收集".
I think this explains why your server allocates so much memory until it reaches a certain cap.For a better understanding I recommend reading Jay Conrod's blog post "A tour of V8: Garbage Collection".
这篇关于Node.js 的 RSS(常驻集大小)随着每个请求而增长,直到达到某个上限是否正常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!