Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am under impression that GC will take care of all the references , memory management time to time.
The object may be of file stream or excel object etc.
We need not worry about the memory management at all. and day before yesterday one of friend said no we have to flush when we create stream objects explicitly.
Please clear the doubts

Thanks in advance
Posted

Check this link, this may be helpfull

http://msdn.microsoft.com/en-us/magazine/bb985010.aspx[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Sep-11 3:36am    
My 5 for a good link; I offered direct answer and the points that are involved in practice, please see.
--SA
Shahin Khorshidnia 12-Jun-12 0:58am    
+5
Yes, we don't have to worry about memory management. However, flush of the stream has nothing to do with memory management. We need to take care about the flush and closing of the stream. Pay attention: many classes implement System.IDisposable. You need to make sure IDisposable.Dispose for such classes is always called when their work is done. One good way of doing so is using "using" statement (not to be mixed up with using clause), see http://msdn.microsoft.com/en-us/library/yh598w02%28v=VS.100%29.aspx[^].

In some classes a failure to dispose can cause additional trouble: a leak of unmanaged memory.

Finally, if you think that GC saves you from leak of managed memory, think again. Yes, it totally guards you against accidental leaks, when you forget to delete some object (there is no delete), but you can easily cause the leak by design mistakes. Remember, an object is scheduled for execution when the running code looses all references to it. Even if object A references object B, B references C and C references A this situation is resolved and they all are destroyed if there are no more references to either of them.

Imagine you create and delete some objects (for example, Forms) and expect the memory allocation to come into the same point when the number of these objects comes to zero. It will work. Now imagine you have a global object such as Dictionary used to hold all those objects for the purpose of quick finding them, say, by name. If you forget to delete references from an instance of the Dictionary, references will be collected there forever effectively preventing destruction — you created a memory leak.

It can happen whenever you have some singleton in the Application Domain. One other technique to solve this problem is System.WeakReference, see http://msdn.microsoft.com/en-us/library/system.weakreference.aspx[^].

[EDIT]

Please see also my recent answer on a related topic:
deferring varirable inside the loop can cuase memory leak?[^].

—SA
 
Share this answer
 
v4
Comments
Shahin Khorshidnia 12-Jun-12 0:58am    
+5
Sergey Alexandrovich Kryukov 12-Jun-12 1:14am    
Thank you, Shahin.
--SA
VJ Reddy 12-Jun-12 1:51am    
Nice explanation. 5!
Sergey Alexandrovich Kryukov 12-Jun-12 2:06am    
Thank you, VJ.
--SA
Manas Bhardwaj 12-Jun-12 1:57am    
very well explained +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900