I asked a question about this method:
// Save an object out to the disk
public static void SerializeObject<T>(this T toSerialize, String filename)
{
XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType());
TextWriter textWriter = new StreamWriter(filename);
xmlSerializer.Serialize(textWriter, toSerialize);
textWriter.Close();
}
in the response I got this as an added remark:
Make sure you always dispose disposable resources such as streams and text readers and writers. This doesn't seem to be the case in your SerializeObject method.
So, I can tell that this is going to seem super lame for someone who has been coding C# for a year or two, but why do I have to dispose it?
Is see that testWriter
has a dispose method, but shouldn't garbage collection take care of that? I came from Delphi to C#. In Delphi I had to clean up everything, so this is not a case of me wanting to be lazy. I just was told that if you force freeing up the memory that your objects take then it can cause bad stuff. I was told to "Just let the garbage collector do it".
textWriter
hits the disk.)With waste being properly disposed of, our environment is cleaner, meaning there are fewer health risks and hazards around to affect us. This includes not having our surroundings polluted and, instead, ensures that our society remains as healthy as possible.
The main benefits of effective waste disposal include: Environmental protection – from pollution or contamination. Money generation – companies may buy recyclable materials due to their value. Additionally, the waste management industry creates employment opportunities.
we should throw garbage in covered bins because if we throw garbage here and there then many types of pollution will occur ,diseases will arise so to escape from these things we should throw garbage in covered bins.
waste disposal, the collection, processing, and recycling or deposition of the waste materials of human society.
The rule of thumb here is pretty simple: always call Dispose()
on objects that implement IDisposable
(not all objects do). You won't always know the reason why an object had to implement Dispose, but you should assume that it is there for a reason.
The easiest way to make sure you do this is through using
:
using (TextWriter tw = new StreamWriter(fileName))
{
// your code here
}
This will call Dispose()
automatically at the end of the using block (it's fundamentally the same as using a try/catch/finally with the Dispose() in the finally block).
For more information on how Dispose works with garbage collection, see here.
You are correct that for properly written code the GC will eventually clean up the native resources. The object will have a finalizer, and during finalization will free up the necessary native resources.
However when this happens is very non-deterministic. Additionally it's a bit backwards because you're using the GC which designed to handle managed memory as a means to manage native resources. This leads to interesting cases and can cause native resources to stay alive much longer than anticipated leading to situations where
The using / dispose pattern adds determinism to the cleanup of native resources and removes these problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With