I’m writing HFT trading application. I’m trying to optimize my application to minimize number of objects that garbage collected.
Example of optimization: instead of calling new Instrument(ticker) many times I create HashSet<string /*ticker*/, Instrument>
So it would be interesting to have such statistics: when application is finished I want to know how many objects were garbage collected during execution. So latter I can compare this number with “optimized” version to check that “optimization” is actually works.
If it is not possible to have such statistics then how can I test if my optimizations works i.e. actually decrease GC use?
How to count disposed objects is depending on your implementation but you can get
GC Notifications.Garbage Collection Notifications
This introduced with GC in .NET 3.5 SP1 to produce notifications whenever GC collection is about to begin and GC collection is completed successfully. So if you are between a very resource intensive phase of your application, GC notification will allow you to get notified that GC is approaching, so that you could stop the current process and wait for GC to complete. This makes your application to run smoothly.
The steps for getting GC notifications:
GC.RegisterForFullGCNotificationto allow for notifications when GC is approaching.GC.WaitForFullGCApproachand/orGC.WaitForFullGCCompletemethods.GCNotificationStatus.Succeededwhen the notification has to be raised.GC.CancelFullGCNotificationto unregister the notification process.Sample Code Implementations
Reference: Garbage Collection Notifications in .NET 4.0