Java Language Java Memory Management Manually triggering GC

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any Java Language Question?