Win a copy of Svelte and Sapper in Action this week in the JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
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?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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
 
Ranch Hand
Posts: 472
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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 4
  • 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: 472
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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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!
 
Greenhorn
Posts: 8
Hibernate Spring Java
  • 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
 
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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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: 8
Hibernate Spring Java
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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.
 
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Above explanation were nice to understand garbage collection concept. I have changed the condition in the above code ( I mean assigned null to different variables ) and analysed the code as below.
Please let me know if I have got it right?

Also, Garbage Collector is not guaranteed to run always. So, Is there any way to find out how many objects are eligible for garbage collections through code?
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Chivid Ram eligible for collection does not mean that collection will happen, as you stated above one can not be sure that garbage collection happens.
 
Vidya Shivram
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The link was broken.Here is the Image
IMG_20170816_160508.jpg
[Thumbnail for IMG_20170816_160508.jpg]
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic