Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从加载的iframe中删除节点 上下文_Javascript_Html_Google Chrome_Iframe_Memory - Fatal编程技术网

Javascript 从加载的iframe中删除节点 上下文

Javascript 从加载的iframe中删除节点 上下文,javascript,html,google-chrome,iframe,memory,Javascript,Html,Google Chrome,Iframe,Memory,parent.html带有两个按钮,具有以下角色: 将另一个文档(child.html)加载到iframe中 卸载子文档 child.html有一个脚本,可以创建n个节点,并将字符串推送到一个数组中(举例说明随着时间的推移内存的使用情况) 示例代码 parent.html: <html> <head></head> <body> <button onclick="loadGame()">Load</button>

parent.html带有两个按钮,具有以下角色:

  • 将另一个文档(child.html)加载到iframe中
  • 卸载子文档
child.html有一个脚本,可以创建n个节点,并将字符串推送到一个数组中(举例说明随着时间的推移内存的使用情况)

示例代码 parent.html

<html>
<head></head>
<body>
  <button onclick="loadGame()">Load</button>
  <button onclick="unloadGame()">Unload</button>
  <div class="loader"></div>
  <script>
    var loadGame, unloadGame;

    loadGame = function() {
      var iframe = document.createElement('iframe');
      iframe.className = 'loader--iframe';
      iframe.src = 'child.html';
      document.querySelector('.loader').appendChild(iframe);
    };
    unloadGame = function() {
      document.querySelector('.loader')
        .removeChild(document.querySelector('.loader--iframe'));
    };
 </script>
 </body>
 </html>
问题 在加载和卸载几次之后,使用Performance>memory record,我发现内存、文档和节点没有得到完全清理:

开始:

  • JS堆:3857912
  • 文件:2
  • 节点:22
完:

  • JS堆:5908928
  • 文件:4
  • 节点:21672

注意:测试是在未加载扩展的情况下使用incognito浏览器窗口运行的

期望值 我试图找到一种将html内容加载到页面中的方法,但在某个时候,我可以卸载内容,确保该孩子占用的所有内存也被释放。因此,主要的疑问是:

  • 这是预期的浏览器行为吗?
  • 有没有办法完全删除子节点、文档和内存分配?
  • <html>
    <head></head>
    <body>
      <p>Loaded Content</p>
      <script>
        (function(){
          var i, el = null,
            x = [];
    
          for (i = 0; i < 10000; i++) {
            el = document.createElement('div');
            el.innerHTML = 'node ' + i;
            document.body.appendChild(el);
          }
          x.push(new Array(1000000).join('x'));
        })()
      </script>
    </body>
    </html>
    
    git clone https://gist.github.com/am/b7c7c762e9064dc9c7fc93e13eb8c0a9
    cd b7c7c762e9064dc9c7fc93e13eb8c0a9
    bash run.sh