当前位置: 首页 > 图文教程 > 工具软件 > 浏览下载 > IE6内存泄露的另类解决方法

浏览下载
如何解决mac下qq不能看到群共享的问题
Google街景视图“侵犯个人隐私”搞笑图片
Chrome比Safari谁更体贴用户关注细节
使用迅雷下载时打开网页缓慢或无法打开
深度解析傲游浏览器防恶反钓的功能
修改注册表使IE8中默认显示的微软雅黑字体
Webjx总结的7款顶级谷歌Chrome浏览器插件
firefox 4浏览器创新的界面设计
Opera 10.5浏览器的预览版的惊喜
10个不错的世界之窗浏览器扩展功能介绍
Chrome OS系统登录过程由浏览器完成
如何在Windows7下对IE8提速
迅雷5.9下载常用快捷键
迅雷网页图片修复工具实现红X图片的修复
辞去2009迎接2010 傲游博客新年祝福
Firefox浏览器和IE浏览器提速配置技巧
迅雷看看播放器3.2新版全新功能
迅雷下载99%停止怎么办?8种解决方法
11款Google Chome浏览器插件
Firefox浏览器设置参数提高浏览速度

浏览下载 中的 IE6内存泄露的另类解决方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-11   浏览: 68 ::
收藏到网摘: n/a

Hedger Wang 在国内 blog 上得到的方法:使用 try … finally 结构来使对象最终为 null ,以阻止内存泄露。

其中举了个例子:

function createButton() {
    var obj = document.createElement("button");
    obj.innerHTML = "click me";
    obj.onclick = function() {
        //handle onclick
    }
    obj.onmouseover = function() {
        //handle onmouseover
    }
    return obj;//return a object which has memory leak problem in IE6
}
var dButton = document.getElementById("d1").appendChild(createButton());
//skipped.... 

对于 IE6 中,引起内存泄露的原因,可看《Understanding and Solving Internet Explorer Leak Patterns》一文。

上面的例子,应该属于上文中的 “Closures”原因。

再看下用 try … finally 的解决方法:

/**
     * Use the try ... finally statement to resolve the memory leak issue
*/
function createButton() {
    var obj = document.createElement("button");
    obj.innerHTML = "click me";
    obj.onclick = function() {
        //handle onclick
    }
    obj.onmouseover = function() {
        //handle onmouseover
    }
    //this helps to fix the memory leak issue
    try {
        return obj;
    } finally {
        obj = null;
    }
}
var dButton = document.getElementById("d1").appendChild(createButton());
//skipped....

可能大家有疑问: finally 是如何解析的呢?

答案是:先执行 try 语句再执行 finally 语句。

例如:

function foo() {
    var x = 0;
    try {
        return print("call return " + (++x));
    } finally {
        print("call finally " + (++x));
    }
}
print('before');
print(foo());
print('after');

返回的结果为:
print » before
print » call return 1
print » call finally 2
print » true
print » after

更多详细的演示:
《Finally, the alternative fix for IE6’s memory leak is available》

相关的一些讨论:
《Is “finally” the answer to all IE6 memory leak issues?》