Difference between null and clear on an Arraylist

Asked

Viewed 3,064 times

5

What’s the difference between:

arrayList.clear();

and

arrayList = null;

The two do not destroy all elements of Arraylist?

1 answer

8


Not.

As the name itself says the first clears all elements (makes the elements null) and the ArrayList has zero elements (the size is changed) and you can continue manipulating it. It does not mean it will save memory.

The second destroys the reference in this variable (puts a reference to 0) preventing it from accessing the object ArrayList and making it impossible for you to use it with this variable. You can even create another object and store it in the same variable, but it will be a new object. If there are no more references to this object it stays in memory, unavailable, until the Garbage Collector collect it from memory. The variable (i.e., the reference to the object that will be 0 while not pointing to an object) continues to exist and until another object is bound to it, nothing can be done but, obviously, store a new object ArrayList, then allowing all operations that this class can perform.

You may find that the result is the same. And it may even be depending on what you want. Conceptually the first makes more sense if you want to clean up the elements and start again with the ArrayList. Setar null in a variable ArrayList means you don’t need it anymore and it’s usually unnecessary.

In Java and many modern languages (which have GC tracker) it is often unnecessary to take any action to say that you no longer need a variable and that you no longer want a reference to an object, just stop using it. Of course there may be cases where you know that it will take too long or there will never be the release of this variable and this can be considered a leak and memory and setar null for this variable may be an option to free the memory if you know that it is the only reference to the object.

It is rare but it is good to know that even with GC there is the possibility of memory leakage, and know how to deal with it. I wouldn’t know how to cite all the examples in Java, but if the variable is static or is in an object (as a member or in a method) created and maintained at the beginning of the application/thread. In C# know that this leak can occur and events (Observer Pattern) if the programmer forgets to release the object at the time of the last unsubscribe.

These codes have "more equivalent results":

arrayList.clear();

and

arrayList = null;
arrayList = new ArrayList();

Thus the following transactions on top of the variable arrayList can be done the same way in a ArrayList "clean" But the same result was achieved with different techniques with different characteristics and different performance, although it is small and probably irrelevant in most cases. The second can generate more pressure in the GC.

  • 2

    "The second destroys the ArrayList making it impossible for you to use it" if this is the only reference for him, of course. If there is another reference to the same ArrayList can continue using it normally, only not through this variable.

  • @mgibsonbr well observed, I will edit.

  • so for the last example... In my case I would have to use the same clear since it is a global arrayList that will be reused, but I have another Arraylist (private), which stores selected data from a Gridview (Android), so it will be used several times, when the person clicks on the button or cancels the action the arrayList was unnecessary, so I would have to just create a new Arraylist when necessary? or it would be good to set NULL, taking into account that Android uses Dalvik

  • It is difficult to make an assessment without seeing the whole (the whole) but as I said, use a null almost never is the solution. If the variable is not actually used, it will be deleted even if it does not establish the null. I don’t know if you came from any language that this was necessary (some required setar a null to free up memory). In Java this is unnecessary. Of course you may be doing something else wrong and this variable may be surviving unnecessarily, but it’s not because you forgot to set null. If I see this in a code, then I look for some other mistake.

  • I do not say this because java has an automated collection, but I say for example in the case of Android which is a much more limited system and does not use the same vm as Java for desktop, uses Dalvik. I know that the GC 'rotates' after a certain time and not in the exact time when the variable is no longer used... So I asked about Android if it would really be necessary to improve performance. Do not run GC, but set arraylist to null, especially when arraylist has a lot of data

  • I understand your concern but there is no problem, because the GC runs according to the need of the platform even though it is a different implementation. If there are no references to the object and there is pressure in the memory, the collection will occur. If there is no pressure on memory, what does it matter if the data remains there in memory? This is not memory leakage. It’s there because it could still be trouble-free. Leakage would be only if it had to be eliminated but it cannot because it still has an unnecessary reference to the object. There you have to find out why this reference is alive. But it’s not because it lacked null.

  • Depends on implementation but note that the clear() do not need to prevent unnecessary object collection. Internally the ArrayList abandons internal objects several times as it grows. And these abandoned objects are collected. Even if the ArrayList in itself is not released, at least the elements of it I assure you will be released with the clear(). Unless Dalvik has a very dirty implementation :)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.