• 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
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Question from mock test

 
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i have a question :-


now the question is :- How many objects are eligible for Garbage Collection after line //do stuff ?
According to me all the c1,c2 and c3 are eligible for garbage collection . However the answer says only two objects are eligible for garbage collection ie c1 and associated Short object .
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
only 2 objects are eligible. Other objects will be still live they will be eligible after the execution exit the Main method (closing braces of Main).
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is discussed many times, just give a try to search first then let us know if you still have confusion.


Regard

Minhaj
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ragi singh wrote:
CardBoard go(CardBoard cb){
cb=null;
return cb;
}


you might think that, here you are assigning a null value to cb, so c2 will also eligible to GC. but Java passes the copy of the reference value . so cb and c2 are two different references
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the reference variable c3 doesn't refer any object since the new key word is not used to instanciate the object.
c2 object is not eligible the GC because the cb object can't make it refer to a different object or null
c1 object is changed to null, and since it holds a short object, it'll too set up to null and these are the objects eligible the GC
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lets understand above code by diagram...


So the object are eligible point by c1 are eligible for garbage collection..

And also remember only objects are eligible for Garbage collection not the refernce variable
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic