Stats

Contributors: 5
Thursday, August 18, 2016
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Manually triggering GC

Download java eBook

Example

You can manually trigger the Garbage Collector by calling

System.gc();

However, Java does not guarantee that the Garbage Collector has run when the call returns. This method simply "suggests" to the JVM (Java Virtual Machine) that you want it to run the garbage collector, but does not force it to do so.

It is generally considered a bad practice to attempt to manually trigger garbage collection. The JVM can be run with the -XX:+DisableExplicitGC option to disable calls to System.gc(). Triggering garbage collection by calling System.gc() can disrupt normal garbage management / object promotion activities of the specific garbage collector implementation in use by the JVM.