积极答复者
WebBrowser 在多线程中,无法释放内存?

问题
-
public partial class Form2 : Form { public Form2() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); webBrowser1.Navigate("http://www.qq.com/"); } protected override void OnClosed(EventArgs e) { base.OnClosed(e); webBrowser1.Dispose(); webBrowser1 = null; } } /////////////////////// public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void timer1_Tick(object sender, EventArgs e) { Thread thread = new Thread(new ThreadStart(delegate { Form2 f = new Form2(); System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); timer.Interval = 2000; timer.Tick += delegate(object o, EventArgs args) { timer.Dispose(); f.Close(); }; timer.Start(); f.ShowDialog(); Thread.CurrentThread.Abort(); })); thread.IsBackground = true; thread.SetApartmentState(ApartmentState.STA); thread.Start(); } }
以上代码运行后,每隔两秒自动打开Form2并关闭。在这个过程中我们观察任务管理器中的内存使用情况。会发现内存和线程数量不断增加无法释放。请问该如何解决????紧急!!!
--------------------------------
该问题我已在猪八戒高价发帖悬赏了
http://task.zhubajie.com/1919114/
非常急迫~ 请大家帮忙~!!
答案
-
您好,
您可以试用「redgate ANTS Memory Profiler」看一下那些物件沒有release。
可参考一下以下的帖子 Call MemoryManagement.FlushMemory();,谢谢!
http://stackoverflow.com/questions/6147923/webbrowser-memory-problem
public class MemoryManagement { [DllImport("kernel32.dll")] public static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max); public static void FlushMemory() { GC.Collect(); GC.WaitForPendingFinalizers(); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1); } } }
以上說明若有錯誤請指教,謝謝。
亂馬客blog: http://www.dotblogs.com.tw/rainmaker/- 已编辑 亂馬客 2012年8月8日 15:40
- 已建议为答案 Lisa ZhuModerator 2012年8月13日 4:50
- 已标记为答案 Lisa ZhuModerator 2012年8月14日 10:20
全部回复
-
您好,
您可以试用「redgate ANTS Memory Profiler」看一下那些物件沒有release。
可参考一下以下的帖子 Call MemoryManagement.FlushMemory();,谢谢!
http://stackoverflow.com/questions/6147923/webbrowser-memory-problem
public class MemoryManagement { [DllImport("kernel32.dll")] public static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max); public static void FlushMemory() { GC.Collect(); GC.WaitForPendingFinalizers(); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1); } } }
以上說明若有錯誤請指教,謝謝。
亂馬客blog: http://www.dotblogs.com.tw/rainmaker/- 已编辑 亂馬客 2012年8月8日 15:40
- 已建议为答案 Lisa ZhuModerator 2012年8月13日 4:50
- 已标记为答案 Lisa ZhuModerator 2012年8月14日 10:20