kundana sharma wrote:
At line 20 how many objects are available for Garbage collection?
My Ans is 2 objects will be Garbage Collected.But some say it to be 4.
It can't be 4, since only 3 objects are ever created.
My Solution:
s1--->SO1. At line 13.Lets acronym Sparrow Object as SO;
s2--->SO2. At line 14.
s3--->SO3. At line 15.
Correct so far
After line 16:s1--->SO3 ,s2--->SO2,s3--->SO3.So SO1 available for GC.
No.
s1-->SO1;s2-->SO2;s3-->SO1. So SO3 eligible for GC.
After line 17:s1--->SO3 ,s2--->SO3,s3--->SO3.So SO2 available for GC.
s1-->SO2;s2-->SO2;s3-->SO1. So still only SO3 eiligible for GC.
After line 18:s1--->SO3 ,s2--->null,s3--->SO3.
s1-->SO2;s2-->nothing;s3-->SO1. So stillonly SO3 eligible for GC.
After line 19:As both s1 and s3 refer to same object, nothing much wrt GC is going to happen in this line.
s1-->SO2;s2-->nothing;s3-->SO2; So now SO1 and SO3 are eligible for GC.