Reputation: 1435
Given the following code snippet:
var request = require('request');
function getGoogle() {
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
//console.log(body) // Print the google web page.
}
});
}
setInterval(getGoogle, 1000);
Using Node version 0.10.17, this code leaks. Any ideas why?
Upvotes: 3
Views: 6440
Reputation: 11
In this page (http://www.joyent.com/blog/walmart-node-js-memory-leak) they actually found a memory leak in the http stack of Node.JS
The good news is that it is fixed in Node.JS version v0.10.22
Upvotes: 1
Reputation: 14963
I see a lot of questions where people go "aw my gawd memory leak!" based on the first few minutes of the app running, without waiting to see if the process hits a ceiling or actually runs out of memory. Node just uses as much memory as allowed to.
See the following rough benchmark. I ran your code (with a timeout of 100 instead), and the memory-usage rapidly increased to ~70 MB, but then stopped. The first column is memory usage.
27368 0:00.36 node test.js
40644 0:00.82 node test.js
47468 0:01.21 node test.js
48192 0:01.40 node test.js
67952 0:02.39 node test.js
69448 0:03.29 node test.js # Increasing fast til around here
70016 0:04.46 node test.js
70624 0:07.43 node test.js
70944 0:10.59 node test.js
71612 0:13.63 node test.js
73120 0:16.83 node test.js
70864 0:18.17 node test.js # Look it's decreasing
67780 0:42.27 node test.js # Considerably later it's even lower!
My guess at why it's like this would be because garbage collection is expensive, but I'm not sure and would be happy if anyone had a real explanation and references.
Upvotes: 2