Re: FileStream.Close() & GarbageCollection - Memory Leak Question
- From: Lasse Vågsæther Karlsen <lasse@xxxxxxxxxxx>
- Date: Mon, 04 Feb 2008 22:20:09 +0100
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
.
- Follow-Ups:
- References:
- Prev by Date: FileStream.Close() & GarbageCollection - Memory Leak Question
- Next by Date: .net compact/smartphone
- Previous by thread: FileStream.Close() & GarbageCollection - Memory Leak Question
- Next by thread: Re: FileStream.Close() & GarbageCollection - Memory Leak Question
- Index(es):
Relevant Pages
|
Loading