Reputation:
I read many things about garbage collection like it's generation, scope etc but want to know when does the garbage collection gets triggered ? an example will be really helpful if possible.
Thanks,
Upvotes: 15
Views: 16727
Reputation: 16983
Garbage collection occurs when one of the following conditions is true:
GC.Collect
method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.Source: Fundamentals of garbage collection - Conditions for a garbage collection
Upvotes: 21
Reputation: 11
AppDomain
unloads from memoryGC.Collect()
calledUpvotes: 1
Reputation: 62265
You are not in control of GC
and can not reliably predict its behavior.
All calls, like GC.Collect
are simple messages to VM to start collection, but that does not mean that collection will eventually start right after the line.
Upvotes: 0