Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Garbage Collection Question

Status
Not open for further replies.

keyser456

IS-IT--Management
Nov 21, 2003
73
US
Ok. What makes something qualify for Garbage Collection? I ask because I'm worried the events I'm hooking up might be keeping the objects from being disposed of.

You tell the program you're interested in an event by using the += like so:

this.OnClick += new EventHandler(this_Clicked);

Assume "this" is a control that is part of a form's Controls collection. When I clear the form's Controls collection is this control disposed of? I believe it is waiting for garbage collection, but do I have to "unhook" the event (using -=) before the control qualifies for garbage collection, or is the CLR smart enough to figure out I won't ever be using that control again? I'm worried the control (now removed from the controls collection) is sitting there isolated in memory watching for an event that will never happen.
 
Hi Keyser

The this keyword refers to the current instance of a class. In effect it is the Form rather than a control within it. It can be used to fully qualify a property or to pass an instance in a method call. e.g. (From .NET Framework SDK)
Code:
using System;
public class Employee 
{
   public string name;
   public string alias;
   public decimal salary = 3000.00m;

   // Constructor:
   public Employee(string name, string alias) 
   {
      // Use this to qualify the fields, name and alias:
      this.name = name;
      this.alias = alias;
   }

   // Printing method:
   public void printEmployee() 
   {
      Console.WriteLine("Name: {0}\nAlias: {1}", name, alias);
      // Passing the object to the CalcTax method by using this:
      Console.WriteLine("Taxes: {0:C}", Tax.CalcTax(this));
   }
}
public class Tax 
{
   public static decimal CalcTax(Employee E) 
   {
      return (0.08m*(E.salary));
   }
}

public class MainClass 
{
   public static void Main() 
   {
      // Create objects:
      Employee E1 = new Employee ("John M. Trainer", "jtrainer");

      // Display results:
      E1.printEmployee();
   }
}
Output
In terms of Garbage collection it will be cleaned up when the CLR has finished with the current instance (well my understanding of garbage collection is a little different - I believe that objects are only cleaned away when there is no space left in the garbage stack for a new object rather than as soon as they're not required?). You do not need to unhook the event for this to take place.

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
That's my point. When exactly is the CLR finished with the current instance? I know garbage collection is handled automatically so you never really know when it will occur unless you force it to occur. I'm just wondering if I should be manually disposing of objects? I have a form that's using a control that is acting like a subform. This subform creates a dynamic # of instances of a control which, we'll call SubformRecord, based on what record you are currently using. Each of these SubformRecords have TextBoxes in their Controls collection which need to watch for the Validate event. When I switch records on the main form, currently all I'm doing to get rid of the SubformRecords is using something to the effect of Subform.Controls.Clear(). Supposing there were 5 or 6 SubformRecords, those controls now disappear and I have no way of accessing them anymore, but are they still watching for their TextBox Validate events? It is not good enough to say it will all be garbage collected when the form or even the entire is closed, because I want to do this right. Some records may have several hundred SubformRecords and I don't want those taking up memory when they aren't needed anymore.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top