Click here to Skip to main content
Click here to Skip to main content

Track object finalization

By , 5 Jan 2011
Rate this:
Please Sign up or sign in to vote.
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 information 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);
  }
 
#endif
 
Exceution of the code actually displaying the information is deferred until after the garbage collector has finished.
 
class MainForm : Form
{
  private static MainForm instance;
  
  public MainForm()
  {
  ....
    instance = this;
  }
 
  public static MainForm Instance
  {
    get
    {
      return instance; 
    }
  }
 
  public static void LogString(string s)
  {
    MainForm mainForm = Instance;
    if( mainForm != null )
    {
      Instance_MessageLogged(mainForm,s);
    }
  }
 
delegate void Instance_MessageLoggedDelegate(object sender, string message);
 
  void Instance_MessageLogged(object sender, string message)
    {
      if (InvokeRequired)
      {
        BeginInvoke(new Instance_MessageLoggedDelegate (Instance_MessageLogged), new object[] { sender, message });
      }
      else
      {
        // Excuted on the GUI thread after GC has completed
        messagesTextBox.AppendText(message);
      }
    }
}
 
In the hope that it might be useful,
Espen Harlinn

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Espen Harlinn
Architect Goodtech Projects & Services AS
Norway Norway
Principal Architect - Goodtech Projects & Services AS.
 
Specializing in integrated operations and high performance computing solutions.
 
I’ve been fooling around with computers since the early eighties, I’ve even done work on CP/M and MP/M.
 
Wrote my first “real” program on a BBC micro model B based on a series in a magazine at that time. It was fun and I got hooked on this thing called programming ...
 
A few Highlights:
  • High performance application server development
  • Model Driven Architecture and Code generators
  • Real-Time Distributed Solutions
  • C, C++, C#, Java, TSQL, PL/SQL, Delphi, ActionScript, Perl, Rexx
  • Microsoft SQL Server, Oracle RDBMS, IBM DB2, PostGreSQL
  • AMQP, Apache qpid, RabbitMQ, Microsoft Message Queuing, IBM WebSphereMQ, Oracle TuxidoMQ
  • Oracle WebLogic, IBM WebSphere
  • Corba, COM, DCE, WCF
  • AspenTech InfoPlus.21(IP21), OsiSoft PI
 
More information about what I do for a living can be found at: harlinn.com or LinkedIn
 
You can contact me at espen.harlinn@goodtech.no

Comments and Discussions

 
GeneralReason for my vote of 5 Nice tip PinmemberCIDev4-Jan-11 6:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140421.2 | Last Updated 5 Jan 2011
Article Copyright 2010 by Espen Harlinn
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid