Javascript 内存泄漏示例

Javascript 内存泄漏示例,javascript,memory-leaks,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 'unuse

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

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);

是什么让你认为内存泄漏?我在Chrome上试用了你的代码,确实内存增长非常快,直到网页在650MB左右崩溃。RAMI不知道是否有内存泄漏,但“新数组(1000000)。join('*')”非常昂贵,它会给你一百万(-1)*(我不知道你为什么需要这个)但我必须知道的是,它们的RAM每10毫秒增加一次。代码部分只是内存泄漏主题的示例。