Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about CH3 Q11: how many objects eligible for garbage collection? (K&B7)

 
Bill barrett
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Chapter 3, Q-11, on P218 I see the following:

When line 16 is reached, how many objects will be eligible for garbage collection?
A. 0
B. 1
C. 2
D. 3
E. 4
F. 5

The answer says B. The book says the following:
"we can now say with stunning clarity and resolve that an object is eligible
for garbage collection when no live thread can access it"

Is this question worded wrong?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,

First of all, a warm welcome to CodeRanch!

I moved your post out of the errata thread, so we can discuss this question more easily. If it turns out to be an errata, I'll post a link to this thread in the errata thread (and update the errata overview accordingly). Hope that's ok with you!

Can you explain a little bit more why you think this question is worded wrong based on the statement (on page 201) "we can now say with stunning clarity and resolve that an object is eligible for garbage collection when no live thread can access it"? Because I don't see what's possibly wrong with this question. The code creates 4 objects and you have to decide how many will be eligible for GC. If no reference variable is referring to an object anymore, no live thread can access it, and this the object is eligible for GC.

Best wishes for 2015!
Kind regards,
Roel
 
Bill barrett
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank Roel and Happy New Year.

My disconnect with the ch3 q11 is that three of the four objects created were set to null. Isn't it the null objects that are eligible for GC?
At line 16, there is still one object that can be reached, therefore it is not eligible for GC.

Thanks
Bill
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.javaranch.com/campfire/StoryPassBy.jsp
maybe this will help
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill barrett wrote:My disconnect with the ch3 q11 is that three of the four objects created were set to null. Isn't it the null objects that are eligible for GC?
At line 16, there is still one object that can be reached, therefore it is not eligible for GC.

First of all, there is no such thing as a null object! A reference variable can be set to null, which means "not refering to any object".

Secondly, as said in my previous post: "If no reference variable is referring to an object anymore, no live thread can access it, and this the object is eligible for GC.". But that doesn't mean you can simply count how many reference variables are assigned to null to get your answer. If it were only that simple! Don't forget an object can be referenced by many reference variables. So if just 1 reference variable is set to null, the object can still be referenced by the other non-null reference variables. So the best way to solve this kind of questions (on the mock and actual exams) is to make a little drawing: reference variables on the left, objects on the right and you draw arrows between reference variables and objects based on the code snippet. If a reference variable is set to null, you delete/remove all outgoing arrows from that reference variable. All objects which don't have an arrow anymore are unreachable and thus eligible for GC.

I made a little drawing myself of this question to illustrate and give you a better idea of this jibber jabber

So have a look at the accompanying drawing. With each arrow or cross I added the according statement from the code snippet. So if you look closely you'll see that just 1 object doesn't have any arrows anymore (the Alpha object which was initially referred by a1) and thus is eligible for GC, all other objects are still reachable through reference variables a2, a2.b2 and Alpha.b1 (therefore not eligible for GC).

Hope it helps!
Kind regards,
Roel
ch3-q11.jpg
[Thumbnail for ch3-q11.jpg]
CH3 Q11 drawing
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs you are great
also paint way creation:
static Alpha.b1 mean reference that from code is Alpha.b1 (reference to static variable)
reference.png
[Thumbnail for reference.png]
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergej Smoljanov wrote:Roel De Nijs you are great

I know

Sergej Smoljanov wrote:also paint way creation:

As I don't have paint on the actual exam, I used blue and green ballpoint pens Although you were a lot slower than me I awarded you a cow for this great post!
 
Wagner Fonseca
Greenhorn
Posts: 7
Hibernate Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am still confused.

And the Beta object referenced in a2.b2?

The reference variable b2 is null, but still reference variable a2.b2 still refer to b2, this means which still the object live on the heap?

I created code:



out print:


thanks
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wagner Fonseca wrote:I am still confused.

I hope this diagram makes it a little clearer.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wagner Fonseca wrote:I am still confused.

And the Beta object referenced in a2.b2?

The reference variable b2 is null, but still reference variable a2.b2 still refer to b2, this means which still the object live on the heap?

Correct!

As mentioned in previous posts (and also visible in the drawings): just 1 object isn't reachable anymore through a reference variable (the Alpha object which was initially referred by a1) and thus is eligible for GC, all other objects are still reachable through reference variables a2, a2.b2 and Alpha.b1 (therefore not eligible for GC).
 
Wagner Fonseca
Greenhorn
Posts: 7
Hibernate Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all,

now no doubts.

Alpha.b1 is instance variable static and keeps the object alive in heap. Here was my doubts.

thanks
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wagner Fonseca wrote:Alpha.b1 is instance variable static

Be careful! There's no such thing as an instance variable static. It's either a class variable (also known as static variable) or an instance variable. Alpha.b1 is clearly a class/static variable.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Bishara wrote:
Wagner Fonseca wrote:I am still confused.

I hope this diagram makes it a little clearer.

Have a cow for that!
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the cow and thanks for broadening my knowledge of garbage collection.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic