How do you Force Garbage Collection from the Shell

2019-01-16 02:06发布

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.

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?

8条回答
姐就是有狂的资本
2楼-- · 2019-01-16 02:37

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
查看更多
Melony?
3楼-- · 2019-01-16 02:41

I don't think there is any command line option for same.

You will need to use jvisualvm/jconsole for same.

I would rather suggest you to use these tools to identity , why your program is high on memory.

Anyways you shouldn't force GC, as it would certainly disturb GC algorithm and make your program slow.

查看更多
登录 后发表回答