Low Orbit Flux Logo 2 D

Java How To Request / Force Garbage Collection

Java - How to Request Garbage Collection in Code

System.gc();                  // request GC
Runtime.getRuntime().gc();    // request GC, equivalent to System.gc()
Runtime.gc();                 // also requests GC

Run a Java GC from the Command Line

It is possible to trigger a garbage collection from the command line using the jcmd command. We’re going to assume that you have your path setup correctly and that jcmd is on the path. If not, you can either set up your path or run this directly from where you have the JDK installed ( see the section below on Jconsole.

You can list all local Java procs and their pids with this command:


You can request a GC on a specific Java PID like this ( assuming the pid is 2106136 ).

jcmd 2106136 GC.run

Run a Java GC from the Jconsole GUI

On windows, your path might look like this ( probably with a newer JDK version ):

C:\Program Files\Java\jdk1.6.0_31\bin

Just navigate there and launch jconsole.exe.

On Linux, the path might be a bit different. For example, I have the JDK installed here ( your will probably be different ):


Just cd to the bin directory and launch jconsole.

cd /home/user1/jdk-15.0.2/bin

You can also launch it directly if you path is setup correctly.


Connect to the process that you want to work with ( remote or local ).

Java How To Request / Force Garbage Collection - jconsole - select process

Go to the memory tab and click on the button labeled “Perform GC”.

Java How To Request / Force Garbage Collection - jconsole - Perform GC

You should see the line on the graph drop almost straight down.