Java 从 Windows 命令行运行时如何请求 JVM 垃圾收集(不是从代码中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/750649/
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
How to request JVM garbage collection (not from code) when run from Windows command-line
提问by Touko
how could I request Java garbage collection externally, starting the program from JAR (Windows BAT used)?
如何从外部请求 Java 垃圾收集,从 JAR(使用 Windows BAT)启动程序?
- From the Java code I can do it with
System.gc()
- When running a JNLP distribution, I get this "Java console" turned on from Control Panel / Java / ... and this Java console provides manual garbage collection.
- 从Java代码我可以做到
System.gc()
- 运行 JNLP 发行版时,我从控制面板 / Java / ... 打开了这个“Java 控制台”,并且这个 Java 控制台提供了手动垃圾收集。
But... When I'm running the jar from command-line / bat the java console doesn't seem to open. Couldn't find help with a brief googling, maybe somebody here?
但是...当我从命令行 / bat 运行 jar 时,java 控制台似乎没有打开。无法通过简短的谷歌搜索找到帮助,也许有人在这里?
采纳答案by Peter
You can use jconsoleto connect to a JVM that is running locally - This provides a "Perform GC" button on the GUI.
您可以使用jconsole连接到本地运行的 JVM - 这在 GUI 上提供了一个“执行 GC”按钮。
You'll need to specify -Dcom.sun.management.jmxremotewhen you kick off your java process.
当您启动 Java 进程时,您需要指定-Dcom.sun.management.jmxremote。
回答by Thorbj?rn Ravn Andersen
If you use the very latest java 6 you also have jvisualvm which complements and extend the jconsole functionality. They are both very useful tools.
如果您使用最新的 java 6,您还有 jvisualvm,它补充和扩展了 jconsole 功能。它们都是非常有用的工具。
回答by Paul de Vrieze
You normally should not have any reason to force garbage collection. Doing so, messes up the garbage collector algorithms (mainly their performance). It will also slow down the program while garbage is being collected. If there is a memory issue, you should use memory tracing tools to find out where references are being held? (Are listeners unregistered?)
您通常不应该有任何理由强制垃圾收集。这样做会弄乱垃圾收集器算法(主要是它们的性能)。在收集垃圾时,它也会减慢程序的速度。如果存在内存问题,您应该使用内存跟踪工具找出引用被保存在哪里?(听众是否未注册?)
As in the other answers, you can use jconsole or jvisualvm. You can also use jmx to do it programmatically.
与其他答案一样,您可以使用 jconsole 或 jvisualvm。您还可以使用 jmx 以编程方式执行此操作。
回答by Ogre Psalm33
For a purely command-line approach, you should be able to use jcmd
. I believe jcmd has been part of the standard JDK install since at least Java 1.7. The command would be something like this:
对于纯粹的命令行方法,您应该能够使用jcmd
. 我相信 jcmd 至少从 Java 1.7 开始就成为标准 JDK 安装的一部分。命令将是这样的:
jcmd <process-id> GC.run
You can get a list of available diagnostic commands that jcmd provides for a particular process like this:
您可以获得 jcmd 为特定进程提供的可用诊断命令列表,如下所示:
jcmd <process-id> help