I am studying about the garbage collector of .NET and wanted to know if there is an event I can sign up to and always know when a collection will be made.
I am studying about the garbage collector of .NET and wanted to know if there is an event I can sign up to and always know when a collection will be made.
Event does not even have it, this would be of little use since at the moment you need to collect it, nothing else can be done.
What you can do is to be notified before an allocation is made, it is not something simple and you need to mount your own event system preferably in another thread . This can be seen at GC.RegisterForFullGCNotification
.
This way you can tell when a large allocation will be made and possibly generate a collection and then decide what to do. This is useful for testing, but uninteresting in production because you have overhead . It has application that notifies a load balance to not send more requests to it until the collection is finished, so another instance or another server can continue to meet the demand without pauses. I see no use in anything very different from this.
In the GC
class documentation you have several things well advanced about using GC. The configuration and monitoring of .NET GC is not the best, but it has some good things. Outside of this you can use tools that help diagnose memory issues.