What is Garbage Collection?

Garbage collection is the processing of removing the un-used objects (ie., objects that are not reachable) from heap memory. It does automatic memory management in Java.

The Garbage Collector is executed by the JVM when it encounters that the JVM is running with low memory. Garbage collection can be initiated/requested to JVM using the following Java methods.

System.gc();
 [or]
Runtime.getRuntime().gc();

Upon receiving the request, the JVM can trigger the Garbage Collection but there are no guarantee that the Garbage Collector will execute immediately.

Interview Questions: 

Search