Tuesday, January 4, 2011

How to get an object garbage collected

Sometimes it's useful to detect when an object is garbage collected.
Garbage collection happens in a separate thread, and if you want to display some info about this,
in a Windows Forms GUI, you need to use BeginInvoke and not Invoke or your application will "freeze".

Invoke will not be executed during garbage collection.



#if DEBUG private static long objectsDestroyedCounter = 0;
~Entity( )
{
objectsDestroyedCounter++;
if( (objectsDestroyedCounter % 10000) == 0)
{
LogDebug(string.Format("{0} objects dropped."
,objectsDestroyedCounter) );
}
}
private static void LogDebug(string s)
{
MainForm.LogString(s);
}
 

No comments :

Post a Comment