How do you Force Garbage Collection from the Shell?

前端 未结 9 645
滥情空心
滥情空心 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 18:08

    just:

    kill -SIGQUIT <PID>
    
    0 讨论(0)
  • 2020-11-30 18:11

    Addition to user3198490's answer. Running this command might give you the following error message:

    $ jcmd 1805 GC.run    
    [16:08:01]
    1805:
    com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file: target process not responding or HotSpot VM not loaded
    ...
    

    This can be solved with help of this stackoverflow answer

    sudo -u <process_owner> jcmd <pid> GC.run
    

    where <process_owner> is the user that runs the process with PID <pid>. You can get both from top or htop

    0 讨论(0)
  • 2020-11-30 18:12

    If you are using jolokia with your application, you can trigger a garbage collection with this command:

    curl http://localhost:8558/jolokia/exec/java.lang:type=Memory/gc
    
    0 讨论(0)
提交回复
热议问题