• 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 in java

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


how many object here are eligible for garbage collection? to this question answer is 2. can somebody please explain how it counts up to two.
I can understand that c1 is one what is the other one and how. please help
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vidya sagari wrote:how many object here are eligible for garbage collection?

after what line of code? please always Quote Your Sources
 
Vidya sagari
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vidya sagari wrote:

after doStuff is reached, how many object here are eligible for garbage collection? to this question answer is 2. can somebody please explain how it counts up to two.
I can understand that c1 is one what is the other one and how. please help
[it is a question from OCPJP practice questions]

 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At line code object whose reference was stored in reference variable c1 will be eligible for GC.
  • If there is no code that uses reference of object of CardBoard stored in c2 reference variable then at line where comment //doStuff is written, an object whose reference is stored in reference variable c2 will also be eligible for GC.
  • So conclusion is at line //doStuff two objects will be eligible for GC.
  •  
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    See this diagram
    GC.png
    [Thumbnail for GC.png]
     
    Rancher
    Posts: 4801
    50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Only two objects are created and, at the comment line, only one is eligible, surely.
    Where does the reference 'c2' stop pointing at its original object?
    At '//do stuff' it's still active.

    ETA:
    Ah...missed the Short attribute.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Dave Tolls wrote:Where does the reference 'c2' stop pointing at its original object?
    At '//do stuff' it's still active.

    I think I should have said at line 15 where the main method ends
     
    Ranch Hand
    Posts: 175
    17
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ganish Patil wrote:At line code object whose reference was stored in reference variable c1 will be eligible for GC.


    I agree with this

    Ganish Patil wrote:...at line where comment //doStuff is written, an object whose reference is stored in reference variable c2 will also be eligible for GC


    I don't agree with this.

    The program creates 4 objects:
  • the object referenced by c1 (a CardBoard object)
  • the object referenced by c1.Story (a Short object)
  • the object referenced by c2 (a CardBoard object)
  • the object referenced by c2.Story (a Short object)

  • At line //doStuff, the 2 objects that are eligible for GC are:
  • the object referenced by c1 (a CardBoard object) - because c1 is null
  • the object referenced by c1.Story (a Short object) - due to island of isolation

  • Ganish Patil wrote:I think I should have said at line 15 where the main method ends


    At line 15 where the main method ends, 4 objects are eligible for GC.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Joe Bishara wrote:

    Ganish Patil wrote:At line code object whose reference was stored in reference variable c1 will be eligible for GC.


    I agree with this

    Ganish Patil wrote:...at line where comment //doStuff is written, an object whose reference is stored in reference variable c2 will also be eligible for GC


    I don't agree with this.

    The program creates 4 objects:
  • the object referenced by c1 (a CardBoard object)
  • the object referenced by c1.Story (a Short object)
  • the object referenced by c2 (a CardBoard object)
  • the object referenced by c2.Story (a Short object)

  • At line //doStuff, the 2 objects that are eligible for GC are:
  • the object referenced by c1 (a CardBoard object) - because c1 is null
  • the object referenced by c1.Story (a Short object) - due to island of isolation

  • Ganish Patil wrote:I think I should have said at line 15 where the main method ends


    At line 15 where the main method ends, 4 objects are eligible for GC.


    I was wrong, completely agreed with you,  
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Mistakes I made,
  • Completely, I missed It is Short not short so total 4 objects were created.
  • At //doStuff line second object of CardBoard was still referenced by c2.
  • Thank you so much Joe Bishara, for correcting me
     
    Joe Bishara
    Ranch Hand
    Posts: 175
    17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You’re welcome  
     
    Vidya sagari
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    SO Do you mean that the answer is actually 4? I am new to java and I don't understand where and how a Short object is created. can you please elaborate on that?
     
    Joe Bishara
    Ranch Hand
    Posts: 175
    17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Vidya sagari wrote:SO Do you mean that the answer is actually 4?


    No. The answer to the original question is 2. At the line containing the //doStuff comment, the 2 objects that are eligible for GC are:
  • the object referenced by c1 (a CardBoard object)
  • the object referenced by c1.Story (a Short object)

  • Vidya sagari wrote:I don't understand where and how a Short object is created. can you please elaborate on that?


    The source code shows that every CardBoard object has-a Short object.
     
    Vidya sagari
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    WOW!!! thank you so much. I finally got the thing.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Eclipse IDE MySQL Database Tomcat Server Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Vidya sagari wrote:SO Do you mean that the answer is actually 4? I am new to java and I don't understand where and how a Short object is created. can you please elaborate on that?


  • See here Short is a wrapper class.
  • CardBoard has a field named Story whose type is Short so when you create an object of CardBoard then one object of Short class is also gets created.
  • You can see we created two objects of CardBoard class i.e. c1 and c2 where each of them have one object of Short referred by Story variable, so total 4 objects are created. i.e. Two objects of CardBoard and two objects of Short.
  • Your question was how many objects are eligible for GC on line where you have comment //do Stuff  so till that line one object of CardBoard is eligible for GC i.e. object of CardBoard referred by c1 and you know that object also contains one object of Short referred by Story variable so this Short object also becomes eligible for GV means total 2 objects are eligible for GC i.e. one object of CardBoard and one object of Short.
  • Object of CardBoard referred by c2 will not be eligible at line where comment  //do Stuff is, because that second object of CardBoard is still refered by c2 varable. so your answer is 2 objects will be eligible on line where comment //do Stuff is.
  •  
    Vidya sagari
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for explaining
     
    reply
      Bookmark Topic Watch Topic
    • New Topic