.net does ArrayList.Clear free up memory?

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?


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.

Tags:

C#

.Net

Arraylist