Re: garbage collection of open resources

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: mikeb (mailbox.google_at_nospam.mailnull.com)
Date: 04/22/04


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


Relevant Pages

  • garbage collection of open resources
    ... Everything I have read suggests that if I open files or database connections ... My question is - could data corruption occur to any open data sources that ... simply means that those open resources will simply be locked ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ObjectDisposedException
    ... is that I receive a resize event for a form that is already closed. ... I do nothing special to release any resources. ... Dispose() after I come out of the ShowDialogmethod, ... still exists after closing? ...
    (microsoft.public.pocketpc.developer)
  • Re: Pythons "only one way to do it" philosophy isnt good?
    ... I don't rely on the refcounter for resources that ABSOLUTELY, ... Open files, for instance, rarely do. ... I actually want access to them in the traceback for debugging ... If a procedure catches an exception but isn't going to return ...
    (comp.lang.python)
  • Re: maximum number of threads
    ... Gabriel Genellina wrote: ... as you can't have 10000 open files at once. ... resources are not infinite. ...
    (comp.lang.python)
  • Re: maximum number of threads
    ... Gabriel Genellina wrote: ... as you can't have 10000 open files at once. ... resources are not infinite. ...
    (comp.lang.python)