Re: FileStream.Close() & GarbageCollection - Memory Leak Question



Tom wrote:
When I use a FileStream variable's null status to determine logic flow
I can get an exception thrown because the variable is not set to null
within the FileStream.Close() method. I could use try{}, catch{}
blocks to trap out the error ... but I need to understand if
programmatically setting the variable to null creates problems in the
GarbageCollection.

i.e. >>

FileStream fs = new FileStream("C:/testfile.txt", FileMode.Open,
FileAccess.Read);

if (fs == null) Console.WriteLine("fs == null");
else Console.WriteLine("fs.Lenth = {0}", fs.Length.ToString());

fs.Close();

fs = null; // << Does this produce a memory leak?

Not at all. It just clears the contents of the variable. Somewhere in memory the memory allocated to the object still resides, pending a future garbage collection.


// Without the above statement an exception is thrown below.

// Replacing above statement with >> GC.Collect();
// Produces the same error.

if (fs == null) Console.WriteLine("fs == null");
else Console.WriteLine("fs.Lenth = {0}", fs.Length.ToString());

If you don't clear the variable, then yes, this will throw an exception. Setting it to null is the correct way.


The GC forcing produces the same error condition. The fs variable is
retaining a no longer valid reference? Seems the Close() method
"should" null the variable?

No, it should not, and cannot.

Close() does not free the memory allocated to the object. What Close() and Dispose() (of IDisposable, you should look it up) does is to close unmanaged resources, like file handles, tcp/ip connection handles, etc.

The memory allocated to the object is still allocated, still referenced by your variable.

However, the object has specific safeguards in place that will throw an exception if you try to use properties or methods that needs the file to still be open.

If you need to use the variable itself to tell the rest of the program wether you have the file or not, clear it, like you've shown.

Close or Dispose does not null the variable because they simply don't have access to the variable.


Does programmatically setting fs = null create a memory leak? I am
concerned the GarbageCollector uses the reference and by nulling it I
am creating a problem?

No, it does not.

The garbage collector will always be able to find all the objects. It is on the other hand the lack of live references to allocated memory that the garbage collector figures out what to collect.

A live reference is not necessarily a variable holding a reference to the memory. The variable also has still have a life, otherwise the garbage collector may still collect the memory.

--
Lasse Vågsæther Karlsen
mailto:lasse@xxxxxxxxxxx
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
.



Relevant Pages

  • Re: Memory Cleanup
    ... Calling GC.Collect is not the ... // The total memory has not gone down. ... reference counters, and nothing happens when objects go out of scope. ... objects just remain in memory waiting for the garbage collector to remove ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Script mysteriously stops executing...
    ... memory leaks...that seems to be the case. ... Standard PHP does not have a garbage collector per se - ... These are for the cyclic reference garbage collector - this is a far ...
    (comp.lang.php)
  • Re: arrays = pointers?
    ... references, that means that updating a reference is perhaps twice as efficient in .NET as in an environment that uses for example reference counting. ... I never once brought up the question of "reference counting", nor do I find it relevant to the discussion I'm participating in. ... This is WAY faster than .NET running through all of the data structures in the memory manager, finding those that refer to a given object and modifying them. ... It's only because each and every variable "knows" what it's doing that the garbage collector can avoid having to store any additional data. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: events and object lifetime
    ... onto the objects with a direct reference and had a method to set the ... Whether you use Dispose() or some other method name, ... hanging around until your application terminates and a "true" memory ... garbage collector and concentrate on programming your application. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Destructors and exceptions
    ... > left to the garbage collector and you have no influence on it. ... collected using reference counting. ... of view is that the exception mechanism doesn't unwind scope. ... the destruction of locals in the presence of an exception ...
    (comp.lang.python)

Loading