• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Bear Bibeault
  • paul wheaton
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Mikalai Zaikin
  • Piet Souris
Bartenders:

scjp GC

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi The following code found in http://jonathangiles.net for scjp preparation,


the comment has given like, until reach line 3, the objects are not eligible for GC.

My question is when we reach line 2 (e3 is already null and it did not refer to any objects) and when reach line 1, e2 did not refer e1, that means e2 is isolated objects. why e2 is not eligible for GC?,
 
Saloon Keeper
Posts: 15136
345
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because e1 refers to an object which still has a reference to the object formerly referenced by e3, which has a reference to the object formerly referenced by e2.

You say the object formerly referred to by e2 is isolated after line 2. This isn't true. I can still reach it using the following line:
After line 3, all thee objects become eligible at the same time.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope a couple of things are true:

1 - I hope that jonathan said in his mock exams that he copied this question directly from the K&B book, making only meaningless and minor changes.
2 - I hope you didn't pay jonathan anything because he's using copyrighted material that's not his.
 
Greenhorn
Posts: 22
Android Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephan,
I originally thought that your assertion of Lost lost = e1.e.e would work was wrong, but I've coded it and it works. I'm confused now.

If e1.e = e3 and e3 is later set to null, this means that e1.e = null right? Then how can there be a e1.e.e if e1.e = null?

I'd appreciate clarification because I remember not understanding this problem in the K&B text.

Thanks.
 
author
Posts: 23939
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christopher Collier wrote:
If e1.e = e3 and e3 is later set to null, this means that e1.e = null right?



No. Of course not. When you set the e1.e reference to point to the same object as the e3 reference, they are two different references that point to the same object. It doesn't tie the two references together, to be always pointing to the same thing.

Henry
 
Christopher Collier
Greenhorn
Posts: 22
Android Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Christopher Collier wrote:
If e1.e = e3 and e3 is later set to null, this means that e1.e = null right?



No. Of course not. When you set the e1.e reference to point to the same object as the e3 reference, they are two different references that point to the same object. It doesn't tie the two references together, to be always pointing to the same thing.

Henry



Thanks Henry.
 
Shankar sanjay
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi All,
my question is very simple,


when we say e3 is null that means e3 object does not refer to any objects (here e3 does not refer to e2). But e1 still refers e3.
again line#2, e2 is null (e2=null) that means e2 doest not refer to e1, please tell now, whether e2 is isolated or not? please correct me if i am wrong.

this question to Stephan : please tell me when e2=null, what is the value for e2.e??, do you think e2.e=e1 when e2 is null?
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now, each of the 'Lost' objects created have 2 valid references pointing to it.
object1 is being referenced by e1 and e2.e
object2 is being referenced by e2 and e3.e
object3 is being referenced by e3 and e1.e


Here, when you set e3=null, object1 loses one of its references. Since e3 is set to null, e3.e is not valid anymore. This makes object2 lose one of its references.
Now it becomes,
object1 is being referenced by e1 and e2.e
object2 is being referenced by e2
object3 is being referenced by e1.e


After this object2 loses its references completely and is eligible for GC. Also, e2.e is not valid anymore and so, one of object1's references is being stripped off.
object1 is being referenced by e1
object2 is not referenced at all
object3 is being referenced by e1.e


Finally, after this part, the remaining objects(object1 and object3) are also being unreferenced completely and are eligible for GC.
object1 is not referenced at all
object2 is not referenced at all
object3 is not referenced at all


Hope this makes you clear.
Things like this gets easier when you sit down with a pencil and paper, and draw out.
 
Stephan van Hulst
Saloon Keeper
Posts: 15136
345
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appreciate the illustration Vinoth, but sadly you are incorrect. I will copy your post and make the necessary changes:


object1 is being referenced by e1 and object2.e
object2 is being referenced by e2 and object3.e
object3 is being referenced by e3 and object1.e



object1 is being referenced by e1 and object2.e
object2 is being referenced by e2 and object3.e
object3 is being referenced by object1.e



object1 is being referenced by e1 and object2.e
object2 is being referenced by object3.e
object3 is being referenced by object1.e


Note that at this point, each of the objects still refer to one of the other objects, and all three objects are accessible through e1.
As you can see, e1 refers to object1, object1.e refers to object3, object3.e refers to object2, and object2.e refers back to object1 again. All objects are reachable, so none are eligible for garbage collection.


Now, even though all three objects still refer to one another, none of them is reachable through code anymore. So now, all three objects become eligible all at once.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<Off topic>

Bert Bates wrote:I hope a couple of things are true:

1 - I hope that jonathan said in his mock exams that he copied this question directly from the K&B book, making only meaningless and minor changes.
2 - I hope you didn't pay jonathan anything because he's using copyrighted material that's not his.



The said question came from notes on jonathan's site

http://jonathangiles.net/blog/wp-content/uploads/2009/06/scjp-6-notes-jonathangiles.pdf

I am not sure which question in K and B was copied. I tried searching for the same statements in the book. It appears the black belt factory also has versions of the same question.

http://jira.javablackbelt.com/QuestionView.wwa?questionId=13934059
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah..yeah..got it! I made a blunder.
Thanks Stephan
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mini sanjay wrote:hi The following code found in http://jonathangiles.net for scjp preparation,


the comment has given like, until reach line 3, the objects are not eligible for GC.

My question is when we reach line 2 (e3 is already null and it did not refer to any objects) and when reach line 1, e2 did not refer e1, that means e2 is isolated objects. why e2 is not eligible for GC?,


After All Said And Done, I Have Been Studying G.C. For The Past Two Days, I Came To Discover That Simply Getting A Pencil And Paper And Practice Drawing A Sketch Of The Heap And References That 'REFER' (I Know C++ Fans Will Prefer 'POINT') To Objects In The Heap 'From-The-Outside-World', Made It Very Easy And So Much Fun!! In The Real Exams Thinking About 'Jackzia-Refers-To-Bert-Refers-To-Katty' Might Burn Too Much Time And Energy Needed To Resolve Other Problems.

 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Deepak!

For everyone reading this thread, let me review one of JavaRanch's policies...

We encourage ranchers to copy and discuss small bits of copyrighted and published content like mock exams. BUT! It's very important to cite the source of the material when you present it. In other words, if you got a question from K&B, just take a moment to mention that the question came from K&B.

In this case the the source of the question was cited (thanks to mini sanjay!), but the folks who have posted this question, Jonathan and JavaBlackBelt did NOT cite their source.

I have sent emails to both of them asking them to cite their sources in the future.

hth,

Bert
 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic