Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Clear Arraylist vs new ArrayList

2,071 views
Skip to first unread message

Philipp

unread,
May 28, 2008, 3:22:10 AM5/28/08
to
Hello,
What is the most efficient and preferred way to clear a List in terms of
speed and memory?

list = new ArrayList();
or
list.clear();

clear() walks all elements and sets them to null, maybe this is not
efficient for large lists?

Phil

Andrea Francia

unread,
May 28, 2008, 3:37:23 AM5/28/08
to
Philipp wrote:
> Hello,
> What is the most efficient and preferred way to clear a List in terms of
> speed and memory?

This seems a premature optimization question.
Google for these terms.

Do you measured the needs to speed up you program and do you discovered
that the problem is the array creation?

>
> list = new ArrayList();
> or
> list.clear();
>
> clear() walks all elements and sets them to null, maybe this is not
> efficient for large lists?

The best way is the more readable way.
When you finish to create your program and after you measured that there
are some bottleneck with a profiler you can solve them.

>
> Phil


--
Andrea Francia
http://andreafrancia.blogspot.com/

Lew

unread,
May 28, 2008, 8:10:06 AM5/28/08
to
Philipp wrote:
>> Hello,
>> What is the most efficient and preferred way to clear a List in terms
>> of speed and memory?
>
> This seems a premature optimization question.
> Google for these terms.
>
> Do you measured the needs to speed up you program and do you discovered
> that the problem is the array creation?
>
>>
>> list = new ArrayList();
>> or
>> list.clear();
>>
>> clear() walks all elements and sets them to null, maybe this is not
>> efficient for large lists?

Andrea Francia wrote:
> The best way is the more readable way.
> When you finish to create your program and after you measured that there
> are some bottleneck with a profiler you can solve them.

Agreeing with Andrea's advice, I hazard some predictions:

- clear() will be faster when the List is very short.
- The performance situation will favor allocation at a fairly small size.
- The difference will not be large enough to really matter to the program.

One cannot assert these statements with certainty without measurements. The
lifespan of objects whose references were in the List and how long those
references were in there could affect the results, for example. Also,
single-transaction performance and overall throughput are not obvious in their
correlation, especially when multi-threading is involved.

My guesses are based on known principles or Java memory management:
- Allocation in the JVM is very fast, on the order of a dozen or so machine
instructions.
- Deallocation of Java objects is virtually instantaneous.
- GC takes time, much less so when most objects are short-lived and no tenured
objects hold references to nursery denizens. GC can severely diminish
performance if not managed correctly, but that primarily pertains to major
collections. Java idiomatically favors use and disposal of short-lived
objects over keeping references to them around for a long time. GC has
several options, especially in multi-processor platforms, that mitigate its
impact.

--
Lew

Arne Vajhøj

unread,
May 28, 2008, 10:56:16 AM5/28/08
to

Probably the new is slightly faster.

But I am pretty sure that it does not matter for your app, so
it is the wrong question.

The correct question is: does creating a new list or removing
the elements from the existing lists best model the real world
your program is simulating ?

Arne

Andreas Leitgeb

unread,
May 28, 2008, 11:47:52 AM5/28/08
to
Arne Vajhøj <ar...@vajhoej.dk> wrote:
> Philipp wrote:
>> What is the most efficient and preferred way to clear a List in terms of
>> speed and memory?
>> list = new ArrayList();
>> or
>> list.clear();

>> clear() walks all elements and sets them to null,

I'd rather think that the internal array that keeps
the refs is dumped on .clear(), without any walk-through.

> The correct question is: does creating a new list or removing
> the elements from the existing lists best model the real world
> your program is simulating ?

E.g. if you pass (a reference to) an ArrayList to another method,
then it makes a *lot* of difference, whether you .clear() the
collection or whether you replace it with a new one within the
called method.

Patricia Shanahan

unread,
May 28, 2008, 1:03:26 PM5/28/08
to
Andreas Leitgeb wrote:
> Arne Vajhøj <ar...@vajhoej.dk> wrote:
>
>>Philipp wrote:
>>
>>>What is the most efficient and preferred way to clear a List in terms of
>>>speed and memory?
>>>list = new ArrayList();
>>> or
>>>list.clear();
>
>
>>>clear() walks all elements and sets them to null,
>
>
> I'd rather think that the internal array that keeps
> the refs is dumped on .clear(), without any walk-through.

I looked at the source code (JRE 1.5), and it does walk through the
array. It might be more efficient to do it the way you say.

That does create a significant difference between clear() and creating a
new ArrayList. The new ArrayList will start with a small array, and
build it up as needed for the elements added after the clearing. Calling
clear leaves the array at the size it was immediately before.

Using clear() might be the better choice if a lot of CPU time is going
on copying in ArrayList as the list rebuilds after the clear. On the
other hand, new might be the better choice if the ArrayList was very
long, will have fewer elements in the future, and the program is using a
lot of memory.

However, there is an important functional difference. If any copies of
the "list" variable escaped, the other holders of references will go on
seeing the old ArrayList if new is used, but will see their list drop to
zero elements after clear. I would code this however best expresses the
intent, and be very careful about changing it for performance tuniing.

Patricia

Roedy Green

unread,
May 28, 2008, 2:32:56 PM5/28/08
to
On Wed, 28 May 2008 09:22:10 +0200, Philipp <sics...@freesurf.ch>
wrote, quoted or indirectly quoted someone who said :

If you need a new array the same size or slightly smaller, clear would
be preferable. If not, new.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Arne Vajhøj

unread,
May 28, 2008, 5:07:05 PM5/28/08
to
Andreas Leitgeb wrote:
> Arne Vajhøj <ar...@vajhoej.dk> wrote:
>> Philipp wrote:
>>> What is the most efficient and preferred way to clear a List in terms of
>>> speed and memory?
>>> list = new ArrayList();
>>> or
>>> list.clear();
>
>>> clear() walks all elements and sets them to null,
>
> I'd rather think that the internal array that keeps
> the refs is dumped on .clear(), without any walk-through.

It does loop and set to null.

>> The correct question is: does creating a new list or removing
>> the elements from the existing lists best model the real world
>> your program is simulating ?
>
> E.g. if you pass (a reference to) an ArrayList to another method,
> then it makes a *lot* of difference, whether you .clear() the
> collection or whether you replace it with a new one within the
> called method.

Absolutely.

Arne

0 new messages