Pl. suggest . . . The foll Q. is from Boon's Mock Exam. Question 65: 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. Ans given : b, c, e. But in my opinion only e is the ans.Can anybody there who can put light on the Above question ? Thanx, Vikas
This is only a partial answer to your question. If anyone else has an answer it would be helpful. The garbage collector for an object can be requested to be run by the programmer, but we do not know for a fact when it will run. The way to run it is with the gc method of the system class or runtime class. (System.gc() Runtime.getRuntime().gc(). As to the object being informed of garbage collection before being collected, I was not aware of that. The only thing I know of is the finalize method. Which is invoked to clean up any operating system resources that you want before the objects are garbage collected. The finalize method is invoked after an object has no references and before actually reclaiming the memory.
hi I think Barry Boone is making a mistake.In his book pg 116 he says that by the time runtime's gc() returns your object will be Garbage collected. I'll trust Bruce Eckel for that !You have no control over Garbage Collection. So answer b is surely wrong. The Garbage Collector is guarenteed to call finalize on every object prior to actually GCing them. Also the GC runs on low memory situations only.Which is why it is said that you can hardly influence or predict when anything will be Collected. So the other two options are correct.
C is definatley correct. How else would your object know to run it's finalyze method if it wasn't informed it was about to be garbage collected. I do agree that B is incorrect, should state you can directly ask the GC to run, but not you can directly run it. Bill