aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock question from SCJP book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock question from SCJP book" Watch "Mock question from SCJP book" New topic
Author

Mock question from SCJP book

Marcus Moreno
Ranch Hand

Joined: Aug 17, 2008
Posts: 58
Hi,

I have a question to an mock exam question of the book " SCJP � Sun Certified Java Programmer " by Terence Gronowski.

As the entire question including the variable names are in german, I've adapted it so that you too may understand it:

public static void main(String[] args){
MyObject m0 = new MyObject();
MyObject m1 = new MyObject();
MyObject m2 = new MyObject();

m0.mh = m1;
m1.mh = m2;
m2.mh = m0;
m0 = new MyObject(); // "the reason"
m0 = m1 = m2;
System.gc(); //line 1
}

Actually the entire book is full of bugs, as well as this question, as it asks "How many objects exist before and after the call to System.gc()?"

The question itself is at least missleading, if not wrong as you can't not tell the JVM to garbage collect, in no way. System.gc() is just a suggest to the JVM to gc.
Anyway, let's assume the question was:
"How many objects exist before "line 1", and how many objects are eligible for gc after "line 1".

Imho 4 objects exist before , and 2 objects are eligible for gc afterwards.
The reason is the line I marked "the reason" is creating another object, while the object, that was formally assigned to m0 still exists as m2 is still pointing to it. However in the line "m0 = m1 = m2;" two objects get lost.

So the answer should be "4,2" imho - however the author writes the answer was "4,3"


Thanks,

Marcus
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
As per my understanding, there is only one object eligible for GC.

When this code completes, following are the references:

1st object--referred by 3rd object's mh reference
2nd object--referred by 1st object's mh reference
3rd object--referred by m0,m1,m1's mh,m2.

The object created in the line commented "reason" is the only object eligible for GC
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

I think that the author is correct in this case.



In the code "here" all the previous references to the other objects are lost and only the reference to m2 object remains in m0, m1, m2. Thus there are 4 objects created and after the call to system.gc() only one object remains and the other 3 are eligible for garbage collection.

Hope i am clear


-kolli
Vaes Bart
Greenhorn

Joined: Oct 28, 2008
Posts: 13

At first glance, I agree with Rekha - only 1 object eligible for GC after line 1.
Marcus Moreno
Ranch Hand

Joined: Aug 17, 2008
Posts: 58
But m2 still points to the object that formally was referenced by mo:
m2.mh = m0;
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

I am sorry it was my mistake, I actually forgot about the "mh" of m2.

Before "line 1" there are 4 objects created, while after "line 1" 2 objects are available for garbage collection.

Your answer of 4, 2 is correct in this case.
Marcus Moreno
Ranch Hand

Joined: Aug 17, 2008
Posts: 58
Anyone else who can cover this?

That book is really DANGEROUS (and should be forbidden by law for this reason) - E.g. it even "proofs" the author's answer "4,3" by printing out
how many times the finalize Method was called - forgetting the fact that
the garbage collector won't always obey to "System.gc()"...
Tomasz Kurpios
Greenhorn

Joined: Oct 31, 2008
Posts: 10
Hi,
I'm afraid you're wrong.

To start with, we can never assume that there is any object eligible for GC after the call to . I do understand, however, that you (and book author) actually meant commenting out whole line:



OK, so let's go back to the point. There is only 1 object eligible for GC after this commented line. Here's the explanation:

m0 references the third created object (so do m1 and m2)
m0.mh references the second created object (so do m1.mh and m2.mh)
m0.mh.mh references the first created object (so do m1.mh.mh and m2.mh.mh)

There is no reference to the fourth object.

Pozdrowienia //Regards
Marcus Moreno
Ranch Hand

Joined: Aug 17, 2008
Posts: 58
First of:
To start with, we can never assume that there is any object eligible for GC after the call to


Is excactly what I wrote... Hmm...


There is only 1 object eligible for GC after this commented line. Here's the explanation:

m0 references the third created object (so do m1 and m2)
m0.mh references the second created object (so do m1.mh and m2.mh)
m0.mh.mh references the first created object (so do m1.mh.mh and m2.mh.mh)


Nope. Here's my explanation:
1 MyObject m0 = new MyObject("a");
2 MyObject m1 = new MyObject("b");
3 MyObject m2 = new MyObject("c");
4
5 m0.mh = m1;
6 m1.mh = m2;
7 m2.mh = m0;
8 m0 = new MyObject("d");
9 m0 = m1 = m2;

In line 1) mo References to object "a". In line 8), it references to the new object "d". The old object "a" is still referenced by object "c", which is referenced by m2. mo, m1 and m2 reference Object "c", which again references object "a", so two objects may be garbage collected, and 2 objects HAVE to remain on the heap.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

when

m0.mh = m1;
m1.mh = m2;
m2.mh = m0;

is executed, the memory structure looks like this



after execution of

m0 = new MyObject();

the memory structure looks like this



and finally when

m0 = m1 = m2;

is executed, the memory structure looks like this



So as can be seen, the object created at step 2 i.e. m0 = new MyObject(); will be eligible for garbage collection...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Tomasz Kurpios
Greenhorn

Joined: Oct 31, 2008
Posts: 10
Nope

You are forgetting that object "a" still has reference to object "b" through mh. If you don't believe me add this line after line 9 and execute the whole code:



If you override the toString() method so that it returns the String you passed to the constructor of MyObject class, you'll get the following output:


So these three objects are still accessible by a living thread.

Pozdrowienia //Regards
Tomasz Kurpios
Greenhorn

Joined: Oct 31, 2008
Posts: 10
My previous post refers to Marcus Moreno not Ankit Garg (who I totally agree with).

Pozdrowienia //Regards
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

wow!! i got it now. I actually forgot about the referencing with "mh".

Ankit thanks for making it clear.
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

and Tomasz Kurpios thanks to you too.
Vaes Bart
Greenhorn

Joined: Oct 28, 2008
Posts: 13

A little addition on this matter: suppose that MyObject() has a private instance Integer object i, like this:

Is it also an additional object for garbage collection in this case ?
Tomasz Kurpios
Greenhorn

Joined: Oct 31, 2008
Posts: 10
I think not, but only because this Integer object will be taken from the cache that Integer class provides for values between -128 to 127. If you initialized it with:


or

then it will be also eligible for GC (assuming you didn't create any other reference to it)
Marcus Moreno
Ranch Hand

Joined: Aug 17, 2008
Posts: 58
Thanks, I really forgot that the old mo object still had a reference to the old m1 Object ("b"). Thanks especially to Ankit for providing this great image, it really made it clear! :-)

P.s.: I just realized I am a "Ranch Hand" myself now! Hooray!
;-)
Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
Hello all

saw this thread only now when i searched for my name...

The theme of this problem are Reference-Islands:

The following program shows what happens. It is a so called reference island. m0, m1 and m2 are bound together by the own member variable mh.
We first have 3 Objects.

And after
m0 = new MyObject(); // "the reason"
we have 4. The former object referenced by m0 is still referenced by m1 and m2.


m0 = m1 = m2; //m0 is re-referenceing Island i.e 3 Objects
here we have 3 referenced objects, onle the ex m0 Object is eligible.

only after nulling m0, the last reference to the island we do not have no referenced objects any more.

I have to null m1 and m2 explicitly as otherwise the Gc does not "see" that these references are null.





In that case the author of this dangerous book was happily right

Docendo discimus (we learn by teaching)
SCJP 5 Zertifizierung, Vorbereitungsbuch; SCJP 6 Zertifizierung, Vorbereitungsbuch

 
 
subject: Mock question from SCJP book