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

查看:18
本文介绍了是否可以在 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 后,下次页面触发垃圾收集时,您会获得部分但不是全部内存(通常 iframe 使用的内存中约有 25% 陷入困境).刷新或导航到新页面 (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.

在诸如 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 之前在 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 更新为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.

不幸的是,该插件并非在所有情况下都有效.对于指向 //en.wikipedia.org/wiki/Memory_leak 的 iframe,它似乎没有多大帮助.

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天全站免登陆