• 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

Problem in question from K&B

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please refer question no 2 of chapter 3 from K&B for scjp 5(page number 259)

class CardBoard
{
Short story =5;
CardBoard go(CardBoard cb)
{
cb=null; return cb;
}
public static void main(String args[])
{
CardBoard c1=new CardBoard();
CardBoard c1=new CardBoard();
CardBoard c1=c1.go(c2);
c1=null;
//do stuff
}
}
after do stuff how many objects are eligible for garbage collection??

it should be two ... one for c1 and another for c3
but in explanation it is not???
please explain me this....

i am not getting garbage collection very well please help me.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



... compiler will give error, as c1 is declared thrice.

What is the answer and explanation given in K&B?
 
vikky agrawal
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I m so sorry i`ve just coied and pasted that lines of code
now this is right code
please explain this now




class CardBoard
{
Short story =5;
CardBoard go(CardBoard cb)
{
cb=null; return cb;
}
public static void main(String args[])
{
CardBoard c1=new CardBoard();
CardBoard c2=new CardBoard();
CardBoard c3=c1.go(c2);
c1=null;
//do stuff
}
}
after do stuff how many objects are eligible for garbage collection??

 
Byju Joy
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output:
c1=CardBoard@3e25a5
c2=CardBoard@19821f
Finalized objectscjp.CardBoard@3e25a5

That means c1 is eligible for garbage collection at line marked do stuff.
 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After doStuff 3 objects will be eligible for garbage collection.

  • c1
  • cb
  • and
  • c3


  • Short has a strong reference so its not eligible for garbage collection
    c1 is explicitly assigned a null value. so it's eligible for GC
    c2 has a strong reference. so its not eligible for GC
    c2 is passed to go method and the reference cb is refering the same object c2. Inside go method, cb is explicitly assigned to null. so it is eligible for GC
    The reference c3 is assigned to null as c1.go(c2) returning null. so it is also eligible for GC.

    So as per my understanding, these 3 objects will be eligible for GC. What is the answer in your book?


    sat
     
    satishkumar janakiraman
    Ranch Hand
    Posts: 334
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    one small correction in my previous post

    the reference cb is local to the method. so the objects c1 and c3 are eligible for GC after doStuff method

    sat
     
    Byju Joy
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    c3 is not an object. Or in other words this reference doesn't point to any real objects. We see only two 'new' - one for c1, another for c2. Which means we can't count c3 as an 'object' eligible for GC. The code I pasted above shows 'finalize' method is called only once. So answer will be 1 object, instead of 2 objects.
     
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    When was doing this excercise I answered also one object, but in the book, it says that the object 'Short story' is also eligible for GC, I did not know that, but it sounds ok...
     
    Byju Joy
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    'Short story' object is eligible for GC only after exit of main method - when the class of off loaded. But in this questions 'do stuff' is marked before the closure of main method. So I believe 'story' object is eligible for GC yet.
     
    Ranch Hand
    Posts: 317
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Byju Joy wrote:'Short story' object is eligible for GC only after exit of main method - when the class of off loaded. But in this questions 'do stuff' is marked before the closure of main method. So I believe 'story' object is eligible for GC yet.


    I don't see how any live thread can access the "short story" object. So I guess two objects is the proper answer.

    cheers
    Bob
     
    vikky agrawal
    Ranch Hand
    Posts: 65
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @byju joy

    you posted that c1 is elegible for gc
    but in book it is given that short object is eligible for gc??

    your finalize method says that c1 is eligible for gc???
    i am not getting it properly?
     
    Byju Joy
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    I don't see how any live thread can access the "short story" object. So I guess two objects is the proper answer.



    The 'main' thread is still alive at the line marked 'do stuff'. This thread dies only after completion of main method. The line with closing brace of main method makes much difference. If 'do stuff' was marked after closing brace then 'story' object is also eligible for GC.

    So I stick to my answer that only one object is eligible for GC - which is 'c1'.
     
    Ranch Hand
    Posts: 710
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Byju Joy wrote:

    I don't see how any live thread can access the "short story" object. So I guess two objects is the proper answer.



    The 'main' thread is still alive at the line marked 'do stuff'. This thread dies only after completion of main method. The line with closing brace of main method makes much difference. If 'do stuff' was marked after closing brace then 'story' object is also eligible for GC.

    So I stick to my answer that only one object is eligible for GC - which is 'c1'.



    There is a Short wrapper object associated with every CardBoard object. When one of those CardBoard objects becomes eligible for gc all (for the sake of this discussion, may be some random exceptions to this) the associated objects within it become eligible also, making the c1 Short object eligible too.
     
    Byju Joy
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    There is a Short wrapper object associated with every CardBoard object. When one of those CardBoard objects becomes eligible for gc all (for the sake of this discussion, may be some random exceptions to this) the associated objects within it become eligible also, making the c1 Short object eligible too.



    ...ah, that was right. So the correct answer is two objects ready for GC: c1, and c1.story.
     
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Byju Joy wrote:
    ...ah, that was right. So the correct answer is two objects ready for GC: c1, and c1.story.



    There is actually an errata item for this question. In the new version, the short value is larger. With this version, the short value of 5 isn't eligible for GC, because autoboxing will get the the Short object from the cache.

    Henry
     
    W. Joe Smith
    Ranch Hand
    Posts: 710
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Henry Wong wrote:

    Byju Joy wrote:
    ...ah, that was right. So the correct answer is two objects ready for GC: c1, and c1.story.



    There is actually an errata item for this question. In the new version, the short value is larger. With this version, the short value of 5 isn't eligible for GC, because autoboxing will get the the Short object from the cache.

    Henry



    Good to know!
     
    Bob Wheeler
    Ranch Hand
    Posts: 317
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Henry Wong wrote:

    Byju Joy wrote:
    ...ah, that was right. So the correct answer is two objects ready for GC: c1, and c1.story.



    There is actually an errata item for this question. In the new version, the short value is larger. With this version, the short value of 5 isn't eligible for GC, because autoboxing will get the the Short object from the cache.

    Henry


    Sorry, I am a little bit lost. I know that two Integer or Short objects with the same value within the range of -128 to 127 are regarded as equals under "==". Does this mean that c1.story is no object at all? Is it a primitive? What is it? Because if it is an object, it would be eligible for gc!?!

    cheers
    confused Bob
     
    reply
      Bookmark Topic Watch Topic
    • New Topic