如何解决 .NET Webbrowser 控件中的内存泄漏? [英] How to get around the memory leak in the .NET Webbrowser control?

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

问题描述

这是 .NET Webbrowser 控件的一个广为人知的老问题.

This is a widely-known, old issue with the .NET Webbrowser control.

总结:拥有 .NET webbrowser 控件导航到页面会增加永远不会释放的内存使用量.

Summary: Having the .NET webbrowser control Navigate to a page increases memory usage that is never freed.

重现内存泄漏:向窗体添加WebBrowser 控件.使用它导航到您想要的任何页面.about:blank 工作正常,向下滚动 Google 图片,直到您的使用量超过 100MB,然后浏览其他地方发现几乎没有任何内存被释放,这是一个更具戏剧性的演示.

Reproduce the memory leak: Add a WebBrowser control to a form. Use it to Navigate to whatever pages you'd like. about:blank works, scrolling down on Google Images until your usage is 100MB+ and then browsing elsewhere to notice barely any of that memory is freed is a more dramatic demonstration.

我目前对应用程序的要求包括长时间运行它,显示有限的 IE7 浏览器窗口.运行 IE7 本身与一些混蛋设置挂钩,BHOs 和组策略也是不可取的,尽管这看起来像是此时的后备.将浏览器嵌入到 Windows 窗体应用程序中.使用不同的浏览器库对我来说不是一个可用的选项.IE7 是必需的.

My current requirements for an application include running it for long periods of time, displaying a limited IE7 browser window. Running IE7 itself with some bastard setup of hooks, BHOs and group policies isn't desired either, although that's looking like the fallback at this time. Embedding a browser into a Windows Forms application is. Using a different browser base is not an available option for me. IE7 is required.

与此已知内存泄漏相关的先前线程和文章:

Previous threads and articles relating to this known memory leak:

经常提出但不起作用的修复:

Often-proposed fixes that DO NOT WORK:

  • 转到不同的页面并不重要.about:blank 触发泄漏.它不需要页面具有 javascript 或任何其他额外技术.
  • 使用不同版本的 Internet Explorer 无关紧要.7、8 和 9 都表现出相同的症状,据我所知,所有版本在控件中都有相同的内存泄漏.
  • Dispose() 控件无济于事.
  • 垃圾收集没有帮助.(事实上​​,我对此所做的研究表明,泄漏发生在 Webbrowswer 控件包装的非托管 COM 代码中.)
  • 最小化进程可用内存并将其设置为 -1、-1(SetProcessWorkingSetSize() 或类似.)只会减少物理内存使用量,对虚拟内存没有影响.
  • 调用 WebBrowser.Stop() 不是一种解决方案,它会破坏使用静态网页以外的任何内容的功能,而不仅仅是稍微减少泄漏.
  • 在导航到另一个文档之前强制等待一个文档完全加载也无济于事.
  • 在单独的 appDomain 中加载控件并不能解决问题.(我自己没有这样做过,但研究表明其他人在这条路线上没有成功.)
  • 使用不同的包装器(例如 csexwb2)无济于事,因为这也会遇到同样的问题.
  • 清除 Internet 临时文件缓存没有任何作用.问题出在活动内存中,而不是磁盘上.

当整个应用程序关闭并重新启动时,内存会被清除.

Memory is cleared when the entire application is closed and restarted.

我愿意直接在 COM 或 Windows API 中编写我自己的浏览器控件,如果这确实可以解决问题.当然,我更喜欢不那么复杂的修复;我宁愿避免下到较低级别来做事,因为我不想在浏览器支持的功能方面重新发明轮子.更不用说在自己的风格浏览器中复制 IE7 功能和非标准行为了.

I'm willing to write my own browser control in COM or Windows API directly, if that's a for-sure fix to the problem. Of course, I would prefer a less complicated fix; I'd rather avoid going down to lower levels to do things, because I don't want to be reinventing the wheel in terms of supported features of a browser. Letalone duplicating IE7 features and nonstandard behaviours in a roll-your-own style browser.

帮助?

推荐答案

此泄漏似乎是非托管内存中的泄漏,因此您在进程中所做的任何事情都不会回收该内存.从您的帖子中,我可以看到您已经尝试过非常广泛地避免泄漏但没有成功.

This leak appears to be a leak in unmanaged memory, so nothing you do in your process is going to reclaim that memory. From your post I can see that you've tried avoid the leak quite extensively and without success.

如果可行,我会建议采用不同的方法.创建一个单独的应用程序,使用 Web 浏览器控件并从您的应用程序启动它.使用描述的方法 在这里 将新创建的应用程序嵌入到您自己现有的应用程序中.使用 WCF 或 .NET 远程处理与该应用程序通信.不时重启子进程以防止它占用太多内存.

I would suggest a different approach if feasible. Create a separate application that uses web browser control and start it from your application. Use the method described here to embed newly created application within your own existing application. Communicate with that application using WCF or .NET remoting. Restart the child process from time to time to prevent it from taking to much memory.

这当然是相当复杂的解决方案,重启过程可能看起来很丑陋.您可能会在每次用户导航到另一个页面时重新启动整个浏览器应用程序.

This of course is quite complicated solution and the restart process could probably look ugly. You might maybe resort to restarting the whole browser application every time user navigates to another page.

这篇关于如何解决 .NET Webbrowser 控件中的内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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