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.

Ameer Moaaviah
  • 1,530
  • 12
  • 27
sunny_dev
  • 765
  • 3
  • 15
  • 34
  • 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 '13 at 05: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 '13 at 05:38

4 Answers4

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.

Daniel Lerps
  • 5,256
  • 3
  • 23
  • 33
  • But since the `List` 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. – Luiggi Mendoza May 31 '13 at 05:40
  • Correct. As soon as the block in which the `ArrayList` is declared terminates all objects will be collected (if not further referenced). – Daniel Lerps May 31 '13 at 05:42
  • Sure, if you're assuming that the list only exists within the method. – NilsH May 31 '13 at 05:43
  • 1
    @system32 if these objects aren't referenced in other part of your program, then yes, they will be marked for GC. – Luiggi Mendoza May 31 '13 at 05:44
  • if List object is returned by the method to the outside world then ofcourse it will not be GCed. Ofcourse the point of question is not aimed at the List object, but rather at the Boss objects created in FOR loop. – sunny_dev May 31 '13 at 05:45
  • 1
    @DanielLerps then you should specify that in your answer since OP hasn't defined this in the question. – Luiggi Mendoza May 31 '13 at 05:45
  • Edited the answer according to suggestions of BheshGurung and LuiggiMendoza and the updated question. – Daniel Lerps May 31 '13 at 05:55
  • @DanielLerps Generally speaking, no one object will be eligible for GC till there is any link to it (directly or through other objects) in any existent stack. – Illia Shestakov May 31 '13 at 06:29
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.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
3

Here's what really happens with your code :

Hello

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
WoooHaaaa
  • 19,732
  • 32
  • 90
  • 138
-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.

Ameer Moaaviah
  • 1,530
  • 12
  • 27