aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC question" Watch "GC question" New topic
Author

GC question

Shawn Kuenzler
Ranch Hand

Joined: Apr 16, 2006
Posts: 73
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?



SCJP 1.5
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023

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

Joined: Apr 16, 2006
Posts: 73
Yes, Java 1.5. This is straight out of Bert's bible. Page 267.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Shawn Kuenzler
Ranch Hand

Joined: Apr 16, 2006
Posts: 73
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

Joined: Mar 06, 2006
Posts: 54
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

Joined: Mar 19, 2006
Posts: 142
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

Joined: Mar 06, 2006
Posts: 54
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

Joined: Nov 14, 2005
Posts: 324
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)


"Know where to find the solution and how to use it - that's the secret of success."
Ravinder Singh
Ranch Hand

Joined: Mar 06, 2006
Posts: 54
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

Joined: Mar 06, 2006
Posts: 54
sorry about the typo. It's c1 (not c2) and its contained wrapper object.
Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
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


SCJP 5.0<br />-----------<br />"Help Ever && Hurt Never"
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023

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

Joined: Jun 12, 2006
Posts: 66
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

Joined: Jun 12, 2006
Posts: 66
Found this:

http://www.javaranch.com/campfire/StoryPassBy.jsp
warren li
Ranch Hand

Joined: May 23, 2006
Posts: 128
_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 ]

SCBCD 1.3: 94%<br />SCWCD 1.4: 91%<br />SCJP 5: 95%
Anil Yarramsetti
Greenhorn

Joined: Jan 03, 2006
Posts: 1
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

Joined: Jun 15, 2006
Posts: 7
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC question