How do you Force Garbage Collection from the Shell?

eyberg picture eyberg · Aug 19, 2010 · Viewed 96.2k times · Source

So I am looking at a heap with jmap on a remote box and I want to force garbage collection on it. How do you do this without popping into jvisualvm or jconsole and friends?

I know you shouldn't be in the practice of forcing garbage collection -- you should just figure out why the heap is big/growing.

I also realize the System.GC() doesn't actually force garbage collection -- it just tells the GC that you'd like it to occur.

Having said that is there a way to do this easily? Some command line app I'm missing?

Answer

user3198490 picture user3198490 · Jan 15, 2014

Since JDK 7 you can use the JDK command tool 'jcmd' such as:

jcmd <pid> GC.run