C# webBrowser 怎么释放内存? 100
使用webBrowser打开网页后,调试的时候,发现占用的内存很大。关闭webBrowser后,内存还是很大。我尝试了:1、webBrowser1.Url=null2、w...
使用webBrowser打开网页后,调试的时候,发现占用的内存很大。
关闭webBrowser后,内存还是很大。
我尝试了:
1、 webBrowser1.Url = null
2、webBrowser1.Document.OpenNew(false)
3、webBrowser1.Dispose()
4、GC.Collect()
都没有效果,网上有一种方法是在线程里动态创建webBrowser,用完了再关闭线程。但是不知道怎么弄,谁有弄过,可否给个示例代码看一下。 展开
关闭webBrowser后,内存还是很大。
我尝试了:
1、 webBrowser1.Url = null
2、webBrowser1.Document.OpenNew(false)
3、webBrowser1.Dispose()
4、GC.Collect()
都没有效果,网上有一种方法是在线程里动态创建webBrowser,用完了再关闭线程。但是不知道怎么弄,谁有弄过,可否给个示例代码看一下。 展开
2个回答
展开全部
微软不认为这是内存泄露,但我们也得解决问题是不是。有一条思路你可以参考一下:
According to MSDN, The System.Windows.Forms.WebBrowser control is a managed wrapper for the ActiveX WebBrowser control, and uses whichever version of the control is installed on the user's computer.
You can find Dispose(bool) method in metadata of WebBrowser class(Press F12 in Visual Stuio) to release unmanaged resource.(NOT Dispose())
The code here
protected override void Dispose(bool disposing) {
if (disposing) {
if (htmlShimManager != null)
{
htmlShimManager.Dispose();
}
DetachSink();
ActiveXSite.Dispose();
}
base.Dispose(disposing);
}
But if you try to call WebBrowser.Dispose(bool), compiler error CS1540 is shown.
WebBrowser class supports Dispose(bool) method, BUT we can't use that.
I think WebBrowser class was designed by wrong way.
I have a idea to call WebBrowser.Dispose(true).
IT IS VERY SIMPLE! but it's not a good way.
Sample Code in here(3 Buttons, and 1 TextBox need)
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Test_20170308_01
{
public partial class Form1 : Form
{
[DllImportAttribute("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize);
public static void FlushMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
public Form1()
{
InitializeComponent();
}
private void addWeb()
{
WebBrowserD webBrowser1 = new WebBrowserD();
webBrowser1.Size = new Size(1070, 585);
this.Controls.Add(webBrowser1);
webBrowser1.Navigate("about:blank");
}
private void RemoveWeb()
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is WebBrowserD)
{
WebBrowserD web = (WebBrowserD)ctrl;
this.Controls.Remove(ctrl);
web.Navigate("about:blank");
web.Dispose(true);
FlushMemory();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
addWeb();
}
private void button2_Click(object sender, EventArgs e)
{
RemoveWeb();
}
private void button3_Click(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is WebBrowserD)
{
WebBrowserD axweb = (WebBrowserD)ctrl;
axweb.Navigate(textBox1.Text);
FlushMemory();
}
}
}
}
public class WebBrowserD : WebBrowser
{
internal void Dispose(bool disposing)
{
// call WebBrower.Dispose(bool)
base.Dispose(disposing);
}
}
}
This code can prevent Memory Leak.
In summary, You need just one class.
public class WebBrowserD : WebBrowser
{
internal void Dispose(bool disposing)
{
base.Dispose(disposing);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
this.webBrowser1.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
追问
都试过了,没有效果呀
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询