Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to delete objects: implement Dispose or create objects in a function?

I have some objects that read a file, save the data in arrays and make some operations. The sequence is Create object A, operate with object A. Create object B, operate with object B... The data read by each object may be around 10 MB. So the best option would be to delete each object after operate with each one. Let say I want my program to allocate around 10 MB in memory, not 10MB * 1000 objects = 1GB

The objects are something like:

class MyClass
{  
    List<string[]> data;

    public MyClass(string datafile)
    {
        using (CsvReader csv = new CsvReader(new StreamReader(datafile), true))
        {
            data = csv.ToList<string[]>();
        }
    }

    public List<string> Operate()
    {
    ...
    }

}

My question is: should I implement dispose? And do something like:

List<string> results = new List<results>();

using (MyClass m = new MyClass("fileM.txt"))
            {
                results.AddRange(m.Operate());
            }

using (MyClass d = new MyClass("fileD.txt"))
            {
                results.AddRange(d.Operate());
            }

...

I´ve read that implementing Disposable is recommended when you use unmmanaged resources (sockets, streams, ...), but in my class I have only big data arrays.

Another way would be to create functions for each objects (I suppose GC will delete a object created in a function automatically):

List<string> results = new List<results>();
results.AddRange(myFunction("fileM.txt"));
results.AddRange(myFunction("fileD.txt"));


public List<string> myFunction(string file)
{
MyClass c = new MyClass(file);
return results.AddRange(c.Operate());
}
like image 409
Alberto Avatar asked Feb 22 '13 02:02

Alberto


1 Answers

IDisposable etc will not help you here, since it doesn't cause anything to get collected. In this type of scenario, maybe the best approach is to use a pool to reduce allocations - essentially becoming your own memory manager. For example, if your List<string> is big, you can avoid a lot of the arrays by re-using the lists - after clearing them, obviously. If you call .Clear(), the backing array is not reset - it just sets a logical marker to consider it as empty. In your specific case, a lot of your objects are going to be the individual strings; that is trickier, but at least they are small and should be collectable in generation-zero.

like image 60
Marc Gravell Avatar answered Oct 14 '22 17:10

Marc Gravell