• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to discard objects stored in an ArrayList

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class called Product:



In my Test class, I have an ArrayList that stores a list of Product.



At some point after I am done using the List, I want to set reference for these objects to "null" so that it is available for garbage collection. How would I do that?

Just calling aList.clear() and setting aList = null, will this mean all the prods stored in aList will be de-referenced and available for GC or do I need to loop over all the products and set them to null explicitly?

Thanks.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the List a field, or a local variable? The way you have written it, it looks like a local variable.
 
Baiju Mehta
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad, sorry about that. aList is a Global variable.

Incase if aList was a local variable, my understanding is that once the method returns, aList is out of scope and hence available for GC. Is that correct?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Just calling aList.clear() and setting aList = null, will this mean all the prods stored in aList will be de-referenced and available for GC or do I need to loop over all the products and set them to null explicitly?



The easy answer here is... an object is eligible for GC, when it is no longer reachable.

For example, if you did a aList = null, and that reference is the only reference to the array list, then the array list object becomes unreachable, and hence, eligible for GC. Furthermore, since the array list is unreachable, if the elements in the list, are not referenced from anywhere else, then the elements too, become unreachable, and hence, eligible for GC.

Henry
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We don't talk about global variables in Java. It is a field. In fact, since you didn't write "public" it is not "visible" outside its own package.
 
Baiju Mehta
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell and Henry for your quick replies.

Campbell, yes, I need to access aList outside this class and it is declared as a public field.

Henry, Just to make sure that I understand what you are saying.. Lets take this example.

I have an Order class:

Now I have a Product class:


Now I have my Test class that looks like this:


Now, since the old products are unreacheable, they should be available for GC. Is this correct?


Another example:

Everything is similar as above, except that now my Test class is a Servlet. Once I add products to order object, I store order into a session. After setting order in a session I do aList = null;

What will happen in this case? The aList object will be available for GC, but the elements of aList are still referenced (stored in order object which is in a active session). So the products stored in aList will NOT be available for GC. Is my understanding correct?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic