我的程序在c#中使用webbrowser,如果我更改了太多页面,我会遇到内存泄漏问题。我找到了一些解决方案,但如果我尝试这些解决方案,例如:
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
EmptyWorkingSet(GetCurrentProcess());
}
首先,内存恢复正常,但是当我更改为页面或使用webbrowser时,内存泄漏将从中断处恢复。我尝试了很多东西,但我找不到解决方案。
答案 0 :(得分:0)
此代码将释放类似于垃圾收集的内存。我发现将它添加到计时器以自动化该过程很有用。
[DllImport("KERNEL32.DLL", EntryPoint = "SetProcessWorkingSetSize", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
internal static extern bool SetProcessWorkingSetSize(IntPtr pProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);
[DllImport("KERNEL32.DLL", EntryPoint = "GetCurrentProcess", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
internal static extern IntPtr GetCurrentProcess();
private void button1_Click(object sender, EventArgs e)
{
IntPtr pHandle = GetCurrentProcess();
SetProcessWorkingSetSize(pHandle, -1, -1);
}