0

I have navigating from one window to another modal window. There are 10 observable collection. After closing the window, i have set null to the all observable collection. But in task manager the memory is not reduced. When I open the modal window 25 mb is increased but when i close the window only 1mb or 2mb only reduced after disposing all observable collection.

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;
   }
}

Please suggestion me i have did anything wrong. Please share your valuable suggestion. I have also checked the memory consumption in visual studio Diagnostic Tools.

DotNetUser
  • 415
  • 3
  • 18
  • 3
    When you set the collections to null (and that was the last reference to them) the garbage collector **is able** to release the memory. Thats all. But the garbage collector will only do this when memory pressure is high or if you force it to collect (you should do this only for debug reasons) – Sir Rufo Apr 25 '19 at 04:56
  • 1
    use this [thread](https://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface) with lots of well explained examples related to this – sujith karivelil Apr 25 '19 at 04:58

1 Answers1

0

Not sure about the negative side , but work for sure

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

Justin CI
  • 2,693
  • 1
  • 16
  • 34
  • Cl, The above code is clears the memory, but I have implemented dispose method to plenty of viewmodel in my application. But I don't know what type of negative will cause the above code. Please anyone suggest me to demerits of above code. – DotNetUser Apr 25 '19 at 05:36
  • https://stackoverflow.com/questions/8292229/pros-and-cons-of-using-setprocessworkingsetsize – Justin CI Apr 25 '19 at 05:49