How can I know when GC starts? Can I give to GC some delegates for such events like “OnStart” and “OnEnd”?
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.
If you really want to to when GC happens you can create an object which has a finalizer and immeditaly set the reference to it to null. This way, the object if still generation 0 and will likely garbage collected the next time the GC runs. After the GC is done, your finalizer will probably be called, so you know that GC has happened.
Note that I doesn’t recommend this and that behaviour is not guaranteed.
If you just want to know when GC runs for curiosity and educational purposes you can use a clr memory profiler tool which usually shows you when and how often GC happens and which objects get collected.