How do you Force Garbage Collection from the Shell?(你如何从 Shell 强制垃圾收集?)
问题描述
所以我在一个远程盒子上查看一个带有 jmap 的堆,我想强制对其进行垃圾收集.在不弹出 jvisualvm 或 jconsole 和朋友的情况下如何做到这一点?
So I am looking at a heap with jmap on a remote box and I want to force garbage collection on it. How do you do this without popping into jvisualvm or jconsole and friends?
我知道您不应该进行强制垃圾回收的做法——您应该弄清楚堆变大/增长的原因.
I know you shouldn't be in the practice of forcing garbage collection -- you should just figure out why the heap is big/growing.
我也意识到 System.GC() 实际上并不强制进行垃圾回收——它只是告诉 GC 你希望它发生.
I also realize the System.GC() doesn't actually force garbage collection -- it just tells the GC that you'd like it to occur.
话虽如此,有没有办法轻松做到这一点?我缺少一些命令行应用程序?
Having said that is there a way to do this easily? Some command line app I'm missing?
推荐答案
你可以通过免费的 jmxterm 程序.
You can do this via the free jmxterm program.
像这样启动它:
java -jar jmxterm-1.0-alpha-4-uber.jar
从那里,您可以连接到主机并触发 GC:
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
查看 jmxterm 网站上的文档,了解有关将其嵌入 bash/perl/ruby/其他脚本的信息.我在 Python 中使用了 popen2 或在 Perl 中使用了 open3.
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.
更新:这是一个使用 jmxterm 的单行:
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
这篇关于你如何从 Shell 强制垃圾收集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:你如何从 Shell 强制垃圾收集?
基础教程推荐
- 验证是否调用了所有 getter 方法 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 大摇大摆的枚举 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
