aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC Question from Marcus Green 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 from Marcus Green" Watch "GC Question from Marcus Green" New topic
Author

GC Question from Marcus Green

Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200


Given the following code, how many objects will be eligible for garbage collection on the line with the comment //here

0
1
2
3
4

I answered 2, but the answer given was 0. Why is this so?


Be Afraid...Be very Afraid...
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919



Zero is correct. Line 1 creates a new integer object. In line a copy of refernce x is passed to method. In the method findOut, the copy is reassigned , not the original object. The original object pointed by x stil exists. Line 4 causes one more reference to point to object created at line 3. z =null makes only y point to the object. Apply similar logic as applied previously for findout(y). When the commented line is reached all objects are refernced and are not eligible for GC. Hope this helps


Groovy
Samir Arora
Greenhorn

Joined: Apr 21, 2005
Posts: 1
Hi,
According to me also it should be 2 only. Lets see if any one can explain !!
Manish Nijhawan
Greenhorn

Joined: Apr 04, 2005
Posts: 10
Hi Pardeep,
As you have explained that at the commented line since all the objects have the references so they are not eligible for the GC. But since that line is the last line of the method , so after that line all the references to all the objects will vanish , so all the Live objects should be eligible for GC.

Please explain me this as i am very much confused about GC.


Manish Nijhawan<br />B-Tech
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
I agree none are eligible for GC.

But if the findOut() is modified like the one below then 2 objects are eligible for garbage collection , am i right ?


[ April 21, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Originally posted by Srinivasa Raghavan:
I agree none are eligible for GC.

But if the findOut() is modified like the one below then 2 objects are eligible for garbage collection , am i right ?



yes
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Originally posted by Manish Nijhawan:
Hi Pardeep,
As you have explained that at the commented line since all the objects have the references so they are not eligible for the GC. But since that line is the last line of the method , so after that line all the references to all the objects will vanish , so all the Live objects should be eligible for GC.

Please explain me this as i am very much confused about GC.


The objects will be eligible for GC after the method completes.
soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300
Hi,

I understand that the object holding 111 in findOut() becomes eligible for GC, but which is the other object that's eligible after the modified findOut() ?

Thanks,
Soumya
[ April 21, 2005: Message edited by: soumya ravindranath ]
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
findOut() is called twice check the code

soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300
Oooops! thanks
nishant vats
Greenhorn

Joined: Feb 11, 2005
Posts: 23
According to me only one object will be elgible for garbage collection..i.e
new Integer(10);
this object has refernce count =0;as u can check .Intially this object was refrenced by x but when x=y is executed .This object is no longer refrenced.
new Integer(99) is being still refernced by y.so only one object is elgible for garbage collection
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Originally posted by nish vats:
According to me only one object will be elgible for garbage collection..i.e
new Integer(10);
this object has refernce count =0;as u can check .Intially this object was refrenced by x but when x=y is executed .This object is no longer refrenced.
new Integer(99) is being still refernced by y.so only one object is elgible for garbage collection


Where is x=y I dont see it. What about others?
soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300
As I see it, there is no x =y in the code given.

When z = y and then z = null, as Pradeep said, it means nothing to z or y. x continues to point to 10.
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
Now I understand why "0" was the answer. The copy of the reference object was reassigned in findOut method and not the original reference.
Sunyaluk Boonmas
Greenhorn

Joined: Apr 18, 2005
Posts: 24
The answer is so clear.

This is the sample of pass by reference problem.

This problem look like this

http://www.coderanch.com/t/248832/java-programmer-SCJP/certification/Dan-exam

This explanation very clear, I think.

http://www.javaranch.com/campfire/StoryPassBy.jsp

And this flash playing is interesting.I think it is very good to show how it work.


http://www.geocities.com/mcglonec1978/javacert/paramPassing_highres.html

This it main url.
http://www.geocities.com/mcglonec1978/javacert/javacert.html#param_passing

Hope it useful for you.


SCJP Passed(93%)<br />SCWCD Preparing<br />SCEA future.<br /> <br />Here is the great community!!
 
jQuery in Action, 2nd edition
 
subject: GC Question from Marcus Green