tags:

views:

186

answers:

2

I have a heavily populated arraylist, which I want to clear and reuse. If I clear it will it free up that previously used memory?

I should also mention that the arraylist is a private read only field of a class that still has lots of active work to do after I use the arraylist first time round. So I can't wait for garbage collection after class goes out of scope.

Is the Clear method fast enough? Or should I destroy and create a new arraylist?

Question update:

If I have field declared like this (thanks to Jon's advice)

    /// <summary>
    /// Collection of tasks.
    /// </summary>
    private List<Task> tasks = new List<Task>();

then I populate it.... (heavily)

Now if instead of clearing it and trimming, can I just call:

tasks = new List<Task>(); 

Would this be recommended?

+11  A: 

Do whichever expresses your intention better. Do you actually want a new list? If so, create a new one. If you conceptually want to reuse the same list, call Clear.

The documentation for ArrayList does state that Clear retains the original capacity - so you'll still have a large array, but it'll be full of nulls instead of reference to the previous elements:

Capacity remains unchanged. To reset the capacity of the ArrayList, call TrimToSize or set the Capacity property directly. Trimming an empty ArrayList sets the capacity of the ArrayList to the default capacity.

Any reason you're using ArrayList rather than List<T> by the way?

Jon Skeet
Hi Jon, to answer your question lack of knowledge I guess, what is the benefit of using List<T>?
JL
@JL: Generic collections are generally better: type safe at compile-time, easier to use (no need for casts), more efficient for value types... what version of .NET are you using? (They're only available from .NET 2.0 and upwards.)
Jon Skeet
Last q: If I had a heavily populate List<T> and after using it set its value to new List<T> (obviously where T = my class) - would this give me a new List<T> and at the same time free up previously used memory for garbage collection?
JL
It would make the previous list eligible for garbage collection if nothing else had a reference to it, yes. It wouldn't actually force the garbage collector to run, of course - but usually that's a good thing.
Jon Skeet
Awesome, thanks!
JL
+2  A: 

If you want the memory to actually be free'd, set it to null and invoke the garbage collector. Then create a new ArrayList. If you set it to null and then create a new one, it will eventually get garbage collected when additional memory is required. Also, I second generic collections. It's been a long time now since I've used ArrayList.

Nate
If performance is an issue then don't call GC.Free() but rather wait for the GC to do its thing when necessary - in fact, in general, I'd consider it a code smell to invoke the GC explicitly.
Jeremy McGee
I agree on GC.Free(), I avoid it. However, I've seen instances where it is necessary. I have read techniques on using it in XNA games between scenes to avoid garbage collection at the wrong time in a game...it has its uses.
Nate