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

108 阅读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 进程:
clipboard1
其中的两个代码当前Chrome加载的扩展程序:
clipboard2
clipboard1
观察到内存迅速增加:
clipboard3
通过比较两个heap profile,发现引起内存泄露是因为code里的closure:
clipboard5
clipboard4