• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Garbage collection

 
Ranch Hand
Posts: 234
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They cease to exist, but here we're returning that reference and save it onto a variable
 
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 234
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
Eclipse IDE Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic