简体   繁体   中英

NodeJS V8 not doing proper garbage collecting

I have this simple http server

var http = require('http');

var server = http.createServer(function(request, response) {

    var data = [];
    for ( var i=0; i < 1000000; i++ ) {
        data.push({});
    }

    response.end('Done');

});

server.listen(3000);

When I start the server the process uses around 8MB of memory. When I make a request to the server the memory usage rises to above 100MB and it just stays like that. Then I hold F5 for a few seconds to spam some requests and the memory usage grows above 400MB at some points. When all the request are processed the server is still using above 100MB.

When I make another request the memory sometimes goes above or close to 200MB or stays approximately the same. I let the server running for a while and the memory doesn't get released.

I tried putting date = null and the result was the same. Then I tried running the server with the --expose-gc flag and putting global.gc() after I null the value and the results are better but the memory still stays above 50MB.

If your system has an abundance of memory available, there is unlikely to be any condition triggering a need for garbage collection. If you can run up the memory usage to a maximum point and continue serving requests, clearly garbage collection is working, as memory will need to be freed before more is allocated.

You can try starting up a different process to deliberately suck up more memory, then look again to see if the original node.js process' garbage collection seems to behave more aggressively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM