Ans
One of the best features of java is that performs automatic garbage collection or memory management, generally jvm performs garbage collection when it needs more memory to continue execution.
Garbage collection in java cannot be enforced, but still sometimes we call System.gc () method explicitly, system.gc () method just “hint” to the jvm that garbage collection should be run. It is not garneted.
Example:
public class GCTest {
public static void main(String[] args) throws InterruptedException {
A a = new A("white");
a = null;
Runtime.getRuntime().gc();
}
}
class A {
private String color;
public A(String color) {
// TODO Auto-generated constructor stub
this.color = color;
}
@Override
public void finalize() {
System.out.println(this.color + " cleaned");
}
}
------------------------------------------------------------------------------
So the program may or may not output the following:
Output:
white cleaned