1

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!

1
  • Run many more requests and see if the OS measured memory usage eventually plateaus. nodejs does not necessarily return all unused memory back to the OS. It may be freed internally and available inside nodejs for future allocations, but not given back to the OS. Commented Feb 21, 2016 at 17:14

1 Answer 1

1

The garbage collector in NodeJS will not kick in until memory usage goes above a certain threshold. I found the following page to be an interesting read:

http://blog.yld.io/2015/08/10/debugging-memory-leaks-in-node-js-a-walkthrough/

On the page several methods of diagnosing memory leaks and heap usage are explained (including exposing the garbage collector so that it can be manually called!)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.