List.clear () vs list = new ArrayList <Integer> ();
Which of the two options is better and faster to clear ArrayList and why?
list.clear() or
list = new ArrayList<Integer>(); It happens that I have to clear all the records from my ArrayList at random times, and I have no way to find out how many new records will be in the future, maybe 0 or 1000. Which method is faster and better, and why?
It's hard to know without a test, but if you have many elements in an ArrayList and the average size is lower, it might be faster to create 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 will remove items 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 is cleared, links to elements belonging to it are disabled, but it retains the same support array. Then mylist was reinitialized and received a new support array, the old one got GCed. Thus, one of the methods is held in memory, and the other gives out its memory and is redistributed from scratch (with the default power). It better depends on whether you want to reduce garbage collection or minimize the current amount of unused memory. Whether a list sticks long enough to be moved out of Eden may be a factor in determining which is faster (because it can make garbage collection more expensive).
I think the answer is that it depends on a number of factors, such as:
- Is it possible to predict the size of the list in advance (i.e., can you accurately determine the capacity)
- whether the size of the list is variable (i.e. each time it is populated),
- how long the list will last in both versions, and
- your heap / gc parameters and processor.
This makes it difficult to predict what will be better. But my intuition is that the difference will not be so big.
Two optimization tips:
Do not waste time trying to optimize this ... if the application is not objectively too slow and measuring with the profiler tells you that this is a performance hot spot. (Most likely, one of these prerequisites will not be fulfilled.)
If you decide to optimize it, do it scientifically. Try both (all) alternatives and determine which one is better by measuring the performance in your real application on a realistic task / workload / input set. (An artificial test can give you answers that do not predict real-world behavior due to factors similar to the ones I listed earlier.)
First .clear(); saves the same list that just clears the list.
Second new ArrayList<Integer>(); creates a new ArrayList in memory.
Suggestion: First, because it is what is meant to be done.
If there is a good chance that the list will contain as many elements as it contains when cleaning it, and if you do not need free memory, clearing the list is the best option. But I guess this probably doesn't matter. Do not try to optimize until you find a performance problem and decide where it came from.
Tried the program below using both approaches. 1. With clearing arraylist obj in for loop 2. Creating a new loop New Arraylist in for.
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. memory allocation has not changed much.
With a new approach to Arraylist.
To full free memory: 64,909 ::
Total free loop memory: 64,775 ::
with a clear approach
Before the cycle, full free memory: 64,909 :: After the cycle, full free memory: 64,765 ::
So, this suggests that there is not much difference in using arraylist.clear in terms of memory usage.
list.clear() will support the same ArrayList, but the same amount of memory. list = new ArrayList<int>(); going to allocate new memory for your ArrayList.
The big difference is that ArrayLists will dynamically expand as you need more space. Therefore, if you call list.clear() , you will still have a potentially large amount of memory allocated for ArrayList, which might not be needed.
list.clear() will be faster nonetheless, but if you want to combine memory layouts you can allocate a new ArrayList.
I would suggest using list.clear () instead of highlighting a new object. When you invoke the βnewβ keyword, you create more memory space. In fact, it does not really matter. I believe that if you know how big the list is, it might be a good idea to create a new space, but then indicate how big the array is.
In truth, it will not matter if you do not learn programming. In this case, you need to learn C ++.