Click here to Skip to main content
12,820,585 members (28,128 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 31 Dec 2010

Track object finalization

, 5 Jan 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

  private static long objectsDestroyedCounter = 0;
  ~Entity( )
    if( (objectsDestroyedCounter % 10000) == 0)
      LogDebug(string.Format("{0} objects dropped.",objectsDestroyedCounter) );

  private static void LogDebug(string s)


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
      return instance; 

  public static void LogString(string s)
    MainForm mainForm = Instance;
    if( mainForm != null )

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 });
        // Excuted on the GUI thread after GC has completed

In the hope that it might be useful,
Espen Harlinn


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 Powel AS
Norway Norway
Chief Architect - Powel 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: or LinkedIn

You can contact me at

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 5 Nice tip Pin
CIDev4-Jan-11 7:18
memberCIDev4-Jan-11 7:18 
GeneralReason for my vote of 5 Thanks for sharing this tip. Pin
linuxjr1-Jan-11 15:19
memberlinuxjr1-Jan-11 15:19 
GeneralThanks Pin
Espen Harlinn1-Jan-11 1:41
mvpEspen Harlinn1-Jan-11 1:41 
GeneralReason for my vote of 5 Good Pin
Shahin Khorshidnia31-Dec-10 17:30
memberShahin Khorshidnia31-Dec-10 17:30 
GeneralCarefull when you do this... Pin
msmits10-Jan-11 2:52
membermsmits10-Jan-11 2:52 
GeneralRe: Carefull when you do this... Pin
Espen Harlinn10-Jan-11 3:27
memberEspen Harlinn10-Jan-11 3:27 
GeneralRe: Carefull when you do this... Pin
msmits10-Jan-11 4:01
membermsmits10-Jan-11 4:01 
GeneralRe: Carefull when you do this... Pin
Espen Harlinn10-Jan-11 4:35
memberEspen Harlinn10-Jan-11 4:35 
>> My point about the GC is that by adding a finalizer you are changing the GC behavior.

>> ...
>> This may be at a much later time.
And? Purpose of tip was to show that it is possible to send info from finalizer to WinForms based UI - using BeginInvoke and not Invoke.
>> there's more to .NET memory management than this
Yes - several books worth of material
>> ... check whether you want to extend their lifetime
No, I don't. If an entity representation is available on the client, but no longer referenced by anything outside the client enity manager I want it to go away during garbage collection. If code references an entity that has not been garbage collected through another entity, the solution uses the existing entity. Entites are synchronized using MSMQ ... This is from a Distributed ORM, a "little" solution I've been working on, some 35 MB of c# code ..., leveraging among other things, a number of proprietary code generators. So far test shows that the solution outperforms MS Entity Framework, which is not a multi-tier technology. The tests involves several years worth of process information from one of the larger oil-fields in the north-sea - i.e. it isn't a fluke Smile | :)

But, thanks all the same Smile | :)
Espen Harlinn
GeneralRe: Carefull when you do this... Pin
msmits10-Jan-11 4:43
membermsmits10-Jan-11 4:43 
GeneralRe: Carefull when you do this... Pin
Espen Harlinn10-Jan-11 4:52
memberEspen Harlinn10-Jan-11 4: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.

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