Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 292
Saitama Avatar asked Aug 24 '15 08:08

Saitama


1 Answers

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.

like image 109
snek Avatar answered Nov 10 '22 01:11

snek