Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, here is another question whose answer I don't understand, from
Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 11419-11425). Enthuware. Kindle Edition.

Consider the following code:

After what line the MyClass object created at line 1 will be eligible for garbage collection? Select 1 option
A. 2
B. 5
C. 6
D. 7
E. Never till the program ends.


My answer was E but it's apparently C. I answered E because even when x is made to point to a different object, a new one that it, mc still points at the object created at line 1. I thought that by doing and returning its reference to x we effectively ended up with two references to the same object and removing one would not remove the other one, hope it makes sense.
 
Henry Wong
author
Marshal
Pie
Posts: 22110
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote: ... and returning its reference to x we effectively ended up with two references to the same object and removing one would not remove the other one, hope it makes sense.


Hint: What happens to the local variables of a method when the method completes / returns?

Henry
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They cease to exist, but here we're returning that reference and save it onto a variable
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's try this with hands since there are only two references. In getMyClassObject(), i give you a ball and tell you to hold it on your right hand. So one reference. When the method ends, you pass the ball to your left hand. Your right hand (also known as variable mc) is free because the method is over. But the ball is now in your left (also known as variable x) so no worries.

Now on line 6, I give you a different ball to hold in your left hand. The original ball is now on the floor and rolling away for the garbage collector to take care of.
 
Henry Wong
author
Marshal
Pie
Posts: 22110
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Just to finish my train of discussion (even though I really like Jeanne's analogy ) ...

Jason Attin wrote:They cease to exist, but here we're returning that reference and save it onto a variable


So, if the local reference variable "cease to exist", meaning that it goes out of scope, then...

Jason Attin wrote: and returning its reference to x we effectively ended up with two references to the same object and removing one would not remove the other one, hope it makes sense.


can you tell us what are the two references being referred to here? The x variable is obviously one of the references, but what is the second one that you are mentioning?

Henry
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I think I got it, like the ball analogy actually :-).
can you tell us what are the two references being referred to here?

Before reading all the comments I would have said x and mc. Now I think I got it, mc ceased to exist when the method exited so we only have x.
 
Leoncio Thiago
Greenhorn
Posts: 11
Eclipse IDE Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:OK I think I got it, like the ball analogy actually :-).
can you tell us what are the two references being referred to here?

Before reading all the comments I would have said x and mc. Now I think I got it, mc ceased to exist when the method exited so we only have x.



Great explanation, thanks for sharing the understanding.

--Thiago
 
Henry Wong
author
Marshal
Pie
Posts: 22110
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leoncio Thiago wrote:
Great explanation, thanks for sharing the understanding.


Agreed. Considering the number of topics on the ranch, where the OP never posts back after the initial post, it is nice to have a follow up with full explanations. Have a cow !!

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic