Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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: 76489
366
  • 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: 23928
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: 76489
366
  • 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?
 
The moustache of a titan! The ad of a flea:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic