I know it is not possible to know when a GC occurs, but there are factors that will tell you how often/when it may occur. What factors are these? One is how many objects are created, etc.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The short answer is that the following events trigger a collection cycle:
Allocation exceeds Gen0 threshold
Collection for a specific generation occurs when the memory threshold for that generation is hit. In the implementation of .NET Version 1.0, the initial thresholds for generations 0, 1, and 2 are 256 kilobytes (KB), 2 megabytes (MB), and 10 MB, respectively. Note that the GC can adjust these thresholds dynamically based on an application’s patterns of allocation. Objects larger than 85 KB are automatically placed in the large object heap.
System.GC.Collect()is calledAllocations only happen in Gen0. After each GC, Gen0 is empty. New allocations will fill up Gen0 and the next GC will happen, and so on. The problem with calling GC.Collect() manually, is that you can end up with it being called more often than you predicted (due to the CLR calling it as well) and performance goes down because you are triggering GC cycles ahead of their schedule.
System is in a low memory situation
This is affected by other processes on the system which means you really have no control over it other than ensuring that you clean up resources correctly in your processes and components.