是否可以在IE中使用iframe而不会出现内存泄漏? [英] Is it possible to use iframes in IE without memory leaks?

查看:97
本文介绍了是否可以在IE中使用iframe而不会出现内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有版本的IE(包括10个)似乎都会保留iframe分配的大量内存,直到发生window.top.unload。这为长寿命页面带来了相当大的挑战,这些页面可能会在其整个生命周期中创建许多iframe。可以在此处找到问题的简化示例:

All versions of IE (including 10) appear to hold on to a significant amount of memory allocated by iframes until window.top.unload occurs. This creates quite the challenge for long-lived pages that may create a number of iframes throughout their lifetime. A simplified example of the problem can be found here:

http:/ /pastebin.com/FmZ7iMHB

该示例使用维基百科页面为iframe放大问题,但即使是单个图像的简单页面也会泄漏。

That example uses a Wikipedia page for the iframe to magnify the problem, but even a simple page with a single image will leak.

简而言之,在IE中销毁iframe之后,当下一次页面触发垃圾收集时,会得到一些但不是全部的内存(通常约为25) iframe使用的内存百分比陷入困境。刷新或导航到新页面(window.top.unload)将释放大部分或全部剩余内存。

In a nutshell, after you destroy an iframe in IE you get some but not all of the memory back the next time a page triggers a garbage collection (typically about 25% of the memory used by the iframe gets stuck in limbo). Refreshing or navigating to a new page (window.top.unload) will free up most or all of the remaining memory.

在像<等工具中检测不到此特定泄漏a href =http://home.orange.nl/jsrosman/ =noreferrer> sIEve 和微软的 JS内存泄漏探测器。我已经阅读了IE中关于漏洞iframe的所有内容,但是我遇到的解决方案没有运气。

This particular leak is not detectable in tools like sIEve and Microsoft's JS Memory Leak Detector. I've read everything I can find about leaky iframes in IE, but have had no luck with the solutions I've come across.

有没有人知道解决方案或解决方法这个问题?我唯一的缓解策略是在父页面销毁之前尽可能多地从iframe中进行清理,但是当你不控制被框架的页面时,这没有任何帮助。

Does anyone know a solution or workaround to this problem? The only mitigation strategy I have is to do as much cleanup as you can from within the iframe before the parent page destroys it, but that doesn't help when you don't control the page being framed in.

推荐答案

我整理了一个jQuery插件,用于清除在某些情况下防止内存泄漏的iframe:

I put together a jQuery plugin for cleaning iframes that prevents memory leaks in some cases:

(function($) {
    $.fn.purgeFrame = function() {
        var deferred;

        if ($.browser.msie && parseFloat($.browser.version, 10) < 9) {
            deferred = purge(this);
        } else {
            this.remove();
            deferred = $.Deferred();
            deferred.resolve();
        }

        return deferred;
    };

    function purge($frame) {
        var sem = $frame.length
          , deferred = $.Deferred();

        $frame.load(function() {
            var frame = this;
            frame.contentWindow.document.innerHTML = '';

            sem -= 1;
            if (sem <= 0) {
                $frame.remove();
                deferred.resolve();
            }
        });
        $frame.attr('src', 'about:blank');

        if ($frame.length === 0) {
            deferred.resolve();
        }

        return deferred.promise();
    }
})(jQuery);

此代码通过在清除帧src之前将帧src更新为about:blank来处理跨源帧内容。要使用该插件,请调用 $ frame.purgeFrame()否则您将调用 $ frame.remove()

This code handles cross-origin frames by updating the frame src to "about:blank" before cleaning its content. To use the plugin, call $frame.purgeFrame() where you would otherwise call $frame.remove().

正如Josh所指出的,显示图像的iframe似乎与内存泄漏相关。例如,创建指向 google.com 的iframe会在IE7和IE8中产生内存泄漏。使用上面的插件可以防止这些泄漏。

As Josh points out, iframes that display an image seem correlated with memory leaks. For example, creating iframes pointing to google.com will produce a memory leak in IE7 and IE8. Using the plugin above prevents those leaks.

不幸的是,插件在所有情况下都无效。 iframes指向 //en.wikipedia.org/wiki/Memory_leak 似乎没什么帮助。

Unfortunately that plugin is not effective in all cases. It does not seem to help much with iframes pointed at //en.wikipedia.org/wiki/Memory_leak.

我用于测试内存泄漏和测试上述插件的代码是 https:/ /gist.github.com/3125807

The code that I used for testing memory leaks and for testing the above plugin is at https://gist.github.com/3125807

正如Josh所说,内存泄漏实际上是IE8中的伪泄漏。但是在IE7中,即使父窗口卸载,也不会回收内存。

As Josh says, the memory leaks are actually pseudo-leaks in IE8. However in IE7 memory is not reclaimed, even when the parent window unloads.

这篇关于是否可以在IE中使用iframe而不会出现内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆