I thought I understood memory usage / garbage collection in NodeJS, however I'm not seeing what I expect when running a very simple NodeJS http server. I created the smallest script I could to show what I'm trying to do.
From my understanding, once variables / buffers go out of scope they should be garbage collected, however I'm not seeing that happen. My buffers seem to be retained, and as such the overall memory usage keeps growing.
Example below:
var http = require("http"),
args = process.argv.slice(2),
port = Number( args[0] ) ? args[0] : 8080
http.createServer( function( request, response ) {
console.log( Date(), request.url, process.memoryUsage().rss / 1024 / 1024 )
var s3url = 'http://hmn-uploads-eu.s3.amazonaws.com/yell-production/uploads/2016/01/BYC-Hero-Home-Small.jpg'
var buffer = ''
http.get( s3url, function( s3response ) {
s3response.on( 'data', function( data ) {
buffer += data
})
s3response.on( 'end', function() {
response.write( buffer )
response.end()
})
} )
}).listen( parseInt( port, 10 ) )
console.log("Server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown");
Essentially I'm just downloading a file and serving it as the response. I expect the buffers to be garbage collected once the request has finished (as the function returns and there's nothing external holding references to the buffers). However My logging shows this:
Server running at
=> http://localhost:8080/
CTRL + C to shutdown
Sun Feb 21 2016 16:26:28 GMT+0000 (UTC) / 10.20703125
Sun Feb 21 2016 16:26:30 GMT+0000 (UTC) / 14.69140625
Sun Feb 21 2016 16:26:31 GMT+0000 (UTC) / 15.22265625
Sun Feb 21 2016 16:26:32 GMT+0000 (UTC) / 16.26171875
Sun Feb 21 2016 16:26:33 GMT+0000 (UTC) / 17.8125
Sun Feb 21 2016 16:26:34 GMT+0000 (UTC) / 18.28515625
Sun Feb 21 2016 16:26:36 GMT+0000 (UTC) / 18.55078125
Sun Feb 21 2016 16:26:37 GMT+0000 (UTC) / 18.81640625
Clearly showing the memory usage is increasing over time. I'm presuming some assumption I have about how the memory should be reclaimed is wrong, but I'm struggling to see what I'm missing here.
Thanks!