在使用wpf prism中的dispose方法调用后,内存不会被释放

问题描述 投票:0回答:1

我已经从一个窗口导航到另一个模态窗口。有10个可观察的集合。关闭窗口后,我将null设置为all observable集合。但在任务管理器中,内存不会减少。当我打开模态窗口时,25 mb增加但当我关闭窗口时,只有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 Diagnostic Tools中的内存消耗。

c# wpf memory-leaks prism idisposable
1个回答
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()

© www.soinside.com 2019 - 2024. All rights reserved.