List list = new ArrayList(); the rest of your code only knows that data is of type List, which is preferable because it allows you to switch between different implementations of the List interface with ease.
clear() method removes all of the elements from this list. The list will be empty after this call returns.
The clear() method of List interface in Java is used to remove all of the elements from the List container. This method does not deleted the List container, instead it justs removes all of the elements from the List.
Definition and Usage The clear() method removes all the elements from a list.
It's hard to know without a benchmark, but if you have lots of items in your ArrayList and the average size is lower, it might be faster to make a new ArrayList.
http://www.docjar.com/html/api/java/util/ArrayList.java.html
public void clear() {
modCount++;
// Let gc do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
List.clear
would remove the elements without reducing the capacity of the list.
groovy:000> mylist = [1,2,3,4,5,6,7,8,9,10,11,12]
===> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
groovy:000> mylist.elementData.length
===> 12
groovy:000> mylist.elementData
===> [Ljava.lang.Object;@19d6af
groovy:000> mylist.clear()
===> null
groovy:000> mylist.elementData.length
===> 12
groovy:000> mylist.elementData
===> [Ljava.lang.Object;@19d6af
groovy:000> mylist = new ArrayList();
===> []
groovy:000> mylist.elementData
===> [Ljava.lang.Object;@2bfdff
groovy:000> mylist.elementData.length
===> 10
Here mylist got cleared, the references to the elements held by it got nulled out, but it keeps the same backing array. Then mylist was reinitialized and got a new backing array, the old one got GCed. So one way holds onto memory, the other one throws out its memory and gets reallocated from scratch (with the default capacity). Which is better depends on whether you want to reduce garbage-collection churn or minimize the current amount of unused memory. Whether the list sticks around long enough to be moved out of Eden might be a factor in deciding which is faster (because that might make garbage-collecting it more expensive).
I think that the answer is that it depends on a whole range of factors such as:
These make it hard to predict which will be better. But my intuition is that the difference will not be that great.
Two bits of advice on optimization:
Don't waste time trying to optimize this ... unless the application is objectively too slow AND measurement using a profiler tells you that this is a performance hotspot. (The chances are that one of those preconditions won't be true.)
If you do decide to optimize this, do it scientifically. Try both (all) of the alternatives and decide which is best by measuring the performance in your actual application on a realistic problem / workload / input set. (An artificial benchmark is liable to give you answers that do not predict real-world behavior, because of factors like those I listed previously.)
The first one .clear();
will keep the same list just clear the list.
The second one new ArrayList<Integer>();
creates a new ArrayList
in memory.
Suggestion: First one because that's what is is designed to do.
Tried the below program , With both the approach. 1. With clearing the arraylist obj in for loop 2. creating new New Arraylist in for loop.
List al= new ArrayList();
for(int i=0;i<100;i++)
{
//List al= new ArrayList();
for(int j=0;j<10;j++)
{
al.add(Integer.parseInt("" +j+i));
//System.out.println("Obj val " +al.get(j));
}
//System.out.println("Hashcode : " + al.hashCode());
al.clear();
}
and to my surprise. the memory allocation didnt change much.
With New Arraylist approach.
Before loop total free memory: 64,909 ::
After loop total free memory: 64,775 ::
with Clear approach,
Before loop total free memory: 64,909 :: After loop total free memory: 64,765 ::
So this says there is not much difference in using arraylist.clear from memory utilization perspective.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With