在 wpf prism 中调用 dispose 方法后内存未释放
Memory is not released after dispose method is calling in wpf prism
我已经从一个 window 导航到另一个模式 window。有10个可观察集合。关闭 window 后,我已将 null 设置为所有可观察集合。但是在任务管理器中内存并没有减少。当我打开模态 window 时,增加了 25 mb,但是当我关闭 window 时,只有 1mb 或 2mb 在处理完所有可观察到的集合后才减少。
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 诊断工具中检查了内存消耗。
不确定消极面,但肯定会工作
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);
}
Call MemoryManagement.FlushMemory() in the Dispose
我已经从一个 window 导航到另一个模式 window。有10个可观察集合。关闭 window 后,我已将 null 设置为所有可观察集合。但是在任务管理器中内存并没有减少。当我打开模态 window 时,增加了 25 mb,但是当我关闭 window 时,只有 1mb 或 2mb 在处理完所有可观察到的集合后才减少。
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 诊断工具中检查了内存消耗。
不确定消极面,但肯定会工作
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);
}
Call MemoryManagement.FlushMemory() in the Dispose