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?

- 6,682
- 9
- 45
- 73

- 23
- 2
-
2Why do you need to have ope file streams if you are not doing anything with them? – Evgeny Lukashevich Dec 16 '12 at 14:49
-
@Eugene Just asking doubts i need to handle muliple file I/O some may be not needed,but need to keep open – phoenix Dec 16 '12 at 14:51
1 Answers
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 Finalize
method (destructor), not the Dispose
method. The finalizer will call Dispose(false)
which will not dispose the underlying stream. You should be OK by leaving the StreamReader
go 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.

- 5,815
- 9
- 32
- 69
-
i intend to do nothing with the file stream just need to keep some open,but other processes may read the file – phoenix Dec 16 '12 at 14:47
-
In this case it's not memory that'd be leaked, but file handles. – Evgeny Lukashevich Dec 16 '12 at 14:48
-
-
On application close all steams will be disposed.Will the garbage collection kill the streams if its kept open for too long (idle) – phoenix Dec 16 '12 at 14:51
-
@phoenix is this discussion answers your concern - http://stackoverflow.com/q/2313728/1437962 – Yusubov Dec 16 '12 at 14:51
-
@ElYusubov I checked it,but it did not explain fully.Can you tell me this Will the garbage collection kill the streams if its kept open for too long (idle) will this effect system memory or perfomance – phoenix Dec 16 '12 at 15:12
-
Yes, open memory streams do effect performance, thus it is better to manage them rather than idle them. Non-active objects are recycled by garbage collector in .NET environment. – Yusubov Dec 16 '12 at 15:18