We help IT Professionals succeed at work.

arrayList.clear() VERSUS arrayList = new ArrayList()

rnicholus
rnicholus asked
on
Medium Priority
1,414 Views
Last Modified: 2008-03-04
I'm wondering if ArrayList clear() method will release memory allocated for the ArrayList back into the Operating System.

1.) I create a new list with 100 items. The list now has 100 object pointers. Each pointer in the list reference to an object somewhere in memory.

2.) Then I call clear(); Now all the objects referenced by the pointer in the list have become eligible for garbage collection. But the list itself still uses memory to keep 100 object pointers.

3.) Then if I fill in the list with 5 more items. Then I have a list with 5 items but the memory size used by the list is stil for 100 object pointers.

Is this correct?
Comment
Watch Question

CERTIFIED EXPERT

Commented:
>>>2.) Then I call clear(); Now all the objects referenced by the pointer in the list have become eligible for garbage collection. But the list itself still uses memory to keep 100 object pointers.
No, objects are not garbage collected, only the array is cleared.

3 If the array is growable like Arraylist or Vector, its size depends on the number of elements, but if by "5 more items" you mean only 5 items long, then the size will be smaller than if it contained 100.
CERTIFIED EXPERT

Commented:
> objects are not garbage collected
unless there are no other references to them anywhere else.

Author

Commented:
>>>>>
3 If the array is growable like Arraylist or Vector, its size depends on the number of elements, but if by "5 more items" you mean only 5 items long, then the size will be smaller than if it contained 100.
>>>>>
I'm not talking about the size of the ArrayList itself but the memory size.

I just want to make sure that's it's not exactly a memory leak in the traditional sense - it doesn't keep growing. But whatever was the maximum size ever used by the ArrayList (for valid non-memory-leak reasons), that maximum size will be retained even when it's no longer necessary.
CERTIFIED EXPERT

Commented:
>>it's not exactly a memory leak in the

you did not mention memory leaking until now.

Arrays are simply upmarket pointers, they hold references to objects (or primitives), not the objects themselves. The memory space available at any given time is, more or less, the total available memory (as set by the JVM) minus the number of objects in memory multiplied by their sizes. You can ask the Runtime to dsiplay that figure.

Author

Commented:
>>>>>>>>>>>>>>>>>>>>>>>>
Arrays are simply upmarket pointers, they hold references to objects (or primitives), not the objects themselves. The memory space available at any given time is, more or less, the total available memory (as set by the JVM) minus the number of objects in memory multiplied by their sizes. You can ask the Runtime to dsiplay that figure.
>>>>>>>>>>>>>>>>>>>>>>>>
Exactly, I understand that after ArrayList.clear(), the object themselves will be eligible for Garbage Collector. But the ArrayList it self is still pointing/ holding references to that pointer. And let say you have 1,000,000 pointers then you do clear() then the pointers are still there in the memory.
CERTIFIED EXPERT

Commented:
>> Exactly, I understand that after ArrayList.clear(), the object themselves will be eligible for Garbage Collector.

Only if they are not being pointed to by something else.

>>But the ArrayList it self is still pointing/ holding references to that pointer.
? what is "that" pointer?

Author

Commented:
the pointers hold by the ArrayList.
CERTIFIED EXPERT

Commented:
?

Author

Commented:
Okay, let me try by example.
1.) Let say you have an ArrayList with 10,000,000,000 pointers to an object. Call this ArrayList: A
2.)
- 1st option: Then you do A.clear()
- 2nd option: Then you do A = new ArryaList()

Which one after step. 2 uses less memory?
CERTIFIED EXPERT
Commented:
The relevant issue is the memory used on the objects themselves; I think the size of the array itself is nominal, an empty array would be no different in size than a new one.