I need a way to be able to trigger full GC from a linux console script on ubuntu.
I know this is extremely bad practice but without going into too much detail this keeps my server running, This is only meant for 1 or 2 days while I fix the actual problem, so I don’t have to wake up in the night and perform manual GC through jconsole or jvisualvm.
Alternatively I have to make a mouse script that clicks the button every 3-4 hours or so which is even worse.
Please help.
If you can have your application start a JMX server (which I believe is implied from your use of jconsole/jvisualvm), then you can invoke the Memory MBean’s
gcoperation via command-line utilities.Firstly you’ll need some kind of command-line JMX client. I’ve used this one in the past for simple command-line invocations and it worked fine. (Edit: In fact I used it just now to test out the following command, and it invoked GC successfully on a local Tomcat process)
Then you’ll need to work out the command to trigger garbage collection. I think this should work (you’ll of course need to change hosts/ports/credentials as appropriate):
Finally, you can schedule invocation of this command via
cronor equivalent.Voila!