How do you Force Garbage Collection from the Shell?

前端 未结 9 684
滥情空心
滥情空心 2020-11-30 17:42

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?<

9条回答
  •  遥遥无期
    2020-11-30 17:57

    You can do this via the free jmxterm program.

    Fire it up like so:

    java -jar jmxterm-1.0-alpha-4-uber.jar
    

    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
    

    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: 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
    

提交回复
热议问题