Clear Arraylist vs new ArrayList

P

Philipp

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
 
A

Andrea Francia

Philipp said:
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.
 
A

Arne Vajhøj

Philipp said:
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?

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
 
A

Andreas Leitgeb

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.
 
P

Patricia Shanahan

Andreas said:
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
 
R

Roedy Green

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?

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

Arne Vajhøj

Andreas said:
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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top