内存泄漏示例

时间:2014-07-28 10:59:24

标签: javascript memory-leaks

请在以下代码部分中逐步解释内存泄漏的原因:

var theThing = null;
var replaceThing = function () {
  var priorThing = theThing;  // hold on to the prior thing
  var unused = function () {
    // 'unused' is the only place where 'priorThing' is referenced,
    // but 'unused' never gets invoked
    if (priorThing) {
      console.log("hi");
    }
  };
  theThing = {
    longStr: new Array(1000000).join('*'),  // create a 1MB object
    someMethod: function () {
      console.log(12);
    }
  };
};
setInterval(replaceThing, 10);

0 个答案:

没有答案