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

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744788540a4593774.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信