InterviewSolution
Saved Bookmarks
| 1. |
Why do we call System.gc() in Java |
|
Answer» The garbage collector is run using the System.gc() METHOD in Java. The use of this method is an indicator to the JVM to recycle the unused objects so that the memory can be freed and available for quick reuse as required. The System.gc() method does not return any values. A program that DEMONSTRATES this method is given as follows: public class Demo { public static void main(String[] args) throws InterruptedException { Demo obj = new Demo(); obj = NULL; System.gc(); } @OVERRIDE protected void finalize() throws Throwable { System.out.println("The garbage collector is called..."); System.out.println("The object that is garbage collected is: " + this); } }The output of the above program is as follows: The garbage collector is called... The object that is garbage collected is: Demo@7978f9b4 |
|