This is a question from anilbachi news letter 19. Which statements about garbage collection are true? Select all valid answers.
a) You can directly free the memory allocated by an object. b) You can directly run the garbage collector whenever you want to. c) The garbage collector informs your object when it is about to be garbage collected. d) The garbage collector reclaims an object�s memory as soon as it becomes a candidate for garbage collection. e) The garbage collector runs in low-memory situations.
I would say b & c are correct. But the solution is b, c, e. howz e possible when u cannot guarantee that it runs.
Joined: Oct 10, 2000
Hi Lakshmi, I am quoting a sentence from Khalid Mughals book (A Programmers guide to Java Certification) which says " Garbage collector will certainly go to work if there is a danger of running out of memory." I guess thats why option e is also valid answer. HTH....
Joined: Sep 13, 2000
Hold on, I thought that you can not make the Garbage Collector run. You can "suggest" the JVM to put effort towards object recycle via System.gc() & Runtime.gc(). See RHE, pg. 21. so b is not a correct choice(Let me know if I overlooked something). Even e is not guaranteed. So, I think the only correct choice is c.
Joined: Oct 11, 2000
what i thought by selecting option b is that you can always run (or better call) System.gc(), though it is not for sure that the objects will be garbage collected.
Joined: Oct 31, 2000
I believe 'e' is correct also. The garbage collector may run at low memory state... or may not, if there's nothing to free up. This doesn't make 'e' false.
Joined: Nov 22, 2008
Hi guys, I would say only options c and e are correct. a) false You cannot directly free memory allocated by an object. b) false There is no guarantee that the garbage collector will run when you call the <code>System.gc()</code> or <code>Runtime.gc()</code> method. The documentation for the <code>gc</code> method states:
"Calling this method suggests that the JVM expends effort towards recycling unused objects."
c) true The garbage collector calls <code>finalize</code> on an object. The SDK documentation on finalize states:
"Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup."
d) false There is no guarantee that the garbage collector will reclaim the object memory as soon as it becomes garbage collectable. e) true If you look at the OutOfMemoryError class description in the SDK documentation, it says:
"Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector."
This means that the garbage collector will run in low-memory situations. Cheers, Beno�t [This message has been edited by Beno�t d'Oncieu (edited November 01, 2000).]