如何从没有X11的Linux控制台触发手动Java GC

时间:2023-01-07 12:02:47

I need a way to be able to trigger full GC from a linux console script on ubuntu. I know this is extremely bad practice but without going into too much detail this keeps my server running, This is only meant for 1 or 2 days while I fix the actual problem, so I don't have to wake up in the night and perform manual GC through jconsole or jvisualvm.

我需要一种能够从ubuntu上的linux控制台脚本触发完整GC的方法。我知道这是非常糟糕的做法,但没有太多细节,这使我的服务器保持运行,这只是在我解决实际问题的1或2天,所以我不必在夜间醒来并执行通过jconsole或jvisualvm手动GC。

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

或者,我必须制作一个鼠标脚本,每3-4小时左右点击一次按钮,这更糟糕。

Please help.

请帮忙。

4 个解决方案

#1


11  

If you can have your application start a JMX server (which I believe is implied from your use of jconsole/jvisualvm), then you can invoke the Memory MBean's gc operation via command-line utilities.

如果您可以让您的应用程序启动JMX服务器(我认为这是使用jconsole / jvisualvm所暗示的),那么您可以通过命令行实用程序调用Memory MBean的gc操作。

Firstly you'll need some kind of command-line JMX client. I've used this one in the past for simple command-line invocations and it worked fine. (Edit: In fact I used it just now to test out the following command, and it invoked GC successfully on a local Tomcat process)

首先,您需要某种命令行JMX客户端。我过去使用过这个用于简单的命令行调用,并且工作正常。 (编辑:事实上我刚刚用它来测试以下命令,并在本地Tomcat进程上成功调用了GC)

Then you'll need to work out the command to trigger garbage collection. I think this should work (you'll of course need to change hosts/ports/credentials as appropriate):

然后你需要找出命令来触发垃圾收集。我认为这应该可行(您当然需要根据需要更改主机/端口/凭据):

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

Finally, you can schedule invocation of this command via cron or equivalent.

最后,您可以通过cron或等效计划调用此命令。

Voila!

瞧!

#2


7  

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

如果你有oracle jvm 1.7,你可以使用jcmd列出jvm PID,然后使用jcmd GC.run到GC。

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

jcmd help将显示其他可用命令。

#3


3  

jcmd <pid> GC.run

Example:

例:

jcmd 20350 GC.run

#4


-2  

It's not bad practice, it is impossible - even for the java application being executed by the JVM. There is a gc() call available but even it is only a hint to the JVM to run garbage collection. From the console, there is normally no way to influence the JVM while it is running.

实践并不坏 - 这是不可能的 - 即使是由JVM执行的Java应用程序也是如此。有一个gc()调用可用,但即使它只是一个提示JVM运行垃圾收集。从控制台,通常无法在JVM运行时影响它。

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

有些人已经为Windows平台提出了这个问题,请参阅问题从Windows命令行运行时如何请求JVM垃圾回收(而不是代码)

You might check out the JVM arguments for stack/heap sizes (both min and max). There are lots of tweaks you can do in that area but they are mostly specific to the JVM you are using.

您可以查看堆栈/堆大小(最小和最大)的JVM参数。您可以在该区域进行大量调整,但它们主要针对您正在使用的JVM。

JVM performance tuning for large applications

针对大型应用程序的JVM性能调优

#1


11  

If you can have your application start a JMX server (which I believe is implied from your use of jconsole/jvisualvm), then you can invoke the Memory MBean's gc operation via command-line utilities.

如果您可以让您的应用程序启动JMX服务器(我认为这是使用jconsole / jvisualvm所暗示的),那么您可以通过命令行实用程序调用Memory MBean的gc操作。

Firstly you'll need some kind of command-line JMX client. I've used this one in the past for simple command-line invocations and it worked fine. (Edit: In fact I used it just now to test out the following command, and it invoked GC successfully on a local Tomcat process)

首先,您需要某种命令行JMX客户端。我过去使用过这个用于简单的命令行调用,并且工作正常。 (编辑:事实上我刚刚用它来测试以下命令,并在本地Tomcat进程上成功调用了GC)

Then you'll need to work out the command to trigger garbage collection. I think this should work (you'll of course need to change hosts/ports/credentials as appropriate):

然后你需要找出命令来触发垃圾收集。我认为这应该可行(您当然需要根据需要更改主机/端口/凭据):

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

Finally, you can schedule invocation of this command via cron or equivalent.

最后,您可以通过cron或等效计划调用此命令。

Voila!

瞧!

#2


7  

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

如果你有oracle jvm 1.7,你可以使用jcmd列出jvm PID,然后使用jcmd GC.run到GC。

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

jcmd help将显示其他可用命令。

#3


3  

jcmd <pid> GC.run

Example:

例:

jcmd 20350 GC.run

#4


-2  

It's not bad practice, it is impossible - even for the java application being executed by the JVM. There is a gc() call available but even it is only a hint to the JVM to run garbage collection. From the console, there is normally no way to influence the JVM while it is running.

实践并不坏 - 这是不可能的 - 即使是由JVM执行的Java应用程序也是如此。有一个gc()调用可用,但即使它只是一个提示JVM运行垃圾收集。从控制台,通常无法在JVM运行时影响它。

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

有些人已经为Windows平台提出了这个问题,请参阅问题从Windows命令行运行时如何请求JVM垃圾回收(而不是代码)

You might check out the JVM arguments for stack/heap sizes (both min and max). There are lots of tweaks you can do in that area but they are mostly specific to the JVM you are using.

您可以查看堆栈/堆大小(最小和最大)的JVM参数。您可以在该区域进行大量调整,但它们主要针对您正在使用的JVM。

JVM performance tuning for large applications

针对大型应用程序的JVM性能调优