自调用的情景, 像递归, 都有一个终止条件的.
但是假设没有终止条件, 一直在不断的执行某个function, 而且是前一个function执行完之后, 再执行下一个.
const showMem = function() {
const mem = process.memoryUsage();
const format = function(bytes) {
return (bytes / 1024 / 1024).toFixed(2) + ' MB';
};
console.log(
'Process: heapTotal ' +
format(mem.heapTotal) +
' heapUsed ' +
format(mem.heapUsed) +
' rss ' +
format(mem.rss)
);
console.log('---------------------------------------------------------------------------');
};
function sleep(ms) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, ms);
});
}
/**
* 测试内存泄露
* @returns {Promise.<void>}
*/
async function testMemLack() {
showMem();
await sleep(100);
await testMemLack();
}
testMemLack();
输出
Process: heapTotal 6.00 MB heapUsed 3.78 MB rss 27.77 MB
---------------------------------------------------------------------------
Process: heapTotal 6.00 MB heapUsed 4.20 MB rss 28.09 MB
---------------------------------------------------------------------------
Process: heapTotal 6.00 MB heapUsed 4.21 MB rss 28.09 MB
---------------------------------------------------------------------------
Process: heapTotal 6.00 MB heapUsed 4.22 MB rss 28.09 MB
---------------------------------------------------------------------------
Process: heapTotal 6.00 MB heapUsed 4.23 MB rss 28.09 MB
---------------------------------------------------------------------------
Process: heapTotal 6.00 MB heapUsed 4.23 MB rss 28.09 MB
---------------------------------------------------------------------------
Process: heapTotal 6.00 MB heapUsed 4.24 MB rss 28.09 MB
---------------------------------------------------------------------------
Process: heapTotal 6.00 MB heapUsed 4.25 MB rss 28.09 MB
---------------------------------------------------------------------------
Process: heapTotal 6.00 MB heapUsed 4.25 MB rss 28.09 MB
---------------------------------------------------------------------------
Process: heapTotal 6.00 MB heapUsed 4.26 MB rss 28.09 MB
---------------------------------------------------------------------------
Process: heapTotal 6.00 MB heapUsed 4.26 MB rss 28.09 MB
---------------------------------------------------------------------------
Debugger attached.
Process: heapTotal 9.50 MB heapUsed 4.57 MB rss 30.25 MB
---------------------------------------------------------------------------
Process: heapTotal 9.50 MB heapUsed 4.59 MB rss 30.25 MB
---------------------------------------------------------------------------
Process: heapTotal 9.50 MB heapUsed 4.60 MB rss 30.25 MB
---------------------------------------------------------------------------
Process: heapTotal 9.50 MB heapUsed 4.61 MB rss 30.25 MB
---------------------------------------------------------------------------
Process: heapTotal 9.50 MB heapUsed 4.62 MB rss 30.25 MB
---------------------------------------------------------------------------
Process: heapTotal 9.50 MB heapUsed 4.63 MB rss 30.25 MB
---------------------------------------------------------------------------
Process: heapTotal 9.50 MB heapUsed 4.64 MB rss 30.25 MB
---------------------------------------------------------------------------
Process: heapTotal 9.50 MB heapUsed 4.65 MB rss 30.25 MB
---------------------------------------------------------------------------
Process: heapTotal 9.50 MB heapUsed 4.66 MB rss 30.25 MB
---------------------------------------------------------------------------
Process: heapTotal 9.50 MB heapUsed 4.67 MB rss 30.25 MB
---------------------------------------------------------------------------
Process: heapTotal 9.50 MB heapUsed 4.68 MB rss 30.25 MB
---------------------------------------------------------------------------
Process: heapTotal 9.50 MB heapUsed 4.69 MB rss 30.25 MB
---------------------------------------------------------------------------
Process: heapTotal 9.50 MB heapUsed 4.59 MB rss 34.16 MB
---------------------------------------------------------------------------
Process: heapTotal 9.50 MB heapUsed 4.59 MB rss 34.41 MB
---------------------------------------------------------------------------
Process: heapTotal 9.50 MB heapUsed 4.59 MB rss 34.47 MB
---------------------------------------------------------------------------
Process: heapTotal 9.50 MB heapUsed 4.59 MB rss 34.48 MB
---------------------------------------------------------------------------
Process: heapTotal 9.50 MB heapUsed 4.60 MB rss 34.71 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.60 MB rss 33.63 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.60 MB rss 32.54 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.60 MB rss 32.46 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.60 MB rss 32.63 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.60 MB rss 32.55 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.61 MB rss 32.46 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.61 MB rss 32.64 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.61 MB rss 32.55 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.61 MB rss 32.47 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.61 MB rss 32.64 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.62 MB rss 32.55 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.62 MB rss 32.47 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.62 MB rss 32.64 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.62 MB rss 32.56 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.62 MB rss 32.48 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.63 MB rss 32.65 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.63 MB rss 32.56 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.63 MB rss 32.48 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.63 MB rss 32.65 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.63 MB rss 32.57 MB
---------------------------------------------------------------------------
Process: heapTotal 6.50 MB heapUsed 4.63 MB rss 32.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.64 MB rss 32.66 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.64 MB rss 32.57 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.64 MB rss 32.49 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.64 MB rss 32.66 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.64 MB rss 32.57 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.65 MB rss 32.49 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.65 MB rss 32.66 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.65 MB rss 32.58 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.65 MB rss 32.49 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.65 MB rss 32.66 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.66 MB rss 38.02 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.67 MB rss 38.66 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.70 MB rss 39.23 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.71 MB rss 39.23 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.72 MB rss 39.23 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.73 MB rss 39.23 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.75 MB rss 39.23 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.76 MB rss 39.23 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.77 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.78 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.79 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.80 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.81 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.82 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.83 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.84 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.85 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.86 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.87 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.88 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.89 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.91 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.92 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.93 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.94 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.95 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.96 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.97 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.98 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 4.99 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 5.00 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 5.01 MB rss 39.48 MB
---------------------------------------------------------------------------
Process: heapTotal 7.00 MB heapUsed 5.02 MB rss 39.48 MB
---------------------------------------------------------------------------