How to run manual Java GC from Linux console without X11

I need a way to run a full GC from a linux script console on ubuntu. I know this is a very bad practice, but without going into details, it keeps my server, it is only for 1 or 2 days while I fix the real problem, so I do not need to wake up at night and execute the GC manual through jconsole or jvisualvm.

Alternatively, I have to make a mouse script that clicks a button every 3-4 hours or so, which is even worse.

Please, help.

+7
java garbage-collection linux
source share
4 answers

If you can run the JMX server application (which I assume is implied by using jconsole / jvisualvm), you can invoke the MBean gc operation in memory using command line utilities.

First, you need some kind of JMX client from the command line. I used this in the past for simple command line calls, and it worked fine. (Edit: I actually used it just now to test the following command, and it successfully called GC in the local Tomcat process)

Then you will need to develop a command to start garbage collection. I think this should work (you, of course, will need to change hosts / ports / credentials):

 java -jar cmdline-jmxclient-XXjar - localhost:8081 java.lang:type=Memory gc 

Finally, you can schedule this command to be cron via cron or its equivalent.

Voila!

+11
source share

If you have oracle jvm 1.7, you can use jcmd to list the jvm ids, and then jcmd <pid> GC.run in the GC.

jcmd <pid> help will show you what other commands are available.

+7
source share
 jcmd <pid> GC.run 

Example:

 jcmd 20350 GC.run 
+3
source share

This is good practice, it is not possible - even for a Java application running the JVM. There is access to the gc () call, but even this is just a hint of the JVM to start garbage collection. On the console, there is usually no way to influence the JVM during operation.

Some asked this question for the Windows platform, see the question How to request JVM garbage collection (not from code) when starting from the Windows command line

You can check the JVM arguments for stack / heap sizes (both min and max). There are many settings you can make in this area, but they are mostly specific to the JVMs that you use.

JVM performance tuning for large applications

-2
source share

All Articles