1

If I have an ArrayList with several elements and I want to clear all elements should I:

  • point it to a new reference? Will this call the GC? In Android calling the GC is not so good.
  • call arraylist.clear(). Does clear itself call the GC to free the memory?

What about if the arraylist is really huge? I think clear has bad performance when the arraylist is huge. So it is better to point to a new reference and just let the GC do its work?

mthandr
  • 3,062
  • 2
  • 23
  • 33
  • Actually, what the call to `clear()` does is iterate over the backing array and setting each entry to `null` (as well as resetting the `size` attribute). No call to the GC, no fancy stuff. – Turing85 Jun 15 '15 at 15:47
  • You have no control over when garbage collection occurs. However, if you have an `ArrayList` which you set `null`, *and there exist no other references to the objects contained the the list before nullifying*, then those objects become *eligible* for garbage collection when the JVM determines it is time. – Kon Jun 15 '15 at 15:47
  • "I think clear has bad performance" - what are you doing there? Adding and removing billions of elements? I bet you don't – AdamSkywalker Jun 15 '15 at 15:49
  • @AdamSkywalker Is it billions of elements that can call the GC in Android? Android memory for each application can really be small so what I am worried is what happens if I put the arraylist to null. Those objects are somewhere still occupying memory no? If there are a lot in multiple arraylists then GC can be forced to be called. Sure I can't control the GC but I think I can do as much as possible to prevent calling it – mthandr Jun 15 '15 at 15:59
  • but the clear also puts the objects to null, which means the objects are still there somewhere, so the GC will be called anyways right? Then I can conclude that there is no real advantage to using clear or dereferencing the arrraylist? – mthandr Jun 15 '15 at 16:00
  • Assuming reference has size of 8 byte, your list with 1M elements will be the size of 8 megabytes. Not that much. – AdamSkywalker Jun 15 '15 at 16:00
  • @androidpotato7 "there is no real advantage" - if we speak about how soon GC will occur, then yes, both methods are equal. But dereference is faster, as was mentioned in the answer. – AdamSkywalker Jun 16 '15 at 11:40

1 Answers1

2

point it to a new reference? Will this call the GC? In Android calling the GC is not so good.

It won't. Usually GC is performed when there is not enough memory for new object allocations.

call arraylist.clear(). Does clear itself call the GC to free the memory?

Again no. Clearing the list will remove references from list to elements, that's all. Actual GC will be performed when needed.

What about if the arraylist is really huge? I think clear has bad performance when the arraylist is huge. So it is better to point to a new reference and just let the GC do its work?

Time complexity of clear() is O(n). Pointing reference to another array list is O(1). So yes, it is faster but I've never seen that in real world applications.

AdamSkywalker
  • 11,408
  • 3
  • 38
  • 76