How to explicitly invoke Garbage Collection in Java



We know that Java runtime system performs the garbage collection asynchronously depending on the available resources.

When there are no more references to an object, the object is finalized.
When the Garbage Collections starts these finalized objects gets collected.


How to call Garbage Collector explicitly?

What if we want to run the garbage collector explicitly just before some memory intensive task.
We just need to call the garbage collector by this statement:
System.gc();

Finalization process can also be invoked explicitly using the following statement:
System.runFinalization();

After the execution of this statement all the objects those are without reference, are finalized.



Previous Post Next Post