问题描述
所以我在一个远程盒子上查看一个带有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?
推荐答案
您可以通过免费的程序。
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 / other脚本。我在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.
UPDATE:这里是使用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
这篇关于你如何从壳牌强制垃圾收集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!