Click here to Skip to main content
15,889,877 members
Articles / Programming Languages / C#
Tip/Trick

Track Object Finalization

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
5 Jan 2011CPOL 20.2K   5   10
Make sure your objects are 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 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.

C#
#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

Execution of the code actually displaying the information is deferred until after the garbage collector has finished.

C#
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
      {
        // Executed on the GUI thread after GC has completed
        messagesTextBox.AppendText(message);
      }
    }
}

I hope that it might be useful.

License

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


Written By
Architect Sea Surveillance AS
Norway Norway
Chief Architect - Sea Surveillance 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.no

Comments and Discussions

 
GeneralReason for my vote of 5 Nice tip Pin
BillW334-Jan-11 6:18
professionalBillW334-Jan-11 6:18 
GeneralReason for my vote of 5 Thanks for sharing this tip. Pin
linuxjr1-Jan-11 14:19
professionallinuxjr1-Jan-11 14:19 
GeneralThanks Pin
Espen Harlinn1-Jan-11 0:41
professionalEspen Harlinn1-Jan-11 0:41 
GeneralReason for my vote of 5 Good Pin
Shahin Khorshidnia31-Dec-10 16:30
professionalShahin Khorshidnia31-Dec-10 16:30 
GeneralCarefull when you do this... Pin
msmits10-Jan-11 1:52
msmits10-Jan-11 1:52 
GeneralRe: Carefull when you do this... Pin
Espen Harlinn10-Jan-11 2:27
professionalEspen Harlinn10-Jan-11 2:27 
GeneralRe: Carefull when you do this... Pin
msmits10-Jan-11 3:01
msmits10-Jan-11 3:01 
Hi Espen,

Putting debug stuff inside compiler directives is a good thing indeed.

My point about the GC is that by adding a finalizer you are changing the GC behavior.

First, when GC detects your object is not used, it is put on a list for finalization, as well as 'upgrading' the generation (0-2) of the object.
A seperate thread will (at some undeterminable point) call the finalizer and mark the object as 'finalized'.
Later, when GC decides to do a higher generation cleanup (in other words, under memory pressure), it will find this object and free its memory.
This may be at a much later time.

So in short, just because the finalizer is called does not mean the object is cleaned up.
Sorry for this really short/blunt explanation, there's more to .NET memory management than this Wink | ;-)

If you're using .NET caches, they have a way of being notified when the cache is cleared.
The idea of these notifications is to check whether you want to extend their lifetime, but I guess logging should fit nicely in there as well.
Don't know if this might help you but I thought I mention it.

Cheers,
Michel
GeneralRe: Carefull when you do this... Pin
Espen Harlinn10-Jan-11 3:35
professionalEspen Harlinn10-Jan-11 3:35 
GeneralRe: Carefull when you do this... Pin
msmits10-Jan-11 3:43
msmits10-Jan-11 3:43 
GeneralRe: Carefull when you do this... Pin
Espen Harlinn10-Jan-11 3:52
professionalEspen Harlinn10-Jan-11 3:52 

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

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