Is this java Object eligible for garbage collection in List

后端 未结 4 582
难免孤独
难免孤独 2021-01-18 05:59

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

List bossList = new ArrayList();
Bos         


        
4条回答
  •  时光取名叫无心
    2021-01-18 07:05

    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.

提交回复
热议问题