谷歌浏览器 - 内容脚本中的chrome.runtime增加了内存堆

时间:2016-09-13 15:55:00

标签: google-chrome google-chrome-extension memory-leaks profiling

我有一个扩展程序,用于加载仅运行代码chrome.runtime;的内容脚本 当我即时创建iframe,由div包装,并在加载iframe后删除div, 运行GC后内存未释放

screenshot

第一个快照是在页面加载时拍摄的(在运行垃圾收集器之后) 然后我创建并销毁了150个iframe并拍摄了另一张快照

堆增加了大约900%

  • 的manifest.json:

    {
      "manifest_version": 2,
    
      "name": "ChromeRuntime",
      "description": "",
      "version": "1.0.0",
    
      "content_scripts": [
        {
          "matches": ["*://*/*"],
          "js": ["load.js"],
          "all_frames": true
        }
      ]
    }
    
  • load.js:

    chrome.runtime;
    

如果您想重现这一点:

  1. 从中安装扩展程序 https://s3-eu-west-1.amazonaws.com/yana.bucket/extension.zip
  2. https://s3-eu-west-1.amazonaws.com/yana.bucket/testPage.zip
  3. 打开main.html
  4. 运行GC
  5. 拍摄快照
  6. 点击"运行!"在页面
  7. 运行GC
  8. 拍摄快照
  9. 有人有解决此问题的方法吗?

    测试页面,test.html:

    <html>
        <head>
            <script src="mainScript.js"></script>
        </head>
        <body>
            <div id="header-row">
                <span onclick="runMe()">Run!</span>
            </div>
        </body>
    </html>
    

    测试页面,mainScript.js:

    var tabCount = 0;
    
    function runMe() {
        for (var i = 0; i < 150; i++) {
            addNewTab();
        }
    
        setTimeout(clear, 3000);
    }
    
    function addNewTab() {
        var nextTabId = ++tabCount;
        var iframeElm = document.createElement("iframe");
        iframeElm.src = 'iframe.html?id=' + nextTabId + "&isOdd=" + (nextTabId % 2);
    
        document.body.appendChild(iframeElm);
    }
    
    function clear() {
        Array.prototype.slice.call(document.getElementsByTagName('iframe'),0).forEach(function (iframeElm) {
            document.body.removeChild(iframeElm);
        });
    }
    

    测试页面,iframe.html:

    <html>
        <head>
        </head>
        <body>
            <div id="this-is-iframe">
                This is the IFRAME!
            </div>
            <div>
                <span>The Id is : </span>
                <span id="iframe-id"></span>
            </div>
            <div>
                <span>To simulate SPA inside the iframe, press here:</span> <!-- <button onclick='simulateSpa()'>Simulate!</button> -->
            </div>
            <div id='content1'> Some content...</div>
            <div id='content2'>
                <div id='sub-content2'> <span>Sub content</span> <span>Sub content2 </span>  <span>Sub content 3</span> </div>
                <div id='sub-content3'> <span>Sub content</span> <span>Sub content2 </span>  <span>Sub content 3</span> </div>
            </div>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
            tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
            quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
            consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
            cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
            proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        </body>
    </html>
    

0 个答案:

没有答案