7

What I am asking might be a stupid question so please pardon me for that. So it goes like this :

List<Boss> bossList = new ArrayList<Boss>();
Boss b = null;
for(Employee e : List<Employee> myList){
    b = new Boss();
    b.setEmployee(e);
    bossList.add(b);
    b = null;
}

So in above scenario, I am creating lot of Boss objects and then de-referencing them(I know I don't need to write "b = null", but i did it for clarity of my question). In normal scenario, I would have marked them to garbage collection, by doeing this, but because in this scenario, I am adding those Boss objects in List collection, are they marked for GC or not? If not then why? And how does List collection work internally to hold references for each Object added, so as to avoid garbage collection?

[EDIT] 

The scope of question is only limited to the individual Boss objects created in for loop, considering that this method returns the reference of the List to the outside world.

2
  • The object itself, which you put in the list, is obviously not available for GC since a reference to it still exists, even if you nullify b.
    – NilsH
    May 31, 2013 at 5:38
  • An object cannot be garbage collected if there is any way in the current program state for it to be reached (excluding through the special class WeakReference and its related classes). One reference, no matter where, from live code is enough.
    – Patashu
    May 31, 2013 at 5:38

4 Answers 4

10

The Boss objects will not be collected by the GarbageCollector because they are still referenced in the code block that you are posted. bossList is an ArrayList which has an internal array of Object thus holding references to those objects which are added to it.

I such a situation not only the references by you are considered but all referneces in all objects involved.

EDIT: Since you are returning the List in your code the objects will not be marked for garbage collection until the list is no longer referenced in your program.

8
  • But since the List<Boss> is declared inside the method, then when the method finishes, the list would be marked for GC, thus the Boss object references will be marked as well. May 31, 2013 at 5:40
  • Correct. As soon as the block in which the ArrayList is declared terminates all objects will be collected (if not further referenced). May 31, 2013 at 5:42
  • Sure, if you're assuming that the list only exists within the method.
    – NilsH
    May 31, 2013 at 5:43
  • 1
    @system32 if these objects aren't referenced in other part of your program, then yes, they will be marked for GC. May 31, 2013 at 5:44
  • 1
    @DanielLerps then you should specify that in your answer since OP hasn't defined this in the question. May 31, 2013 at 5:45
5

ArrayList has Object[] elementData internally. When you added b to bossList ArrayList assigned elementData[0] = b. So when you assigned null to b the instance of Boss is still referenced from elementData[0] and cannot be GCed. But since ArrayList instance is referenced only from method's variable after the method returns both ArrayList and Boss instances will be eligible for GC.

3

Here's what really happens with your code :

Hello

-1

Since java is pass by reference, whenever you add b to bossList, bossList starts referencing the memory location which b is pointing to. So when b nullified only link from b to the reference is broken. Thus keeping the object accessible through bossList.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.