C# Dispose object after using in list?

I have a class and this class has "value" variable contain a big data reading from file :

 public class MyClass : IDisposable
{
    public string name {get; set;}
    public string value {get; set;} //I'm load huge data for this variable

    public MyClass(string name, string value)
    {
        this.name = name;
        this.value = value;
    }

    public void Execute()
    {
        Console.WriteLine(this.name + ":" + this.value);
    }

    public void Dispose()
    {
        Dispose(true);            
        GC.SuppressFinalize(this); 
    }

    protected virtual void Dispose(bool disposing)
    {                     
        if (disposing)
        {
            this.name = null;
            this.value = null;
        }                      
    }
}

And i'm using this class in list that it's global variable

List<MyClass> listQ = new List<MyClass>() { new MyClass("1", "BIGDATA1"),
            new MyClass("2", "BIGDATA2"),
            new MyClass("3", "BIGDATA3"),
            new MyClass("4", "BIGDATA4"),
            new MyClass("5", "BIGDATA5"),
            new MyClass("6", "BIGDATA6"),
        };

    private void FUC_Load(object sender, EventArgs e)
    {                        
        foreach (MyClass m in listQ)
        {
            m.Execute();
            m.Dispose();
        }                       
    } 

So i wanna ask that if i do like that, will have any problem? My object will be disposed or not? Any the best way? Thank!

Upvotes: 4

Views: 14207

Answers (4)

Tony U.
Tony U.

Reputation: 56

If possible, use the Queue class instead of List. This way, when you Dequeue, you automatically remove the object from the collection:

while(queueCollection.Count > 0) {
    MyClass obj = queueCollection.Dequeue();
    obj.Execute();
}

Upvotes: 2

Paulo Morgado
Paulo Morgado

Reputation: 14846

The IDisposable interface is for deterministic finalization.

It started as a way to release unmanaged resources but nowadays is commonly used for also finalizing managed objects.

Finalizing doesn't mean releasing the object. It's just a manifestation of your intention to not use the object anymore.

If your object supports Open/Close, it should mean that you can open and close it more than once. In this case Dispose should call Close (or do the same) but the object is unusable anymore.

Many implementations of IDisposable have additional logic to control if the object has been disposed and an event when the object is disposed. This event is often used to remove the object from collections it has been added to.

The object itself will only cease to exist when the garbage collector reclaims it.

Upvotes: -1

Matthew Watson
Matthew Watson

Reputation: 109557

That's an improper use of Dispose().

Dispose() is intended for use in two cases:

  1. When you are disposing unmanaged resources.
  2. When you are disposing an object of a type that itself implements IDisposable.

You should rename your Dispose() method to be something like Clear() to better describe what is happening.

Alternatively (and I think probably better in your case) you could simply set the listQ reference to null once you have iterated over it. Since you are destroying the data it holds as you iterate over it, it is clear that you don't need the data afterwards.

If you set the listQ reference to null, all its memory, including that of all the objects that it holds references to, will (eventually) be garbage-collected, assuming that nothing else holds references to those objects.

Note that you'd have to make sure that all references to listQ are set to null. (References to it on the stack will automatically disappear when the method they are declared in return, so you don't need to worry about those.)

Upvotes: 9

Prabhu Murthy
Prabhu Murthy

Reputation: 9261

I guess you are really worried about the objects eating up your memory.You dont have to unless you are using unmanaged resources like accessing a network or remote db resource.Since this is not your case,the runtime is intelligent enought to free up the resources when it deems necessary.

Garbage collection in .net runtime manages the Managed resources for you and you should not be worried too much about freeing up your managed handles.

When your List reference goes out of scope or when the GC knows that this list is not used ,the object will be cleared from memory.

Upvotes: 2

Related Questions