Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you Force Garbage Collection from the Shell?

People also ask

Is it possible to force garbage collection in?

You really can't force Java GC. The Java garbage collection algos are non-deterministic, and while all of these methods can motivate the JVM to do GC, you can't actually force it.

Is it possible to force garbage collection in Java?

It is not guaranteed! The API documentation for System. gc( ) states that "When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects." The responsibility for a programmer is to make sure no references remain for objects.

Can you manually call the garbage collector?

You can call Garbage Collector explicitly, but JVM decides whether to process the call or not. Ideally, you should never write code dependent on call to garbage collector.

How do you manually collect garbage?

Select Monitoring > Performance. Click Garbage Collect. Garbage Collect calls the JVM's System. gc() method to perform garbage collection.


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

jcmd <pid> GC.run


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


You can do this via the free jmxterm program.

Fire it up like so:

java -jar jmxterm-1.0-alpha-4-uber.jar

From there, you can connect to a host and trigger GC:

$>open host:jmxport
#Connection to host:jmxport is opened
$>bean java.lang:type=Memory
#bean is set to java.lang:type=Memory
$>run gc
#calling operation gc of mbean java.lang:type=Memory
#operation returns: 
null
$>quit
#bye

Look at the docs on the jmxterm web site for information about embedding this in bash/perl/ruby/other scripts. I've used popen2 in Python or open3 in Perl to do this.

UPDATE: here's a one-liner using jmxterm:

echo run -b java.lang:type=Memory gc | java -jar jmxterm-1.0-alpha-4-uber.jar -n -l host:port

Addition to user3198490's answer. Running this command might give you the following error message:

$ jcmd 1805 GC.run    
[16:08:01]
1805:
com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file: target process not responding or HotSpot VM not loaded
...

This can be solved with help of this stackoverflow answer

sudo -u <process_owner> jcmd <pid> GC.run

where <process_owner> is the user that runs the process with PID <pid>. You can get both from top or htop


for linux:

$ jcmd $(pgrep java) GC.run

jcmd is packaged with the JDK, $(pgrep java) gets the process ID of java


There's a few other solutions (lots of good ones here already):

  • Write a little code to access the MemoryMBean and call gc().
  • Using a command-line JMX client (like cmdline-jmxclient, jxmterm) and run the gc() operation on the MemoryMBean

The following example is for the cmdline-jmxclient:

$ java -jar cmdline-jmxclient-0.10.3.jar - localhost:3812 'java.lang:type=Memory' gc

This is nice because it's only one line and you can put it in a script really easily.