• 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

Doubt regarding Garbage Collection found in K&B OCP Java 6 Practice Exams Book

 
Greenhorn
Posts: 27
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I would like to know your opinion on other doubt about a question of K&B OCP Java 6 Practice Exams Book. The question is #28 of the Practice Exam 3.

The book says that the correct answer to the question "how many objects are eligible for garbage collection when line 5 (//do more stuff) is reached?" is:

D. Four objects are eligible for garbage collection.

The explanation is

D is correct because only one Account object is created, and a1, a2, and a3 all refer to it. The Account object has two Long objects associated with it, and finally the anonymous Banker object is also eligible.


In my opinion there are 5 objects eligible for garbage collection. Apart from those mentioned by the book, I think that the 5th eligible object is the one created in the following line: The longValue() method returns a long primitive, which will be autoboxed (and therefore a new Long object is created) to be assigned to a3.password. As the next line assigns a new Long object to the Account's password field, the "old" object is eligible for garbage collection in that very moment. Any thoughts?
 
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are onto something here. As far as I can tell, you are correct.

Note that there's also a String created, but it won't be collected because it goes to the String pool.
 
Francisco J. Bermejo
Greenhorn
Posts: 27
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I think you are onto something here. As far as I can tell, you are correct.

Note that there's also a String created, but it won't be collected because it goes to the String pool.


Hi Stephan! Yes, I'm aware of the String literal going to the String pool . Let's see what the other folks think. Thanks for your answer!
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Difficult question. I thought the Wrapper also has a pool (therefore the Method valueOf).
Only when new is called a new Object is created if not its up to the Wrapper to use the pool or create something new.
 
Francisco J. Bermejo
Greenhorn
Posts: 27
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

micha koern wrote:Difficult question. I thought the Wrapper also has a pool (therefore the Method valueOf).
Only when new is called a new Object is created if not its up to the Wrapper to use the pool or create something new.


Hi again micha, I think that you're referring to the (a kind of) wrapper pool which can hold Boolean, Byte, Short (from -128 to 127), Character (up to '\u007f'), and Integer objects (from -128 to 127), created by means of autoboxing. It never holds Long objects (to my knowledge). Even though it would also hold Longs, the value we are discussing about is by far too large (1024).
 
micha koern
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh Right!
Try this:


The output is false, so there is really a new Object- i agree 5 Objects are eligible.
 
Francisco J. Bermejo
Greenhorn
Posts: 27
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

micha koern wrote:Oh Right!
Try this:


The output is false, so there is really a new Object- i agree 5 Objects are eligible.



Anybody else?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Who says the password field is a Long?

I am looking at it being a long. And therefore no 5th object. But that is based on an assumption that the password field is a primitive.

and therefore

a2.password = 4455L;

is not creating a new Long object either.

Mark
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the very top Mark. password and acctNum are both Long.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the inconvenience, but I posted by mistake my initial thought. But after thinking a bit more I have a new explanation, which makes more sense in my opinion (so I had to delete this post).

It's true that a total of 5 objects are created, but when reaching line 5 just 4 will be eligible for garbage collection (because 1 of the created objects became already eligible on another time, so this object could already be garbage collected when reaching line 5).

An overview of these 5 objects with some comments:
  • the Account-object (referred to by a1, a2 and a3)
  • the Long-object containing 1024 (and assigned to by a1.acctNum)
  • a1.acctNum.longValue() will result in a long primitive, but this primitive is autoboxed to a new Long-object (and then assigned to a3.password)
  • 4455L will (thanks to autoboxing) result in another Long-object which is assigned to a2.password (and because a2 and a3 are referring to the same object, the Long-object created in the previous line becomes eligible for GC at this line)
  • the anonymous Banker-object (on line 4)


  • So maybe only the explanation why 4 is the correct answer needs some adjusting
     
    Francisco J. Bermejo
    Greenhorn
    Posts: 27
    Eclipse IDE Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Roel De Nijs wrote:
    It's true that a total of 5 objects are created, but when reaching line 5 just 4 will be eligible for garbage collection (because 1 of the created objects became already eligible on another time, so this object could already be garbage collected when reaching line 5).



    I understand what you mean. Until now I simply assumed (wrongly) that in this kind of questions the garbage collector did not run until the asked line is reached. Thank you very much for your time Roel!
     
    Master Rancher
    Posts: 4796
    72
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I think it's technically ambiguous whether the autoboxing of 1024L and 4455L will result in a newly-created object or a cached value. The JLS essentially requires that certain values for certain types are cached - but the language is deliberately vague in that nothing says the JVM can't cache other values as well. So if you're dealing with any boolean, byte or short, a char in the range 0 - 255, or an int in the range -128 - 127, the boxed value must be cached. But it's entirely possible that other values are cached as well. As far as the JLS and API are concerned, we just don't know.

    Now, it's possible to look at how this is implemented in current JDKs. Unboxing is done with methods like Integer.valueOf() and Long.valueOf() - you can look at the implementations of these methods to see what they do. And it turns out that the current (JDK 6) implementation of Long.valueOf() does cache some values - but only between -128 and 127. However, that's more than they are required to do at all. And it's entirely possible that some future JDK may decide to cache more of them, e.g. if memory becomes even cheaper. It's even possible that happens on some current JDK. So, the answer to "does autoboxing the number 1024L create a new Long instance?" is: probably, but we don't really know. It's implementation-dependent. Same for 4455L.

    As for the question of when exactly these objects are eligible for garbage collection: I think it only makes sense to list all objects that have recently become eligible. Not just the latest ones. Because really, one could just as well argue that most of those objects became eligible at line 14, or at the end of line 4, not at line 5. If you try to make such distinctions, everything becomes far too ambiguous. Much better to ask: how many objects have become eligible by line 5? And my answer would be, 3 to 5. Though it's definitely 5 on my current JDK, I can't say what it will be for all JDKs.
     
    author
    Posts: 9050
    21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Guys,

    After a brief review I think there is a problem with this question, as you all have been discussing

    At this point, I think I'm going to say that there are indeed 5 objects eligible.

    As far as the point concerning whether the Long's might be in a pool, at this point I'm tending towards sticking to my guns and saying that each of the Long's created in this question will be a new object.

    Thanks for the keen eyes you guys!

    What I will say is that everyone who has been posting on this thread is MORE than ready to handle the GC questions on the real exam.

    Bert
     
    Francisco J. Bermejo
    Greenhorn
    Posts: 27
    Eclipse IDE Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi again! Bert and others confirmed that 5 objects are eligible for GC, but I'm very confused now. This is because after reading Roel's answer I thought that:

    Francisco J. Bermejo wrote:Until now I simply assumed (wrongly) that in this kind of questions the garbage collector did not run until the asked line is reached.


    And by chance I found this question from the K&B Study Guide that (apparently) confirmed my new thoughts. It's question 1 from the 3rd Chapter:

    This question asks us hoy many objects are eligible for GC when // do Stuff is reached. According to the book the correct answer is C. (2 objects), because:

    "Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible."



    If there are 5 objects eligible in my previous question, why aren't 4 objects eligible in this (c1, c2 and their respective Short objects)?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15485
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The object referenced by c2 is still reachable (through c2).
     
    Francisco J. Bermejo
    Greenhorn
    Posts: 27
    Eclipse IDE Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:The object referenced by c2 is still reachable (through c2).


    Oops, you are completely right!! Thanks very much Stephan
     
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi all ,

    i think the answer given by book is correct. For an Account object here , we can create only 2 Long objects.

    Declared as
    class Account{
    Long acctNum;
    Long password;
    }

    The one and only password(Long) object is initialized first time as
    a3.password = a1.acctNum.LongValue();
    and then
    a2.password = 4455L;
    here all a1.password , a2.password and a3.password refers to same Long object of Account object.

    I think there is no significance of line 12 because a1.password( a1.password or a2.password or a3.password) is
    initialized again at line 13.
    How could we create 3 Long objects for one Account object???
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Milind Vaishnaw wrote:How could we create 3 Long objects for one Account object???


    Did you read (and understand) every post in this thread? I gave (a few posts above) a complete overview of the 5 objects which are created in the code snippet.

    But just to prove that the number of reference variables gives no indication of the number of objects that are created, I give this example:

    You have just 1 reference variable myLong, but you have created 1000 Long-objects (of which 999 are aligible for GC).
     
    Ranch Hand
    Posts: 394
    Eclipse IDE Oracle Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Milind Vaishnaw wrote:Hi all ,

    i think the answer given by book is correct. For an Account object here , we can create only 2 Long objects.

    Declared as
    class Account{
    Long acctNum;
    Long password;
    }

    The one and only password(Long) object is initialized first time as
    a3.password = a1.acctNum.LongValue();
    and then
    a2.password = 4455L;
    here all a1.password , a2.password and a3.password refers to same Long object of Account object.

    I think there is no significance of line 12 because a1.password( a1.password or a2.password or a3.password) is
    initialized again at line 13.
    How could we create 3 Long objects for one Account object???



    Hello Milind And Hello Friends, as usual I decided to stay with my practice of drawing a diagram and getting a clear
    picture of the situation. We all are correct! 5 objects are eligible for garbage collection when line 5 is reached.

    @Millind, just incase you are NOT very clear about line 12. READ CAREFULLY what Francisco Bermejo explained: "The longValue() method returns a long primitive, which will be autoboxed (and therefore a new Long object is created) to be assigned to a3.password. As the next line assigns a new Long object to the Account's password field, the "old" object is eligible for garbage collection in that very moment."

    Take a close look at the diagram and I guess you will get a clearer picture of what happened.

    I simply want to mention what I studied in the K & B book (Chapter 3, page 184 & 185) local variables LIVE on the stack; hence when line 5 is reached the go() method completes and is removed from the stack, local reference variables a1, a2, a3 disappear too. From then on we can guess what happens to the Objects in 'The Heap'...I hope this helps.

    One more thing; I advise the use of diagrams, practice drawing diagrams, this one took me 5mins to create with paint
    and 1min. to draw with pencil and paper, REMEMBER that for the real exams an average of NO MORE than 3mins should
    be spent to answer each question.
    Account_Banker.png
    [Thumbnail for Account_Banker.png]
    Diagram
     
    Milind Vaishnaw
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @Ikpefua Thank You sir...now its clear.
     
    Ranch Hand
    Posts: 241
    5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for correcting the answer in the book, Bert. I thought 5 too... just as well this forum exists and I could look it up straight away
     
    Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic