.Net WebBrowser控制内存泄漏问题 [英] .Net WebBrowser Control Memory Leak Issue

查看:69
本文介绍了.Net WebBrowser控制内存泄漏问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于WPF的应用程序中托管WebBrowser控件之后,观察到在重复访问自定义网页时,内存消耗不断增加,因为这是一个自定义浏览器应用程序,因此WPF浏览器应用程序在一段时间后进入挂起状态。



我明白自定义网页(正在加载)写得很糟糕导致内存泄漏,所以我的问题是,我们可以带来什么方式降低内存消耗或阻止它在第一时间徒步旅行。



一种解决方案是按照贾斯汀罗杰斯的泄漏模式



我想知道的是,我们如何在基于自定义Web浏览器的应用程序中实现一些解决方案来控制进程的内存消耗?



在此先感谢,

小事

After hosting WebBrowser control in WPF based application, it was observed that in repeated access to custom web pages the memory consumption keeps spiking and since this is a custom browser application so the WPF browser application goes into a hung state after some time.

I understand that the custom web page (being loaded) has badly written JavaScript resulting in memory leaks, So my question is, what are the ways in which we can bring down the memory consumption or stop it from hiking at first place.

One Solution is to clean the JavaScript as per the rules mentioned at Leak Patterns by Justin Rogers

What i was wondering was that, How can we implement some solution inside the Custom Web Browser based application to control the memory consumption of the Process?

Thanks in Advance,
Little Things

推荐答案

由于我们可能控制或不控制基于Web浏览器控件的应用程序中加载的网页,因此对于网页



A)我们控制的网页代码

B)我们无法控制的网页代码





A)我们控制的网页代码:

我们必须系统地实施下面提到的要点。以下是在客户端进行编码时应注意的两点。



1)我们可以而且应该通过实施贾斯汀罗杰斯的泄漏模式



2 )糟糕的JQuery编程技术也可以使应用程序停止运行,因此有一个很好的解决方案,我觉得也应该包含在清理方法中 JQuery GarbageCollection





B)代码对于那些我们无法控制的网页:

我们必须系统地实现一系列应该在Page UnLoad中调用的函数。以下两点可以包含在



1)事件处理清理管理器方法将跟踪所有添加的事件,并且在卸载时,它可以取消注册所有的处理程序。



这样的东西

Since we might or might not have control over the web page loaded in our Web Browser control based application, so for the web pages

A) Code for those web pages on which we have control
B) Code for those web pages on which we don''t have control


A) Code for those web pages on which we have control:
We will have to systematically implement points mentioned below. Following are the two points that should be taken care off while coding on client side

1)We can and should perform a clean up by implementing the rules mentioned in Leak Patterns by Justin Rogers

2)Bad JQuery programming technique can also bring the application to a halt and so there is a great solution that i felt should also be included in the clean up approach JQuery GarbageCollection


B) Code for those web pages on which we don''t have control:
We will have to systematically implement a series of functions which should be called at Page UnLoad. Following are the two points that can be included in the

1) Event handling clean up manager method will keep track of all the added events and On unload, it can unregister all the handlers.

something like this
function ReleaseHandler() {
        var EvtMgr = (function() {
            var listenerMap = {};

            // Public interface
            return {
                addListener: function(evtName, node, handler) {
                    node["on" + evtName] = handler;
                    var eventList = listenerMap[evtName];
                    if (!eventList) {
                        eventList = listenerMap[evtName] = [];
                    }
                    eventList.push(node);
                },

                removeAllListeners: function() {
                    for (var evtName in listenerMap) {
                        var nodeList = listenerMap[evtName];
                        for (var i = 0, node; node = nodeList[i]; i++) {
                            node["on" + evtName] = null;
                        }
                    }
                }
            }
        })();
    }





2)对于解引用函数,清除函数将作为参数传递给DOM元素的引用,如这个



2) For dereferencing function, the purge function will be passed a reference to a DOM element as an argument like this

purge(document.body)



循环遍历元素的属性。如果它找到任何函数,则将它们清空。这打破了循环,允许回收内存。它还将查看所有元素的后代元素,并清除它们的所有周期。清除功能对Mozilla和Opera无害。这对IE很重要。在删除任何元素之前,应该通过removeChild方法或通过设置innerHTML属性来调用清除函数。



函数清除(d){

var a = d.attributes,i,l,n;

if(a){

for(i = a.length - 1; i> ; = 0; i - = 1){

n = a [i] .name;

if(typeof d [n] ===''function'') {

d [n] = null;

}

}

}

a = d.childNodes;

if(a){

l = a.length;

for(i = 0; i< l; i + = 1){

purge(d.childNodes [i]);

}

}

} b / b
注意:IE和基于IE的控件(如Web浏览器控件)中内存泄漏的最大原因是因为DOM有一个单独的垃圾收集器对于javascript。


It loops through the element''s attributes. If it finds any functions, it nulls them out. This breaks the cycle, allowing memory to be reclaimed. It will also look at all of the element''s descendent elements, and clear out all of their cycles as well. The purge function is harmless on Mozilla and Opera. It is essential on IE. The purge function should be called before removing any element, either by the removeChild method, or by setting the innerHTML property.

function purge(d){
var a = d.attributes, i, l, n;
if (a) {
for (i = a.length - 1; i >= 0 ; i -= 1) {
n = a[i].name;
if (typeof d[n] === ''function'') {
d[n] = null;
}
}
}
a = d.childNodes;
if (a) {
l = a.length;
for (i = 0; i < l; i += 1) {
purge(d.childNodes[i]);
}
}
}

Note: The biggest reason for this Memory Leak in IE and IE based controls (Like Web Browser Control) is because there is a separate garbage collector for the DOM and for javascript.


这篇关于.Net WebBrowser控制内存泄漏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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