Skip to content

Instantly share code, notes, and snippets.

@fengmk2
Last active February 19, 2016 19:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fengmk2/8fdbd896fee458efe0b4 to your computer and use it in GitHub Desktop.
Save fengmk2/8fdbd896fee458efe0b4 to your computer and use it in GitHub Desktop.
co memory leak again
// "co": "~4.1.0",
var co = require('co');
var urllib = require('urllib');
var count = 0;
co(function* () {
while (true) {
try {
yield urllib.request('http://cnpmjs.org/mirrors/apis/iojs');
} catch (err) {
console.error(err);
}
yield sleep(10);
// global.gc();
console.log('run %d, %smb', count++, process.memoryUsage().rss / 1024 / 1024);
}
}).catch(function (err) {
throw err;
});
function sleep(ms) {
return function (callback) {
setTimeout(callback, ms);
};
}
var http = require('http');
var co = require('co');
var urllib = require('urllib');
var count = 0;
var running = false;
function get(url) {
return function (callback) {
http.get(url, function(res) {
var size = 0;
res.on('data', function (data) {
size += data.length;
}).on('end', function () {
callback(null, {
size: size,
statusCode: res.statusCode
});
});
}).on('error', callback);
};
}
function getPromise(url) {
return new Promise(function(resolve, reject) {
http.get(url, function(res) {
var size = 0;
res.on('data', function (data) {
size += data.length;
}).on('end', function () {
resolve({
size: size,
statusCode: res.statusCode
});
});
}).on('error', reject);
});
}
function* start() {
if (running) {
return;
}
running = true;
var r;
try {
r = yield urllib.request('http://npm.taobao.org');
// r = yield get('http://127.0.0.1:7001/');
// r = yield getPromise('http://127.0.0.1:7001/');
} catch (err) {
console.error(err.message);
} finally {
running = false;
}
count++;
if (count % 10 === 0) {
console.log('run %d, %s:%s, %smb',
count, r && r.status, r && r.size, process.memoryUsage().rss / 1024 / 1024);
}
}
var fn = co.wrap(start);
setInterval(function () {
fn.call(null).catch(function (err) {
throw err;
});
}, 10);
var http = require('http');
var co = require('co');
var urllib = require('urllib');
var counts = {
0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0
};
var runnings = {};
function get(url) {
return function (callback) {
http.get(url, function(res) {
var size = 0;
res.on('data', function (data) {
size += data.length;
}).on('end', function () {
callback(null, {
size: size,
statusCode: res.statusCode
});
});
}).on('error', callback);
};
}
function getPromise(url) {
return new Promise(function(resolve, reject) {
http.get(url, function(res) {
var size = 0;
res.on('data', function (data) {
size += data.length;
}).on('end', function () {
resolve({
size: size,
statusCode: res.statusCode
});
});
}).on('error', reject);
});
}
[1, 2, 3, 4, 5, 6].forEach(function (index) {
var fn = co.wrap(function* start() {
if (runnings[index]) {
return;
}
runnings[index] = true;
var r;
try {
r = yield urllib.request('http://127.0.0.1:7001');
// r = yield get('http://127.0.0.1:7001/');
// r = yield getPromise('http://127.0.0.1:7001/');
} catch (err) {
console.error(err.message);
} finally {
runnings[index] = false;
}
counts[index]++;
if (counts[index] % 10 === 0) {
console.log('[#%d] run %d, %s:%s, %smb, %j',
index, counts[index], r && r.status, r && r.res.size,
process.memoryUsage().rss / 1024 / 1024,
process.memoryUsage());
}
});
setInterval(function () {
fn.call(null).catch(function (err) {
throw err;
});
}, 10);
});
@fengmk2
Copy link
Author

fengmk2 commented Jan 19, 2015

need to manual global.gc(); to protect memory leak.

@fengmk2
Copy link
Author

fengmk2 commented Jan 19, 2015

{ rss: 21938176, heapTotal: 12704512, heapUsed: 6675520 }
run 705
{ rss: 21938176, heapTotal: 12704512, heapUsed: 6687112 }
run 706
{ rss: 21938176, heapTotal: 12704512, heapUsed: 6698704 }
run 707
{ rss: 21938176, heapTotal: 12704512, heapUsed: 6710296 }
run 708
{ rss: 21938176, heapTotal: 12704512, heapUsed: 6721888 }
run 709
{ rss: 21938176, heapTotal: 12704512, heapUsed: 6733480 }
run 710
{ rss: 21938176, heapTotal: 12704512, heapUsed: 6745072 }
run 711
{ rss: 21938176, heapTotal: 12704512, heapUsed: 6756664 }
run 712
{ rss: 21938176, heapTotal: 12704512, heapUsed: 6768256 }
run 713
{ rss: 21938176, heapTotal: 12704512, heapUsed: 6779848 }
run 714
{ rss: 21938176, heapTotal: 12704512, heapUsed: 6791440 }
run 715
{ rss: 21950464, heapTotal: 12704512, heapUsed: 4762320 }
run 716
{ rss: 21950464, heapTotal: 12704512, heapUsed: 4773944 }

@fengmk2
Copy link
Author

fengmk2 commented Jan 19, 2015

add var b = new Buffer(1024*1024); and v8 will auto gc

@fengmk2
Copy link
Author

fengmk2 commented Jan 22, 2015

no-loop.js gc log show old pointers too many:

[16178] 25642303 ms: Scavenge 318.8 (646.4) -> 306.9 (646.4) MB, 61.4 ms [allocation failure].
[16178] Memory allocator,   used: 661880 KB, available: 837256 KB
[16178] New space,          used:   1947 KB, available:  14436 KB, committed:  32768 KB
[16178] Old pointers,       used: 222129 KB, available:  92032 KB, committed: 492550 KB
[16178] Old data space,     used:  76148 KB, available:  26007 KB, committed: 102982 KB
[16178] Code space,         used:   3318 KB, available:   4648 KB, committed:   7968 KB
[16178] Map space,          used:   8596 KB, available:   1506 KB, committed:  10205 KB
[16178] Cell space,         used:     28 KB, available:     99 KB, committed:    128 KB
[16178] PropertyCell space, used:     27 KB, available:     36 KB, committed:     64 KB
[16178] Large object space, used:   2048 KB, available: 836215 KB, committed:   2068 KB
[16178] All spaces,         used: 314244 KB, available: 138767 KB, committed: 648734 KB
[16178] External memory reported:  12084 KB
[16178] Total time spent in GC  : 32439.8 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment