Is it instantaneous after the GC runs?
I was monitoring Memory (Available MBytes) using perfmon while running an application I'm working on. As I loaded files I could see the Available MBytes going down as expected, but as I closed them it didn't go back up so I'm wondering if I am not disposing correctly or if there is something else that affects this.
When a file is loaded:
- the contents are stored in some objects
- a new instance of a UserControl is created
- a new TabPage is created
- the UserControl is added to the TabPage
- the TabPage is added to the TabControl
Reading on SO I saw that one of the most common things that can cause Memory Leaks is not unsubscribing from event handlers and so I made sure I did that in my UserControl's dispose method. I subscribe to the 'RemovingTab' event of the TabControl but the TabPages themselves have no attached handlers.
When closing a file, I remove stored instances of the objects and call dispose on both the UserControl and the TabPage. I tried running the CLR profiler (which I haven't had any experience with) and it said there was no GC runs.
No. As a matter of fact, depending on the memory pressure of your particular application, the
GC
might not free up any memory during the whole lifespan of your application.Yes that is a common cause.
The
GC
is very good at doing what it's supposed to do and it has been fined tuned to extremes. If it doesn't run its probably because it has decided that it doesn't need to run. Therefore, when the memory is freed up by theGC
is up to theGC
to decide and, if I'm not mistaken, is implementation defined, so it is not something you can rely upon.And last but not least, the
IDisposable
pattern is not, by any means, a deterministic pattern for reclaiming memory; that is most definitely not the pattern's goal.