class Car {} class Ferrari extends Car {} class Camry extends Car {}
public class GC { public static void main(String[] args) { Car a1 = new Ferrari(); Car a2 = new Camry(); Car a3 = a1; a1 = null; Car a4 = a1; Car a5 = a2; a1 = a2; a2 = a4; a5 = null; a3 = a1;
// Here } }
My doubt is while GC wether we must count reference varaibles equal to null or count objects without any references .Please someone help me in understanding this.
I guess objects not pointed by any reference variables are eligible for gc.we garbage collect objects not reference variables. for example if an object is pointed by 2 variables and one of them is set to null,it is still being pointed by the other and hence will not be collected.
gc can also be called when there exists an island of isolation.but that is not the case with your code.by the time the execution reaches //Here all reference variables are null,hence both objects will be eligible for collection
I agree that a1 and a3 are still pointing to Camry and Ferrari is eligible FWIW, I have to draw pictures for the GC stuff. I use solid lines to objects and then when a reference 'breaks away' I use dotted lines. Works for me.
I agree that a1 and a3 are still pointing to Camry and Ferrari is eligible FWIW, I have to draw pictures for the GC stuff. I use solid lines to objects and then when a reference 'breaks away' I use dotted lines. Works for me.
Thanks for all your reply... Only ferrari is eligible for Gc...means 1 object... We should not consider reference varaible a2=a4=a5=null into count.Because if count them then its 4 objects available for GC.
Please someone tell me whether whatever i have understood is correct.
I agree that a1 and a3 are still pointing to Camry and Ferrari is eligible FWIW, I have to draw pictures for the GC stuff. I use solid lines to objects and then when a reference 'breaks away' I use dotted lines. Works for me.