Hi, Question Which statements are true ? A. Calling Runtime.gc() will suggest JVM to run garbage collection. B. Calling System.gc() will suggest JVM to run garbage collection. C. You cannot suggest JVM to run garbage collection. D. Only option B is true. If the above kind of question comes then how to answer ? . As per my thinking option D is correct . Usually looking at the question we will think A and B options are true. Calling gc() method on Runtime need object instance as it is not static method like System.gc(). Pls clarify whether we have to conside Runtime.gc() as correct statement or not . Regards Raju
Option A and B are right. U can suggest garbage collection but u can't force it. the methods in A and B do not force garbage collection, they are merely encouraging the JVM to start garbage collection.
Hi Crusty , I think you didn't get my point. I know answer A and B are correct . But my question is how can we sure about Runtime.gc() is correct ?. To me if it is Runtime.getRuntime().gc() then it is correct becuase gc() method in Runtime is not static. So I am just asking in exam if the statements says Runtime.gc() do we need to consider that as Runtime.getRuntime().gc() automatically ?
I think u should read the java API, but to save u time, here is a quote from the documentation
public class Runtime extends Object Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method. An application cannot create its own instance of this class.