Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

GC

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The source is http://www.irixtech.com/node/80/quiz/start




My answer was 0 , but the corrct answer was 2.
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
     
    Ranch Hand
    Posts: 284
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 664
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 23909
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 664
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Does that mean gc2.gc and gc3.gc will not be referencing any object?
     
    Henry Wong
    author
    Posts: 23909
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 664
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Alot...


    I NOW got what Islands of Isolation meant........
     
    Ranch Hand
    Posts: 177
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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?
     
    Nabila Mohammad
    Ranch Hand
    Posts: 664
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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!
     
    Ranch Hand
    Posts: 206
    Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This is an example of "Islands of Isolatio". You can find more details about it in K&B book
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic