如何在没有 X11 的情况下从 linux 控制台触发手动 Java GC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4155696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:56:10  来源:igfitidea点击:

How to trigger manual Java GC from linux console with no X11

javalinuxgarbage-collection

提问by ?var ?rn Kvaran

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.

请帮忙。

采纳答案by Andrzej Doyle

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 gcoperation via command-line utilities.

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

Firstly you'll need some kind of command-line JMX client. I've used this onein 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 cronor equivalent.

最后,您可以通过cron或等效的方式安排此命令的调用。

Voila!

瞧!

回答by Kelly S. French

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 平台提出了这个问题,请参阅问题How to request JVM garbage collection (not from code) when run from Windows command-line

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 性能调优

回答by arya

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

如果您有oracle jvm 1.7,您可以使用jcmd来列出jvm PID,然后jcmd <pid> GC.run进行GC。

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

jcmd <pid> help将向您展示其他可用的命令。

回答by helder.vasc

jcmd <pid> GC.run

Example:

例子:

jcmd 20350 GC.run