I have a WPF application that allows the user to open, close and reopen an arbitrary amount of child windows.
Using VS2015's integrated memory profiler I found out that a certain type of child window, let's call it ProblematicChildWindow, always stays in memory after having been closed, so its instances add up in case of frequent close and reopen actions. Unfortunately I cannot reproduce the problem in a minimal application (everything works fine there).
I know about WPF's common memory leak situations, and I came to the conclusion that the profiler must be wrong. So I added the following code to the problematic class:
~ProblematicChildWindow()
{
using (StreamWriter sw = File.CreateText("d:\\garbagecollection.txt"))
{
sw.WriteLine(DateTime.Now.ToShortTimeString() + " garbage collected.");
}
}
Now the strange thing is:
After opening and closing an instance of ProblematicChildWindow and forcing a garbage collection, I find a new file "garbagecollection.txt" in my D:\ folder, BUT
VS2015's memory profiler tells me that there is still an instance of ProblematicChildWindow in memory!
My question: by the fact the instance has obviously been garbage collected (because the finalizer has been called), can I be sure that the occupied memory will be released?
VS2015's memory profiler seems to have a bug in this case.
UPDATE: .NET Memory Profiler tells me that there is no instance left in memory after closing. But VS2015 does (just checked it again).