*
The moose likes Beginning Java and the fly likes ArrayList methods-clear or removeall-for cleaning up objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList methods-clear or removeall-for cleaning up objects" Watch "ArrayList methods-clear or removeall-for cleaning up objects" New topic
Author

ArrayList methods-clear or removeall-for cleaning up objects

Matthew X. Brown
Ranch Hand

Joined: Nov 08, 2000
Posts: 165
I'm trying to clean up my objects after I get done with them.
During a session on my tomcat server, serveral arraylists are built for the end user-at the end of the session, I want to clear out the arraylists so that they can be ready for garbage collection-and to free up memory. I'm not sure if the arraylists' clear or removeall methods will do the trick.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
you can also set the ArrayList reference to null.

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Hi Matthew
As the previous answer says, if there are no other active references to the ArrayList in question, set its reference to null and it will become eligible for garbage collection.
However, I get the feeling that you wish to clean up all the objects contained within the ArrayList. In order to make these objects eligible for garbage collection, you need to ensure that there are no other active references to them in other parts of the program. If there are, simply calling <code>clear()</code> on the ArrayList and then setting it to null will NOT result in the objects it contained becoming eligible for garbage collection. Although the memory used by the ArrayList object may be reclaimed, the memory used by the objects that the ArrayList contained will still be in use. So be careful.
Hope this helps
Michael

------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley


"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Matthew X. Brown
Ranch Hand

Joined: Nov 08, 2000
Posts: 165
What is the definition of an "active" reference by the java virtual machine?
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
By 'active reference' I mean a reference from an object that is not itself eligible for garbage collection, i.e. if 2 objects hold references to each other in a situation known as a 'circular reference' in C++, those objects will still be eligible for garbage collection in Java as long as there are no other references to them.
So, a reference to an object from another object that is itself eligible for garbage collection is not considered to be an 'active' reference, and will not prevent garbage collection.
------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ArrayList methods-clear or removeall-for cleaning up objects