I want to create multiple FileStreams and need to keep them open – there will be no I/O operations. What will be the memory consumption? If I create large number of such streams will this effect system performance?
I want to create multiple FileStreams and need to keep them open – there
Share
In short: It is not a good idea to keep File Streams open, because it is un-managed resource.
In .NET framework architecture all un-managed resources cause memory big leaks, if NOT managed correctly in the code.
If you are saying – “I don’t want to just let it go out of scope. Then the garbage collector will eventually call the Dispose, killing the stream. But i want to keep the stream open.”
Garbage collector will call the
Finalizemethod (destructor), not theDisposemethod. The finalizer will callDispose(false)which will not dispose the underlying stream. You should be OK by leaving theStreamReadergo out of scope if you need to use the underlying stream directly. Just make sure you dispose the underlying stream manually when it’s appropriate.