Javascript 从加载的iframe中删除节点 上下文
parent.html带有两个按钮,具有以下角色: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>
- 将另一个文档(child.html)加载到iframe中
- 卸载子文档
<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
<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