Reputation: 1654
I'm trying to make sure instances of a certain class get released, by using console.WriteLine() in the destructor, but the output never shows up.
I have carefully searched for any lingering references, as well as event subscriptions and not finding any. Just for my own sanity, and before I continue my search, can someone confirm that:
GC.Collect();
GC.WaitForPendingFinalizers();
Will force a complete reclaim, no matter how small the objects are?
Upvotes: 6
Views: 4810
Reputation: 564363
In general, this should clean up most things.
However, if you have code in your finalizers, it's possible that you will need to call GC.Collect()
twice, as the first time will cause the finalizers to execute, but the actual memory cannot be cleaned until after the finalizer has completed, which means the subsequent call will catch the object.
Upvotes: 6