Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC question

 
Shawn Kuenzler
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When // doStuff is reached, c1 and it's Short object are eligible for GC. But since c3 has a reference to the same object, shouldn't it still be ineligible for GC?

 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

For SCJP 1.5, "story" in both c1 and c2 reference to the same Short object. So, there is only one object can be GC.
 
Shawn Kuenzler
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Java 1.5. This is straight out of Bert's bible. Page 267.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Sorry about that, but story s/b 200 not 5. it's in the errata posted at the top of the forum, and here:

http://www.coderanch.com/t/253802/java-programmer-SCJP/certification/Updated-Errata

Does the question make sense if Short story = 200; ?

Bert
 
Shawn Kuenzler
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, didn't see the errata post on that earlier.

I'm not seeing how 200 makes it any different. Is c3 referencing the same object that c1 was? Or is c3 referencing a new object that the go() method returned?

If it's the former, I would think zero items are eligible for GC. If it's the latter, it might actually make sense to me!

Thanks,
Shawn
 
Ravinder Singh
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shawn,

I'm not seeing how 200 makes it any different. Is c3 referencing the same object that c1 was? Or is c3 referencing a new object that the go() method returned?


c3 refers to the reference variable c2 which has been assigned a null value in a call to the go() method.

I think there are two main objects (technically 4 including each object's own copy of their contained wrapper objects) that are eligible for garbage collection namely, objects refered by c1 and c2.

Why c1:
Because a CardBoard object was created on the heap (along with the contained wrapper object story) as a result of the call to new CardBoard() and its address was assigned to the reference variable c1. Later in the code, c1 was assigned a null value meaning it no longer holds reference to the CardBoard object (and the associated contained wrapper object), therefore making it eligible for garbage collection.

Why c2:
Because a copy of the c2 object's address was passed as an argument to the go() method invoked on c1 object. Inside the method, reference variable cb which holds the address of c2 object was also assigned a null value, thereby making the object commonly referenced by c2 and cb eligible for garbage collection.

I hope this clarifies....
Guys..pls. correct me if i'm wrong anywhere because I believe the answer to this question i.e. Q2. SCJP 5 by K&B Page 266 is not correct.

------------------
Ravinder
 
S Thiyanesh
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When Short story = 5, you end up with all the objects referencing the
pooled instance of the Short Object.
But when its made as Short story = 200, there will be no pooled instance
created, as the value is out of range(-128 to 127).
So new Short objects are created for each instance of the class.
Hope this is clear.
 
Ravinder Singh
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that's right.

Sorry about that, but story s/b 200 not 5. it's in the errata posted at the top of the forum, and here:

http://www.coderanch.com/t/253802/java-programmer-SCJP/certification/Updated-Errata

So with the new value for variable story as per the errata above, total 4 objects becomes eligible for garbage collection....the option not mentioned in the question.

----------
Ravinder
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why c2:
Because a copy of the c2 object's address was passed as an argument to the go() method invoked on c1 object. Inside the method, reference variable cb which holds the address of c2 object was also assigned a null value, thereby making the object commonly referenced by c2 and cb eligible for garbage collection.


When go() method is called, both c2 and cd refer to same object. So when we set cb to null it will not have any affect on c2 which still refer to same object. Thus this object will not be elligible for GC( as c2 refer it)
 
Ravinder Singh
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Amir, you're right. I overlooked that part.

So..finally there are only two objects that will be eligible for garbage collection - ie. object referenced by c2 and its contained wrapper object called story.

Thanks guys for all your responses.

--------
Ravinder
 
Ravinder Singh
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry about the typo. It's c1 (not c2) and its contained wrapper object.
 
Swapnil Trivedi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amir Explained: "When go() method is called, both c2 and cd refer to same object. So when we set cb to null it will not have any affect on c2 which still refer to same object. Thus this object will not be elligible for GC( as c2 refer it)"

Amir, may be you are right but as far as I know any changes made to the object references passed as an argument reflects outside. So if we make cb=null then it will automatically make C2 to be null.
And also u are saying in your explaination itself that cb & C2 refers to the same object hence proved.

So if C1 is eligible for GC then why not C2???

You can also check K&B book page no 204 for more explaination...
I am really getting confused by this problem

Is anybody theer who can solve this puzzle and explain it clearly
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Amir, may be you are right but as far as I know any changes made to the object references passed as an argument reflects outside. So if we make cb=null then it will automatically make C2 to be null.
And also u are saying in your explaination itself that cb & C2 refers to the same object hence proved.
So if C1 is eligible for GC then why not C2???

Always Passed By Value in Java's Method Invocation. "cb = null" does not affect variable c2.
 
Anupama Ponnapalli
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Swapnil Trivedi:
Amir Explained: "When go() method is called, both c2 and cd refer to same object. So when we set cb to null it will not have any affect on c2 which still refer to same object. Thus this object will not be elligible for GC( as c2 refer it)"

Amir, may be you are right but as far as I know any changes made to the object references passed as an argument reflects outside. So if we make cb=null then it will automatically make C2 to be null.
And also u are saying in your explaination itself that cb & C2 refers to the same object hence proved.



Taking Owen's comment further, its always pass by value in Java. The go method gets a copy of c2 => cb. Since c2 itself is a reference to a cardboard object on the heap, its value is the address of the object on the heap. Thus, while we are in the method go, there are two references to the same object => cb, c2.

Now, if any attribute/instance variable of the cardboard object is modified using cb, the changes will reflect outside the method, since its a valid reference.

But, if we set the reference cb to null, we are repointing the reference to "nothing". the object itself is unaffected. So is c2. Now, there is only one reference to the cardboard object => c2.

Makes sense?
 
Anupama Ponnapalli
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found this:

http://www.javaranch.com/campfire/StoryPassBy.jsp
 
warren li
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
_ALL_ java variables are passed/copied by its own value.
This value points to the object on the heap - if it is a reference variable - but they are not the object itself.
Probably if people have some C pointer background, it will help a lot in understanding what "value" is exactly passed/copied.
[ June 15, 2006: Message edited by: warren li ]
 
Anil Yarramsetti
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In between, does c3 is eligible for GC or not?
I undersrood C1 is eligible and C2 not.
I think C3 is eligible since it was assigned the value returned by the go() method, which is a null (cb=null . Am I correct or missing something?
 
murali palaniappan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only C1 is garbage collected.

c3 is having only null
it never pointed to valid live object.so it is never GC.

Cb is also no,bcoz it never pointed to valid live object.

ok bye cheers
Muralee
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic