Reputation: 9870
What is the benefit from a user's point of view for closing a stream?
When running this code, it doesn't make visible difference to the user, whether you comment out response.Close();
or not.
Can anyone give me an example where not closing a stream/file handle etc would cause a noticeable problem to the user?
WebRequest request = WebRequest.Create("http://www.microsoft.com");
WebResponse response = request.GetResponse();
StreamReader responseStream = new StreamReader(response.GetResponseStream());
string responseText = responseStream.ReadToEnd();
Console.WriteLine(responseText); // Displays the HTML of the website
response.Close();
EDIT: Everyone is saying stuff like "it's good to dispose/close a stream so you release unmanaged resources". But can anyone provide me with an example showing how this can create a noticeable difference to the end user?
Upvotes: 0
Views: 1313
Reputation: 13338
It is important to close/dispose all streams, when done using them.
Best way to do this is by using a using statement
:
using(StreamReader responseStream = new StreamReader(response.GetResponseStream())
{
//streamreader will be disposed automatically,
//when done executing within brackets.
}
If the stream(s) isn't/aren't properly closed/disposed, it will cause a memory leak. Resources used by the stream, will be released when closing/disposing the stream.
Everyone is saying stuff like "it's good to dispose/close a stream so you release unmanaged resources". But can anyone provide me with an example showing how this can create a noticeable difference to the end user?
Try following code in your application:
private void LoopUndisposedStream()
{
for(int i = 0; i < 5000000; i++)
{
CreateUndisposedStream();
}
}
private void CreateUndisposedStream()
{
WebRequest request = WebRequest.Create("http://www.microsoft.com");
WebResponse response = request.GetResponse();
StreamReader responseStream = new StreamReader(response.GetResponseStream());
string responseText = responseStream.ReadToEnd();
Console.WriteLine(responseText); // Displays the HTML of the website
}
Above code is what, you should absolutely NEVER do. But since you're asking for an example, showing the performance problems, when not disposing every stream. Here it is.
Upvotes: 4
Reputation: 11228
By closing a Stream
you release resources, in addition you make some objects that you no longer need eligible for GargabeCollector so it reduces memory usage etc.
Upvotes: 1
Reputation: 29668
By closing a stream you release the resources bound up with it, although those resources can vary depending on the type of stream you use, with some having greater impact that others.
Take FileStream
for example. While the stream is open it has an open handle on the file. If you do not close it the handle will not be released until the object is garbage collected at some unspecified point in the future.
Now, depending on how you opened that file you might run into issues, take for example this:
FileStream file = new FileStream("example.dat",FileMode.Open,FileAccess.Read,FileShare.None);
This gets an exclusive lock on the file which won't be released. This means any other calls to open the file and read will fail.
So it's always better, especially when dealing with classes that interface with unmanaged resources to release as soon as you're done.
One of the easiest ways of doing this is using the using
construct, for example:
using (FileStream file = new FileStream("example.dat",FileMode.Open,FileAccess.Read,FileShare.None)) {
// Do something useful
}
You can use using
with any class that implements IDisposable
and it will ensure that once outside of the scope of the statement Dispose
is called and any resources are released.
Upvotes: 4