本文主要选取了4 Types of Memory Leaks in JavaScript and How to Get Rid Of Them 这篇文章中的一小部分来讲明一下js中产生内存泄漏的常见状况. 对于较难理解的第四种状况, 参考了一些文章来进行说明.javascript
js中若是不用var
声明变量,该变量将被视为window
对象(全局对象)的属性,也就是全局变量.html
function foo(arg) { bar = "this is a hidden global variable"; } // 上面的函数等价于 function foo(arg) { window.bar = "this is an explicit global variable"; }
因此,你调用完了函数之后,变量仍然存在,致使泄漏.java
若是不注意this
的话,还可能会这么漏:node
function foo() { this.variable = "potential accidental global"; } // 没有对象调用foo, 也没有给它绑定this, 因此this是window foo();
你能够经过加上'use strict'
启用严格模式来避免这类问题, 严格模式会组织你建立意外的全局变量.浏览器
var someResource = getData(); setInterval(function() { var node = document.getElementById('Node'); if(node) { node.innerHTML = JSON.stringify(someResource)); } }, 1000);
这样的代码很常见, 若是id
为Node
的元素从DOM
中移除, 该定时器仍会存在, 同时, 由于回调函数中包含对someResource
的引用, 定时器外面的someResource
也不会被释放.闭包
var elements = { button: document.getElementById('button'), image: document.getElementById('image'), text: document.getElementById('text') }; function doStuff() { image.src = 'http://some.url/image'; button.click(); console.log(text.innerHTML); } function removeButton() { document.body.removeChild(document.getElementById('button')); // 虽然咱们用removeChild移除了button, 可是还在elements对象里保存着#button的引用 // 换言之, DOM元素还在内存里面. }
先看这样一段代码:ide
var theThing = null; var replaceThing = function () { var someMessage = '123' theThing = { someMethod: function () { console.log(someMessage); } }; };
调用replaceThing
以后, 调用theThing.someMethod
, 会输出123
, 基本的闭包, 我想到这里应该不难理解.函数
解释一下的话, theThing
包含一个someMethod
方法, 该方法引用了函数中的someMessage
变量, 因此函数中的someMessage
变量不会被回收, 调用someMethod
能够拿到它正确的console.log
出来.this
接下来我这么改一下:url
var theThing = null; var replaceThing = function () { var originalThing = theThing; var someMessage = '123' theThing = { longStr: new Array(1000000).join('*'), // 大概占用1MB内存 someMethod: function () { console.log(someMessage); } }; };
咱们先作一个假设, 若是函数中全部的私有变量, 无论someMethod
用不用, 都被放进闭包的话, 那么会发生什么呢.
第一次调用replaceThing
, 闭包中包含originalThing = null
和someMessage = '123'
, 咱们设函数结束时, theThing
的值为theThing_1
.
第二次调用replaceThing
, 若是咱们的假设成立, originalThing = theThing_1
和someMessage = '123'
.咱们设第二次调用函数结束时, theThing
的值为theThing_2
.注意, 此时的originalThing
保存着theThing_1
, theThing_1
包含着和theThing_2
大相径庭的someMethod
, theThing_1
的someMethod
中包含一个someMessage
, 一样若是咱们的假设成立, 第一次的originalThing = null
应该也在.
因此, 若是咱们的假设成立, 第二次调用之后, 内存中有theThing_1
和theThing_2
, 由于他们都是靠longStr
把占用内存撑起来, 因此第二次调用之后, 内存消耗比第一次多1MB.
若是你亲自试了(使用Chrome的Profiles查看每次调用后的内存快照), 会发现咱们的假设是不成立的, 浏览器很聪明, 它只会把someMethod
用到的变量保存下来, 用不到的就不保存了, 这为咱们节省了内存.
但若是咱们这么写:
var theThing = null; var replaceThing = function () { var originalThing = theThing; var unused = function () { if (originalThing) console.log("hi"); }; var someMessage = '123' theThing = { longStr: new Array(1000000).join('*'), someMethod: function () { console.log(someMessage); } }; };
unused
这个函数咱们没有用到, 可是它用了originalThing
变量, 接下来, 若是你一次次调用replaceThing
, 你会看到内存1MB 1MB的涨.
也就是说, 虽然咱们没有使用unused
, 可是由于它使用了originalThing
, 使得它也被放进闭包了, 内存漏了.
强烈建议读者亲自试试在这几种状况下产生的内存变化.
这种状况产生的缘由, 通俗讲, 是由于不管someMethod
仍是unused
, 他们其中所须要用到的在replaceThing
中定义的变量是保存在一块儿的, 因此就漏了.
若是我没有说明第四种状况, 能够参考如下连接, 或是在评论区评论.