• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

ArrayList methods-clear or removeall-for cleaning up objects

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can also set the ArrayList reference to null.

Bosun
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the definition of an "active" reference by the java virtual machine?
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
The only thing that kept the leeches off of me was this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic