How do I run garbage collector from shell?

If you need to force the execution of Garbage Collection of a Java process from the command line the best option is to use the jcmd utility.

The Java diagnostic command (JCMD) is another JDK tool (available in the bin folder of the JDK) that will trigger a garbage collection routine if the JVM can safely schedule a stop-the-world pause.

Just retrieve the PID of the Java process and execute:

jcmd <pid> GC.run

Other than that, you run the jmap -histo:live <pid> command that will force a full GC on the heap before it prints anything out.

Found the article helpful? if so please follow us on Socials