9. True of False You can invoke the garbage collector whenever you want a] True b] False The answer given is b false, but i think is a true, because you can atleast invoke the garbage collected whenever you want but doesn't mean it will run. Please clarify. As i am getting confused, on similar questions. Regards Sharmila
Hi Sharmila, You cannot force the JVM to garbage collect an object by invoking a call to <code> System.gc() </code> or a call to <code> Runtime.gc() </code>. To quote the JAVA API clearly states the fol:
gc <code> public static void gc()</code> Runs the garbage collector. Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects. The call <code> System.gc() </code> is effectively equivalent to the call: <code> Runtime.getRuntime().gc() </code>
The best you can do is to speed-up the gc is by setting the object references to <code> null </code> when you no longer require them. Doing so will ensure that memory leaks dont exists and suggest the JVM's garbage collector to take steps to speed-up the garbage-collection process. Whe the garbage collector finds that memory that is no longer accessible from any live thread, it takes steps to release the memory back into the heap for re-use. Hope this handles your querry. Please revert in case of any further clarification on this. Ravindra Mohan
[This message has been edited by Ravindra Mohan (edited May 02, 2001).]
Ravindra, but isn't System.gc called as invoking the constructor. So shouldn't the answer to that question be true. I understand that this will not necessary mean that gc takes place. But atleast we can invoke the constructor.
but isn't System.gc called as invoking the constructor. So shouldn't the answer to that question be true. I understand that this will not necessary mean that gc takes place. But atleast we can invoke the constructor.
Let me clarify here. <code> gc</code> is not a constructor, rather is a <code> static method of "java.lang.System" class </code>. Thats the reason you dont need to create an instance of <code> System class </code> but can call the method directly by giving its "classname".method. I hope this clarifies the confusion in your mind. Rgds, Ravindra Mohan
[This message has been edited by Ravindra Mohan (edited May 03, 2001).]
Hi Sharmila, Ravindra You're right; the word 'invoke' normally means 'call' ie you can invoke the System.gc() method or you can call the System.gc() method. I would also have responded 'true' to this question, based on the wording. If it said 'you can run the garbage collector whenever you want' then the answer would be false; since, as Ravindra pointed out, calling the method simply sends a request to the gc, asking it to run...it doesn't force the gc to run. The wording on the real exam is very clear and straight forward so you shouldn't run into this type of problem. If you see the word 'invoke' it invariably means 'call'. Hope that helps.
------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform