0

I recently created a new WPF application having a loop in MainWindow.xaml that creates a new object of another page (Test.xaml) and creates a dialog of Test.xaml page. In Test.xaml i added a Textblock and updated the Text property on Loaded event and closing the page immediately.

Now when i ran the code, i saw that the memory used by this application is increasing continuously. Why is this really happening?

I am already disposing previous object and rewriting with new object, still it is increasing memory because of previous object.

What could be the reason behind it? How can i remove this behavior?

I tried creating the memory dump of the application to analyze memory leaks, there i saw that Hashtable, DependencyProperty and EventHandler are using the most of the memory.

MainWindow.xaml

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        int counter = 0;
        Test test = null;

        do
        {
            test = null;
            test = new Test();

            test.ShowDialog();
        } while (++counter != 5000);

        test = null;
    }

Test.xaml

    private void Test_Loaded(object sender, RoutedEventArgs e)
    {
        MyTextBox.Text = "Testing leaks";
        Close();
    }

My expectation is the object should be disposed of totally and should not contribute in memory increase.

Stefan
  • 17,448
  • 11
  • 60
  • 79
  • How much memory is it using? While you should `Dispose`, the Garbage Collector will eventually come around and clean up things that are out of scope when it gets enough memory pressure. Do don't this in production, but temporarily for understanding try adding a `GC.Collect()` every 100 iterations or so to see what happens. – Crowcoder May 19 '19 at 13:47
  • GC.Collect is not doing anything and the base memory used was ~40mb, that increased to 62mb till 500 iterations. – Navjot Singh May 19 '19 at 13:51
  • That's nothing, try something that uses significant memory. Like maybe give the form a property that is a byte[] array initialized to 1MB or something like that. – Crowcoder May 19 '19 at 13:57
  • Take a look at [Fighting Common WPF Memory Leaks with dotMemory](https://blog.jetbrains.com/dotnet/2014/09/04/fighting-common-wpf-memory-leaks-with-dotmemory/). – Clemens May 19 '19 at 14:31

1 Answers1

2

.NET is using a garbage collector that runs once in a while. When you set the object to null, you effectively only remove the reference to the object, so it can be collected during the next garbage collection. Because your program doesn't do a lot of allocations and there is no memory pressure, I don't think the garbage collector will be run.

Resetting a variable to null is generally not necessary, because the reference is also lost when the variable gets out-of-scope. You may want to give JetBrains DotMemory a try and you'll see that the application will have a relative constant memory usage after the initial load.

Don't inject GC.Collect() in your code, because the garbage collector generally has good heuristics to know when it needs to be called.

Ramon de Klein
  • 5,172
  • 2
  • 41
  • 64
  • i ran it for some time and the memory went to 102MB, initial load was at ~40MB. Will try with JetBrains DotMemory too. – Navjot Singh May 19 '19 at 14:04