无限循环自调用, 存在内存泄露吗?
发布于 7 年前 作者 axetroy 5078 次浏览 来自 问答

自调用的情景, 像递归, 都有一个终止条件的.

但是假设没有终止条件, 一直在不断的执行某个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
---------------------------------------------------------------------------
10 回复
function sleep(ms) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            new Array(1000000).join('*');
            resolve();
        }, ms);
    });
}

我也在研究这个问题,刚跑了一会,发现没。

应该不会泄露,每次函数执行完所有的资源都被释放了

首先这个不叫内存泄漏,内存上涨是因为调用深度越来越大导致的,内存泄漏一般指用户在heap上申请的内存空间没有被及时释放导致的。 递归可以分为普通递归和尾递归。 尾递归指的是一个函数的递归调用只出现在函数结束处,且这个调用不是任何表达式的一部分,不是这种形式的递归都是普通递归。尾递归的好处就是一些编译器可以识别它,并做出相应优化。普通递归在进入下一次递归调用时是要保存上一个调用的调用帧的。普通递归的调用深度取决于操作系统的调用深度限制,因此普通递归的调用深度过深会导致爆栈。 尾递归由于是最后一次调用且不是任何表达式的一部分,因此不需要上一个函数的调用信息,也就不需要保存上个调用的栈帧,因此不需要压栈(你可以暂时这么理解),所以被优化过的尾递归不管多深都不会爆栈。

js中我记得要用’use strict’可以开启尾递归优化选项,具体版本就没去考究了。

@nullcc 非常感谢, 如你说的, 这就是一个"尾递归"

已经不需要上一个调用的栈帧, 可是打印出来的heapUsed还是在一直增长, 这是为何呢

无限递归自调用和内存泄露没有关系,out of memory 和memory leak是两个不同的概念,无限递归调用可能引发的问题是Maximum call stack size exceeded

@axetroy 你试试添加’use strict’,js默认没有开启尾递归优化

无限调用会发生内存溢出;跟内存泄露不是一回事,如果泄露调用一次也会泄露啊

回到顶部