aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt regarding Garbage Collection found in K&B OCP Java 6 Practice Exams Book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt regarding Garbage Collection found in K&B OCP Java 6 Practice Exams Book" Watch "Doubt regarding Garbage Collection found in K&B OCP Java 6 Practice Exams Book" New topic
Author

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

Francisco J. Bermejo
Greenhorn

Joined: Mar 02, 2011
Posts: 27

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?

OCPJP 6 96%
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

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

Joined: Mar 02, 2011
Posts: 27

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!
micha koern
Greenhorn

Joined: Mar 25, 2011
Posts: 23
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

Joined: Mar 02, 2011
Posts: 27

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

Joined: Mar 25, 2011
Posts: 23
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

Joined: Mar 02, 2011
Posts: 27

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?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

Look at the very top Mark. password and acctNum are both Long.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5271
    
  13

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

    SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
    http://www.javaroe.be/
    Francisco J. Bermejo
    Greenhorn

    Joined: Mar 02, 2011
    Posts: 27

    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!
    Mike Simmons
    Ranch Hand

    Joined: Mar 05, 2008
    Posts: 3014
        
      10
    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.
    Bert Bates
    author
    Sheriff

    Joined: Oct 14, 2002
    Posts: 8815
        
        5
    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


    Spot false dilemmas now, ask me how!
    (If you're not on the edge, you're taking up too much room.)
    Francisco J. Bermejo
    Greenhorn

    Joined: Mar 02, 2011
    Posts: 27

    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
    Bartender

    Joined: Sep 20, 2010
    Posts: 3647
        
      16

    The object referenced by c2 is still reachable (through c2).
    Francisco J. Bermejo
    Greenhorn

    Joined: Mar 02, 2011
    Posts: 27

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

    Oops, you are completely right!! Thanks very much Stephan
    Milind Vaishnaw
    Greenhorn

    Joined: Mar 26, 2011
    Posts: 2
    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
    Bartender

    Joined: Jul 19, 2004
    Posts: 5271
        
      13

    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).
    Ikpefua Jacob-Obinyan
    Ranch Hand

    Joined: Aug 31, 2010
    Posts: 394

    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.



    [Thumbnail for Account_Banker.png]



    OCPJP 6.
    In Your Pursuit Towards Certification, NEVER Give Up.
    Milind Vaishnaw
    Greenhorn

    Joined: Mar 26, 2011
    Posts: 2
    @Ikpefua Thank You sir...now its clear.
    Chandella Montero
    Ranch Hand

    Joined: Feb 18, 2011
    Posts: 89
    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


    OCA, OCP Java 6
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Doubt regarding Garbage Collection found in K&B OCP Java 6 Practice Exams Book