最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJS: calling global.gc() doesn't reduce memory to minimum? - Stack Overflow

programmeradmin0浏览0评论

To investigate memory leaks, I have setup a route that triggers global.gc() at every POST /gc

app.post('/gc', function(req, res){
     global.gc();
});

However, I've noticed that if I spam this request, it reduces the memory usage more and more each time. Shouldn't calling global.gc() once is enough to reduce the memory to minimum?

If so, why does calling multiple times consecutively reduces memory at every call?

(I'm using Node.js v0.12)

To investigate memory leaks, I have setup a route that triggers global.gc() at every POST /gc

app.post('/gc', function(req, res){
     global.gc();
});

However, I've noticed that if I spam this request, it reduces the memory usage more and more each time. Shouldn't calling global.gc() once is enough to reduce the memory to minimum?

If so, why does calling multiple times consecutively reduces memory at every call?

(I'm using Node.js v0.12)

Share Improve this question edited Aug 24, 2015 at 13:27 Saitama asked Aug 24, 2015 at 8:59 SaitamaSaitama 4976 silver badges18 bronze badges 3
  • How (and when) do you measure memory consumption? – Bergi Commented Aug 24, 2015 at 13:39
  • @Bergi The application is connected to PM2, I measure the memory in real-time using pm2 monit. I waited for the memory to climb pretty high (1G). I then send global.gc() signal a few times. Each time it reduces memory by around 30-200Mb, until it hits an absolute minimum of ~80Mb. So it took like 10 calls for a full garbage collection, which contradicts what it should do (full garbage collection in one shot). – Saitama Commented Aug 24, 2015 at 21:21
  • 2 This is due to the way how v8 gc works (sweeping) have a look into this great article about the gc jayconrod./posts/55/a-tour-of-v8-garbage-collection – Bernie Commented Nov 3, 2015 at 19:37
Add a ment  | 

1 Answer 1

Reset to default 4

At a very high level, V8 splits up GC into two parts. Looking for garbage to collect, and actually collecting the garbage. Calling gc() only does the second part, collecting already-found garbage.

发布评论

评论列表(0)

  1. 暂无评论