使用Chrome development tool查看代码内存泄露

96 阅读1分钟

Created by Jerry Wang on Jul 22, 2015

test source code:

<html>
<script>
var theThing = null;
var replaceThing = function () {  
        var priorThing = theThing;
        var unused = function () {
                  if (priorThing) {       console.log("hi");     }  
       };
       theThing = {     longStr: new Array(10000000).join('Jerry'),  //
                  someMethod: function () {       console.log("g");     }  
       };
};  
setInterval(replaceThing, 4000);
</script>

任务管理器里若干个chrome.exe 进程:

其中的两个代码当前Chrome加载的扩展程序:


观察到内存迅速增加:

通过比较两个heap profile,发现引起内存泄露是因为code里的closure:


要获取更多Jerry的原创文章,请关注公众号"汪子熙":