?
Solved

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

Posted on 2008-01-25
10
Medium Priority
?
1,366 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?
0
Comment
Question by:rnicholus
  • 6
  • 4
10 Comments
 
LVL 17

Expert Comment

by:krakatoa
ID: 20746029
>>>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
 
LVL 17

Expert Comment

by:krakatoa
ID: 20746054
> objects are not garbage collected
unless there are no other references to them anywhere else.
0
 

Author Comment

by:rnicholus
ID: 20746085
>>>>>
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 17

Expert Comment

by:krakatoa
ID: 20746475
>>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
 

Author Comment

by:rnicholus
ID: 20760798
>>>>>>>>>>>>>>>>>>>>>>>>
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
 
LVL 17

Expert Comment

by:krakatoa
ID: 20761826
>> 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
 

Author Comment

by:rnicholus
ID: 20801567
the pointers hold by the ArrayList.
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 20801698
?
0
 

Author Comment

by:rnicholus
ID: 20801834
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
 
LVL 17

Accepted Solution

by:
krakatoa earned 225 total points
ID: 20804132
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month5 days, 22 hours left to enroll

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question