• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
My Solution:
s1--->SO1. At line 13.Lets acronym Sparrow Object as SO;
s2--->SO2. At line 14.
s3--->SO3. At line 15.
After line 16:s1--->SO3 ,s2--->SO2,s3--->SO3.So SO1 available for GC.
After line 17:s1--->SO3 ,s2--->SO3,s3--->SO3.So SO2 available for GC.
After line 18:s1--->SO3 ,s2--->null,s3--->SO3.
After line 19:As both s1 and s3 refer to same object, nothing much wrt GC is going to happen in this line.
Is this correct? some say 4 objects will be Garbage collected.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please always tell us where such questions come from.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic