aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC 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 "GC" Watch "GC" New topic
Author

GC

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
The source is http://www.irixtech.com/node/80/quiz/start




My answer was 0 , but the corrct answer was 2.


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

My answer was 0 , but the corrct answer was 2.


Question. At line 9, how do you reach the two objects created at line 4 and line 5. Those two objects are no long reachable, and hence, eligible for GC.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Line 4: Object #1 created, referenced by gc2.
Line 5: Object #2 created, referenced by gc3.
Line 6: Object #2 referenced by gc2.gc.
Line 7: Object #1 referenced by gc3.gc.

At this point:
  • Object #1 has 2 active references pointing to it: gc2 and gc3.gc.
  • Object #2 has 2 active references pointing to it: gc3 and gc2.gc.
  • Line 8: Object #3 created, referenced by gc2.

    At this point, gc2 has been reassigned to point to Object #3. So gc2 no longer references Object #1, and gc2.gc no longer references Object #2. So now:
  • Object #1 has only 1 active reference pointing to it: gc3.gc.
  • Object #2 has only 1 active reference pointing to it: gc3
  • Line 9: Object #3 referenced by gc3.

    At this point, gc3 has been reassigned to point to Object #3. So gc3 no longer references Object #2, and gc3.gc no longer references Object #1. Therefore:
  • Object #1 has 0 active references pointing to it.
  • Object #2 has 0 active references pointing to it.
  • Objects #1 and #2 are eligible for garbage collection.

    EDIT: Changed "references" to "active references." See my follow-up below regarding an "island of isolation."
    [ September 13, 2008: Message edited by: marc weber ]

    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    Pranav Bhatt
    Ranch Hand

    Joined: Mar 20, 2006
    Posts: 284
    Correct answer will be 2 only. My explanation will be as below-:
    GC gc2 = new GC( );
    GC gc3 = new GC( );


    so we will have 2 objects on heap(referenced by variable gc2 and gc3) each having one internal reference variable as gc cause of public GC gc; in code. Lets say these as Obj3 and Obj3.

    Now cause of
    06. gc2.gc = gc3;
    07. gc3.gc = gc2;

    Obj2 reference variable gc will point to Obj3 and gc of Obj3 will point to Obj2.
    Now the code gc2 = new GC( ); will point to some other object say Obj4 and gc3 = gc2; makes gc3 points to Obj4 too. So we are left with gc2 and gc3 pointing to Obj4, and the Obj2,Obj3 as having no external reference shall be garbage collected.

    Hope this may help you as i can't show it with images
    Nabila Mohammad
    Ranch Hand

    Joined: Nov 05, 2007
    Posts: 661
    Originally posted by marc weber:

    At this point, gc2 has been reassigned to point to Object #3. So gc2 no longer references Object #1, and gc2.gc no longer references Object #2. So now:


    Why doese gc2.gc no longer references Object # 2.

    I was on the right track - I just thought gc.2 will still reference
    Object #2 and gc.3 will still refernce Object #1 which will leave no Object for GC.
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18896
        
      40

    Why doese gc2.gc no longer references Object # 2.

    I was on the right track - I just thought gc.2 will still reference
    Object #2 and gc.3 will still refernce Object #1 which will leave no Object for GC.


    gc2.gc still references Object #2. Or more correctly, the object that gc2 used to refer to, dot gc, still references Object #2.

    Keep in mind that the gc2 reference got reassigned, and the gc instance variable was part of the old object that it was assigned to, so that is no longer reachable, via gc2. The new object that gc2 refers to, doesn't have the gc variable set.

    Henry
    Nabila Mohammad
    Ranch Hand

    Joined: Nov 05, 2007
    Posts: 661
    Does that mean gc2.gc and gc3.gc will not be referencing any object?
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18896
        
      40

    Originally posted by Nabila Mohammad:
    Does that mean gc2.gc and gc3.gc will not be referencing any object?


    Well, take a look at the code. gc2 and gc3 has been assign to the new third object. Did the gc instance variable of this third object get set? Or is it the default value of null?

    Henry
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Yes,
  • Object #1 still has a variable 'gc' that references Object #2.
  • Object #2 still has a variable 'gc' that references Object #1.
  • But at the end of this code, Object #1 and Object #2 are no longer reachable through gc2 and gc3, because gc2 and gc3 both reference Object #3.

    Therefore, gc2.gc and gc3.gc both reference the 'gc' in Object #3. Because we have no way to reach Objects #1 and #2, we have no way to reach their instance variables.

    Objects #1 and #2 are an "island of isolation," because they both have references pointing to each other, but neither can be reached by the program code. This makes them eligible for garbage collection.
    Nabila Mohammad
    Ranch Hand

    Joined: Nov 05, 2007
    Posts: 661
    Thanks Alot...


    I NOW got what Islands of Isolation meant........
    Paul Somnath
    Ranch Hand

    Joined: May 19, 2008
    Posts: 177
    Originally posted by marc weber:

    Therefore, gc2.gc and gc3.gc both reference the 'gc' in Object #3.


    What does this mean?
    How can gc2.gc and gc3.gc both reference to 'gc' in Object#3?
    gc2 and gc3 both reference Object#3 after Line 9. gc2.gc still references Object#2 and gc3.gc references Object#1.
    So is the above quoted line correct?


    Preparing for SCJP 6.0
    Nabila Mohammad
    Ranch Hand

    Joined: Nov 05, 2007
    Posts: 661
    Originally posted by Paul Somnath:


    What does this mean?
    How can gc2.gc and gc3.gc both reference to 'gc' in Object#3?
    gc2 and gc3 both reference Object#3 after Line 9. gc2.gc still references Object#2 and gc3.gc references Object#1.
    So is the above quoted line correct?



    gc is a variable associated with Object gc3 and gc2 and they can be accessed via the object they are associated with.
    If both the object are reassigned to a new Object , then the variable assosicated with them will also reference the new Object.

    This is because we acess the variable through the object.
    Each object has its own variable.
    But now when they are reassigned , we cannot access them.
    And they are called islands of Isolation.


    Hope that was clear!
    chander shivdasani
    Ranch Hand

    Joined: Oct 09, 2007
    Posts: 206

    This is an example of "Islands of Isolatio". You can find more details about it in K&B book


    Enjoy, Chander
    SCJP 5, Oracle Certified PL/SQL Developer
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: GC