1

I have the following class.

public class Foo
{
    private string _DirName;
    private FileStream  _MyfileStream;
    public FileStream  MyfileStream
    {
        get { return _MyfileStream; }               
    }

    public string DirName
    {
        get { return _DirName; }
        set 
        { 
            _DirName = value;
            _MyfileStream = new FileStream(value, FileMode.Create); 
        }
    }            
}

I have Created a List Of Foo as like the following:

List<Foo> FooList = new List<Foo>();
FooList.Add(new Foo() { DirName = @"F:\sample\sample.txt" });
FooList.Add(new Foo() { DirName = @"D:\sample\file.pdf" });

So each list item is creating a File stream. hence the number of streams increased as the number of list item increases. how can i dispose the allocated memory for these streams?

Andy
  • 3,997
  • 2
  • 19
  • 39
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • 1
    What do you mean by "the allocated memory for these streams"? Do you know what garbage collection is? Why do you create a FileStream if you aren't going to read the file at that moment? At which specific moment do you want to dispose, and why? The answer obviously is `FooList.ForEach(f => f.MyfileStream.Dispose())`, but the design here is questionable. – CodeCaster Sep 05 '15 at 11:42
  • you can just save the path of stream as string. then load and unload it whenever its needed. – M.kazem Akhgary Sep 05 '15 at 11:44

2 Answers2

3

All Foo objects and their opened streams will remain in memory and not garbage-collected while the FooList object remains reachable from any point in your application. For example, if FooList is a static member variable, or an instance member variable in a WinForm, that would be the case.

On the other hand, if FooList is a local variable in a method, once the method exist FooList would no longer be reachable and the list and Foo objects would sooner or later be garbage-collected. I'm pretty sure your open streams would be garbage-collected, too. They will be closed automatically through the finalizer.

Using explicit Dispose methods is in most situations optional. Generally, Dispose is used to deterministically free shared resources, such as file streams, open network ports, etc. Classes that require Dispose generally invoke Dispose from the finalizer as well to guarantee clean-up at garbage collection time if the developer/program did not do this at an earlier time. However, having file streams open in your Foo class will not prevent them from being garbage-collected.

Also, listen to CodeCaster and M.kazem and don't open the streams immediately if you don't use them immediately. This is just consuming resources and locking files unnecessarily.

Christoph
  • 2,211
  • 1
  • 16
  • 28
2

Probably Foo should implement IDisposable. You can then iterate that FooList and call Dispose on each item when you no longer need it.

usr
  • 168,620
  • 35
  • 240
  • 369
  • Disposing a stream will free up the shared resource (in this case a file handle) but not free up the memory. Memory will only be freed up by a garbage collection. It is not clear what the OP really wants: Free up memory (title) or only the memory (resources) by the streams? – Christoph Sep 05 '15 at 11:53
  • @Christoph it's unclear to me what he asked precisely. Let's see. That said your answer is a lot better. +1 – usr Sep 05 '15 at 11:54
  • Just corrected my comment and undid the -1 because your answer is certainly not wrong either. Sorry ;-) – Christoph Sep 05 '15 at 11:56