Re: garbage collection of open resources
From: mikeb (mailbox.google_at_nospam.mailnull.com)
Date: 04/22/04
- Next message: Nicholas Paldino [.NET/C# MVP]: "Re: Exceptions and performance hit"
- Previous message: Mario Reiley: "Re: Applications Server from Microsoft"
- In reply to: John Wood: "Re: garbage collection of open resources"
- Next in thread: John Wood: "Re: garbage collection of open resources"
- Reply: John Wood: "Re: garbage collection of open resources"
- Reply: Mark Broadbent: "Re: garbage collection of open resources"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 22 Apr 2004 09:38:08 -0700
John Wood wrote:
> Eventually you might run out of resources. In this case two things could
> happen:
> 1. When you next go to use that file, it may say that it's locked or already
> open.
> 2. Opening a new file will might throw an exception stating that there are
> too many open files.
>
> The GC does dispose of all objects eventually, and even if you set the
> reference to null and forget to close the file handle, the file stream
> itself implements IDisposable and will close the file for you. Just that it
> won't happen until potentially much later, leaving the option for open
> handles to accumulate.
Another thing to be aware of is that on application shutdown finalizers
are not necessarily called. So, the file close will occur (as the OS
does this on process shutdown) but any buffered data might not be
flushed to the file.
Compile and run the following with no command line arguments to see this
behavior (run with "dispose" or "close" on the command line to get the
data flushed):
//==========================================================
using System;
using System.IO;
using System.Text;
public class MyClass
{
public static void Main(string[] args)
{
string arg = null;
if (args.Length > 0) {
arg = args[0].ToLower();
}
StreamWriter sw = new StreamWriter( @"c:\temp\swtest.out");
sw.WriteLine( "Does this data actually make it into the file?");
sw.Write( "It depends...");
if (arg != null) {
switch (arg) {
case "dispose":
((IDisposable) sw).Dispose();
break;
case "close":
sw.Close();
break;
default:
// do nothing
break;
}
}
}
}
//==========================================================
>
> "Mark Broadbent" <no-spam-please@no-spam-please.com> wrote in message
> news:u$n1$NIKEHA.3916@TK2MSFTNGP10.phx.gbl...
>
>>Everything I have read suggests that if I open files or database
>
> connections
>
>>or the like, that I should explicitly close those said resources before
>>making the object subject to GC. This is obviously good programming
>>technique.
>>
>>However what happens if this practice is not followed. e.g.
>>that I have a base object that creates an instance of a filestream and for
>>arguements sake an instance of a oledb connection.
>>Both sub objects are opened (one to a file and one to a db) and used to
>>read/ write data.
>>
>>Say for instance that I implement the IDisposible interface for the parent
>>class definition (that instanciates both objects) and within the Dispose()
>>method I set both object variable references to null (without closing
>
> them).
>
>>I am under the impression that they will be made available to the GC.
>>
>>My question is - could data corruption occur to any open data sources that
>>have been made available to garbage collection OR does (as I suspect) it
>>simply means that those open resources (open files) will simply be locked
>>out until the GC destroys them?
>>
>>Obviously I know that there are several reasons for closing the resources
>>but I would like to know that corruption is not one of them.
>>
>>
>>--
>>
>>Br,
>>Mark Broadbent
>>mcdba , mcse+i
>>=============
>>
>>
>
>
>
-- mikeb
- Next message: Nicholas Paldino [.NET/C# MVP]: "Re: Exceptions and performance hit"
- Previous message: Mario Reiley: "Re: Applications Server from Microsoft"
- In reply to: John Wood: "Re: garbage collection of open resources"
- Next in thread: John Wood: "Re: garbage collection of open resources"
- Reply: John Wood: "Re: garbage collection of open resources"
- Reply: Mark Broadbent: "Re: garbage collection of open resources"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|