Frequent Events and Garbage Collection

When an event will be fired many times it is a good practice to do periodic garbage collection.  When a VSTA add-in hooks into an event which is fired many times this becomes even more important because the memory usage for the host process will grow very high without periodic garbage collection. 

 

The charts below demonstrate that when a VSTA add-in is hooked into an event the memory usage grows.  This is case even if the VSTA add-in does not perform any actions when the event is fired (see the "Empty VSTA Event Hooked-up" line in Chart 1).  If the VSTA add-in does perform actions when the event is fired the memory usage will grow even faster (see the "VSTA Event Hooked-up" line in Chart 1).  When GC.Collect is called periodically this increase in memory usage is almost completely negated; notice in Chart 1 the base line "No VSTA Event Hooked-up" is at the same memory usage levels as the lines where garbage collection is called periodically ("VSTA Event Hooked-up & Host call GC every 50 event fires" and "VSTA Event Hooked-up & Host call GC every 500 event fires").  These three lines, the base and lines where periodic garbage collection is done, are displayed again in Chart 2 to show how close the memory usage levels are. 

Chart 1- Memory Usage Levels With and Without Periodic Garbage Collection

 

Chart 2- Memory Usage Levels With Periodic Garbage Collection 

This applies to both VSTA v 1 and VSTA v 2.


Posted Jun 17 2009, 10:46 AM by Melody
Filed under: , ,
Copyright Summit Software Company, 2008. All rights reserved.