• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

GC (Boone's Exam 2)

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a question from Boone's exam 2:
Which statements about Garbage collection are true? Select all valid answers.
a)You can directly free the memory allocated to an object
b) You can directly run the garbage collector whenever you want.
c) The garbage collector informs the object when it is to be garbage collected.
d) The garbage collector reclaims an object memory as soon as it becomes a candidate for garbage collection
e)The garbage collector runs in low memory situations.
Answers are b and c. I do not understand why!
Because RHE states (Page 21):
Even though you are not using an object any longer, you cannot say if it will be collected in 1 milliseconds,100 milliseconds, or it will be collected at all.. There are methods like System.gc() and Runtime.gc() that look as if they "run the garbage collector". Even those cannot be relied upon in general as some other thread might prevent the garbage collection thread from running. In fact the documentation for gc() methods states:
" Calling this method SUGGESTS that the JVM expend effort towards recycling unused objects"
What should the answer be?
Thanks
Ajay
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a)You can directly free the memory allocated to an object
No you cannot. There is no equivalent of a "free()" function in Java   Sure you can set any object reference variable to null. That does not mean that its memory will be instantly reclaimed by the JVM.


b) You can directly run the garbage collector whenever you want.
No you cannot. You can request that it be run but then again your request may be ignored by the JVM.


c) The garbage collector informs the object when it is to be garbage collected.
 Yes. If by "inform" you mean that a particular method is going to be executed on the soon-to-be-garbage -collected Object so that it can do clean up before it is destroyed.


d) The garbage collector reclaims an object memory as soon as it becomes a candidate for garbage collection. 

No way. It may NEVER reclaim the memory of any object wanted dead or alive. (Bon Jovi reference there!)


e)The garbage collector runs in low memory situations. 
... and in High memory situations.. and during New as well as Full moons. This question I wish would disappear from the Java Programmer Certification landscape. Does the word unpredictable mean anything?
 
Ajay Nair
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tony, for the explanations.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic