我已经从一个窗口导航到另一个模式窗口。有10个可观察的集合。关闭窗口后,我将null设置为所有可观察的集合。但是在任务管理器中,内存不会减少。当我打开模式窗口时,会增加25 mb,但是当我关闭窗口时,在处理所有可观察到的集合后,只会减少1 mb或2 mb。
private bool disposedValue = false;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
Collection1 = null;
Collection2 = null;
Collection3 = null;
Collection4 = null;
Collection5 = null;
}
disposedValue = true;
}
}
请建议我我做错了什么。请分享您的宝贵建议。我还检查了Visual Studio诊断工具中的内存消耗。
答案 0 :(得分:0)
不确定负面因素,但可以肯定
public class MemoryManagement
{
/// <summary>
/// Clear un wanted memory
/// </summary>
public static void FlushMemory()
{
try
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
catch (Exception e)
{
}
}
/// <summary>
/// set process working size
/// </summary>
/// <param name="process">Gets process</param>
/// <param name="minimumWorkingSetSize">Gets minimum working size</param>
/// <param name="maximumWorkingSetSize">Gets maximum working size</param>
/// <returns>Returns value</returns>
[DllImportAttribute("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet =
CharSet.Ansi, SetLastError = true)]
private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize);
}
在Dispose中调用MemoryManagement.FlushMemory()