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

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?
rnicholusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krakatoaCommented:
>>>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.
0
krakatoaCommented:
> objects are not garbage collected
unless there are no other references to them anywhere else.
0
rnicholusAuthor 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.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

krakatoaCommented:
>>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.
0
rnicholusAuthor 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.
0
krakatoaCommented:
>> 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?
0
rnicholusAuthor Commented:
the pointers hold by the ArrayList.
0
krakatoaCommented:
?
0
rnicholusAuthor 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?
0
krakatoaCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.