0

我已经从一个窗口导航到另一个模式窗口。有 10 个可观察的集合。关闭窗口后,我将 null 设置为所有可观察的集合。但是在任务管理器中,内存并没有减少。当我打开模态窗口时,增加了 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 诊断工具中的内存消耗。

4

1 回答 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()

于 2019-04-25T05:14:22.013 回答