【问题标题】:Removing iframe contents from memory onunload/onbeforeunload in IE7在 IE7 中从内存 onunload/onbeforeunload 中删除 iframe 内容
【发布时间】:2013-07-22 17:32:45
【问题描述】:

我有一个 iframe,它通过 ajax 加载到我的页面中。在每个间隔上,一个新的 iframe 都会覆盖旧的 iframe。出于某种原因,iframe 内容在 IE7 中保留在我的记忆中。我确定它是 iframe 内容,因为当我将一个小站点加载到 iframe 中时,内存大小会缓慢增加。当我将大型网站加载到框架中时,每次刷新都会更快。

我想在每次调用间隔时从内存中清除 iframe 内容。我尝试了下面的脚本(这个是针对 window.onunload 的 - 我也在每个间隔都尝试过),但两者都不起作用,并且在我结束浏览器会话之前将 iframe 内容保留在我的内存中。

// Remove iframecontents
window.onunload = function() {
    $('iframe').each(function(i, frame) {
        frameDoc = frame.contentDocument || frame.contentWindow.document;
        frameDoc.removeChild(frameDoc.documentElement);
    });
}

// Remove iframe with jQuery
window.onunload = function() {
    $('iframe').each(function(i, frame) {
        $(frame).remove();
    });
}

是否有任何方法可以从我的内存中删除 iframe 中的内容而无需重新启动浏览器?

这些解决方案似乎也不起作用: Iframes and memory management in Javascript jQuery DOMWindow script doesn't release memory

【问题讨论】:

  • 奇怪,但我终于在那周晚些时候通过使用以下代码为 IE7 解决了这个问题:codewindow.onunload = function() { $('iframe').remove(); } code

标签: javascript memory iframe memory-leaks


【解决方案1】:

我从 Windows 7 开始使用 IE 的经验一直是您不能明确强制它进行垃圾收集。它与 Windows 动态内存管理一起工作。如果您的系统整体内存不足,那么 IE 将开始更频繁地释放内存。如果您有很多可用内存,它将一直保留到您终止该进程为止。

但是,每个页面(或 iframe)确实会占用一定数量的内存,并且 IE 往往只有在您离开时才会卸载其中的一大块。这对于 SPA 应用程序(单页应用程序)来说是一种糟糕的情况。这使得明智地编码以首先保持低内存使用变得更加重要。

【讨论】:

    猜你喜欢
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多