• 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

Mock question from SCJP book

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 13
Netbeans IDE Opera Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At first glance, I agree with Rekha - only 1 object eligible for GC after line 1.
 
Marcus Moreno
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But m2 still points to the object that formally was referenced by mo:
m2.mh = m0;
 
Ravikanth kolli
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()"...
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Tomasz Kurpios
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My previous post refers to Marcus Moreno not Ankit Garg (who I totally agree with).

Pozdrowienia //Regards
 
Ravikanth kolli
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow!! i got it now. I actually forgot about the referencing with "mh".

Ankit thanks for making it clear.
 
Ravikanth kolli
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and Tomasz Kurpios thanks to you too.
 
Vaes Bart
Greenhorn
Posts: 13
Netbeans IDE Opera Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
;-)
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Acetylsalicylic acid is aspirin. This could be handy too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic