GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Question : I think the answer provided is wrong... 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 "Garbage Collection Question : I think the answer provided is wrong..." Watch "Garbage Collection Question : I think the answer provided is wrong..." New topic
Author

Garbage Collection Question : I think the answer provided is wrong...

kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
Hi here is the question :


After line 6 (x2=x4) runs, how many objects are eligible for garbage collection?
The answer is 1.
However I think it should be 0 because after the function call m1(X mx),
mx is assigned to x2, which is later assigned to x4, however, mx still points to the object, so shouldn't it be 1 instead of 0?

please let me know what you all thinks..

Thanks...
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Note that mx is a local variable and therefore destroyed after the method finishes.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9398
    
    2

Hi above,

If mx is a local variable, and if that is destroyed then what will be the value of X x2 = m1(x);? Could you please tell me.

Regards,
Jothi Shankar Kumar. S


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
mx is just a reference. A reference to the actual object is returned, but the local variable, mx, is destroyed.
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
Hi,

I read somewhere that for Garbage collection questions its always useful if you make diagram. This will help you visualize object memory model.

So here i will try to draw object memory model for each crucial step in program.
Rectangle box shows object in memory and inside box unique number represent id of object.




Now after step-6 you can see how many objects are eligible for Garbage collection. i think step-3 and 4 require some explanation, in step-3 we create new object and reference variable points to that object and in step-4 once static method returns refernce variable mx goes out of scope, therefore it is no longer valid variable.

I hope it helps !!


If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Keith Lynn:
mx is just a reference. A reference to the actual object is returned, but the local variable, mx, is destroyed.


Objects are the one that are destroyed, not the variables.
kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
To yogesh sood:
I think there is something wrong with your diagram in the middle there...

after the static method executed..
both mx and x2 points to the newly created X object, notice mx is just a copy of reference x, but after the static method is finished, mx-the reference variable no longer exits.......

As the others pointed out already.....I just want to drive the point home for others as well as myself : garbage collection is for OBJECTS only, not reference variables........I think reference variables get pushed off the stack..that is how they get destroyed....but i could be wrong, do feel free to correct me there..

again, thanks...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

To yogesh sood:
I think there is something wrong with your diagram in the middle there...


Really? Where? It looks fine to me.

As the others pointed out already.....I just want to drive the point home for others as well as myself : garbage collection is for OBJECTS only, not reference variables........I think reference variables get pushed off the stack..that is how they get destroyed....but i could be wrong, do feel free to correct me there..


Great summary. Objects are GC'ed when they are no longer reachable. And the way to reach it is to use a reference that is still in scope. Local variables (which a parameter is) are no longer in scope when the method returns.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Alton Hernandez:


Objects are the one that are destroyed, not the variables.


I'm talking about the variables that are on the stack.
kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
Originally posted by Henry Wong:


Great summary. Objects are GC'ed when they are no longer reachable. And the way to reach it is to use a reference that is still in scope. Local variables (which a parameter is) are no longer in scope when the method returns.

Henry


Notice yogesh posted this :

Step-2: In static method before execution of first line in that method :
_______
x--->|new 1|
mx-->|_______|

well, I believe mx should not be pointing to the same object as x,
it should points to another object that is newly created in the static method..

after the static method returns, mx is destroyed, it is no longer in scope, however, the object it was pointing too is still there, and no reference points to that object after line 6 is executed, hence, it now becomes eligible for garbage collection, agree??

please let me know.

many thanks...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Originally posted by kay lin:

Notice yogesh posted this :

Step-2: In static method before execution of first line in that method :
_______
x--->|new 1|
mx-->|_______|

well, I believe mx should not be pointing to the same object as x,
it should points to another object that is newly created in the static method..


No. Java uses pass by copy of the reference for parameters. This means that you can change the reference in the method to a different object, and it will not affect the caller. However, the object that the copied reference is pointing to, is the same object. This means that if the object is mutable, it can be changed by the method.

In other words... Yogesh's diagram is correct.

Henry
kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
but the line mx = new X();
makes mx points to a newly created object X, does it not?
at least that is what i though..
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Originally posted by kay lin:
but the line mx = new X();
makes mx points to a newly created object X, does it not?
at least that is what i though..


Yes, it does. Which is what is correctly shown in Step 3 of the diagram. Step 2, as shown, is correct.

Henry
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
Hi Kay Lin,

Actually i have provided step-2 which is showing state just after call to static method and before execution of line 1 in static method.

The state after execution of line 1 in static method is shown in step-3, which is in sync what you have mentioned that

but the line mx = new X();
makes mx points to a newly created object X, does it not?
at least that is what i though..
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9398
    
    2

Hi Yogesh,

That was really a very good way to explain GC related questions. I will follow this. Thanks man. I really understood it a lot. If my understanding is correct, the return mx from the static method returns the bits representing how to reach the object new 2( as given from your explanation). and this copy is given to x2 in the line X x2 = m1(x);. Please correct me if I'm wrong.

Regards,
Jothi Shankar Kumar. S
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
Hi Jothi,

Yes Jothi, you got it correct coneptually.

Now physically or under the hood how it happens i might not be right person to comment i understand this way that this object will be created on Heap, and reference to that object is return back to caller. But i have never tried to generate byte code for the same and verify

Good luck.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9398
    
    2

Hi Yogesh,

Thanks for the feedback. You have a good signature..."If it's green, it's biology..." add this as well...If it's garbage collection, it's Java. Was just joking. Anyways thatks for the diagram and I'm confident with questions related to GC.

Regards,
Jothi Shankar Kumar. S
[ October 15, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection Question : I think the answer provided is wrong...