This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList methods-clear or removeall-for cleaning up objects

 
Matthew X. Brown
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1510
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can also set the ArrayList reference to null.

Bosun
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Matthew X. Brown
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the definition of an "active" reference by the java virtual machine?
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic