I am doing Diagnostic Exam from Devaka's Exams. I am feeling difficulty in Garbage Collection. Can anyone explain me how to proceed to get how many objects are eligible for garbage collection after Line 12....
When you initialize a2 at Line 8 you are passing a new object A to the constructor and then, the instance variable "a" of a2 refers to that new object.
In Line 12, that instance variable is set to null, so there is no reference to that isolated object.
The rest of the objects still have a reference to them.
(I think I'm having problems explaining myself :lol: )
Ankit Garg wrote:
and after execution of a2.a=null; //Line12 , this happens
So this should happen
Banu Chowdary wrote:
I am feeling difficulty in Garbage Collection. Can anyone explain me how to proceed to get how many objects are eligible for garbage collection
The way Ankit explained it..........that's exactly the way to go about solving GC questions.
Always draw the objects on paper as they are created and make a cross when their reference is removed in the code.
In such a way, in the end you can see how many objects are left isolated.
If Ankit can do it in the message, you should be able to do it on paper.......
Practice a lot(on paper). You should do fine after some experience.
You really explain well, such types of questions......on the forum....
IN both the code the object created is eligible for GC. because there was only one reference and that is also set to null.
The only difference is that in first code you are requesting GC to do its work.
I the second one. Its left to GCr. It would do if it feels like doing GC...
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !