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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The 'using' statement vs GC memory management 2

Status
Not open for further replies.

iwease

IS-IT--Management
Sep 4, 2006
104
CA
Hi,

I've been reading a bit about the using statement.

ie

Code:
using (TextWriter w = File.CreateText("log.txt"))
{
    w.WriteLine("This is line one");
}

My understanding is that this code will call the Dispose() method of w at the end of the using block. None of my previous code has ever included the 'using' block since I just assumed the .NET garbage collector picks everything up and handles all my memory management. Am I wrong? Should I be using the 'using' statement?
 
The GC does clean up memory but that doesn't mean you should reply on the GC to handle every aspect of your memory usage and clean up. You should always call dispose.

You are correct on the using { }. From my understanding of it to date as well is it is nothing more than making the dispose() easier. Personally I have not used it though and like to manage that myself. Old school scared programmer that I'll have a memory lieak thing ;-)


[sub]____________ signature below ______________
I am Tedward Keyboardhands!!!
You are a amateur developer until you realize all your code sucks.
Jeff Atwood[/sub]
 
i use the [tt]using[/tt] blocks as much as possible. this why i don't need to worry about a [tt]finally[/tt] block in a try/catch if all i need to do is dispose of an object.
also variables declared within the block will not exist outside the block.

this is typically how my code would look.
Code:
try
{
   string string1;
   using (TextWriter w = File.CreateText("log.txt"))
   {
      string1 = "This is line one";
      string string2 = "This is line two";
      w.WriteLine(string1);
      w.WriteLine(string2);
   }
   //at this point string1 = This is line one
   //and string2 does not exist
}
catch((Specific Exception e)
{
   //handle exception
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
ok, two questions then.

1) my code in the past doesn't use using or dispose (because i relied on the GC). What is the penalty for not calling Dispose or using 'using'? Future code should use using/dispose but my old code is ok with the automatic garbage collection, right?

2) Dispose is just the same as a destructor in C++ right? Just deletes any created variables, etc?
 
1. this becomes an issue if the process is running in large loops, or if multiple users are accessing the routine in a small timespan. if a dozen users are using it sporadically, then you shouldn't have a problem.

2. i don't know. haven't programmed c++.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
2) yep

Christiaan Baes
Belgium

"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
ok, thanks everyone. I will continue to update my coding practices
 
1) my code in the past doesn't use using or dispose (because i relied on the GC). What is the penalty for not calling Dispose or using 'using'? Future code should use using/dispose but my old code is ok with the automatic garbage collection, right?
People usually report problems like "I just created that file, why can't I open it now for writing??" It's because they didn't dispose the File object used to create it, and NTFS thought they still had a write lock on it.

Using either the Using-block or the try-finally pattern (which is what the compiler creates behind the scenes for you) will save you from these hard-to-solve bugs.

2) Dispose is just the same as a destructor in C++ right? Just deletes any created variables, etc?
Yes and no. You can think of it as letting the garbage collector know that it's OK to free the memory. This will lower your memory footprint sooner, as well as make the GC's job easier.

The easiest way to determine if you need to implement IDisposable on your classes is to run CodeAnalysis (if you have TeamDeveloperEdition of VS.NET) or FxCop (if you have ProfessionalEdition or lower). It will tell you when you need to implement it, as well as lots of other (mostly!) useful info.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I work a LOT in the BusinessObjects SDK which is primarily wrappers around COM objects. Depending on the situation, I religiously use either using{} or try..finally with dispose() when I'm working with these objects because the garbage collection tends to leave the COM objects sitting around for a while and I want to make sure they're gone when I'm done with them to free up the resources and the connections to BO.

-Dell

A computer only does what you actually told it to do - not what you thought you told it to do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top