• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

doubt:garbage collection eligibility

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

This question is from enthuware series.

After what line the MyClass object created at line 1 will be eligible for GC?





Select 1 correct option.
a 2


b 5


c 6
At line 6, x starts pointing to a new MyClassObject and there remains no reference to the original MyClass object.

d 7


e Never till the program ends.

How come the answer is c? can someone explain please?

thanks,
Piya
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets see what the memory representation would be for this program

when line 1 is executed, this will be the memory representation



now this same object is returned to main method. Since mc was local to getMyClassObject method, so it would no longer exist after the method body completes. This object will be caught by reference variable x so the memory representation would be



After this at line 6, x will start pointing to a new instance of MyClass and thus this object will have no reference to it

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before line 6, x variable has the reference of MyClass instance

see line 4:



But in line 6, a new variable has been assigned to it.



So, only one object is elligible for garbage collection.
 
Good night. Drive safely. Here's a tiny ad for the road:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic