Trigger a remote gc using HotSpotVirtualMachine, inspired by jcmd's source code. @param pid
(Integer pid)
| 241 | * @param pid |
| 242 | */ |
| 243 | private static void triggerGc(Integer pid) { |
| 244 | VirtualMachine vm = null; |
| 245 | try { |
| 246 | vm = VirtualMachine.attach(String.valueOf(pid)); |
| 247 | HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm; |
| 248 | try (InputStream in = hvm.executeJCmd("GC.run");) { |
| 249 | byte b[] = new byte[256]; |
| 250 | int n; |
| 251 | do { |
| 252 | n = in.read(b); |
| 253 | if (n > 0) { |
| 254 | String s = new String(b, 0, n, "UTF-8"); |
| 255 | tty.print(s); |
| 256 | } |
| 257 | } while (n > 0); |
| 258 | tty.println(); |
| 259 | } |
| 260 | } catch (Exception e) { |
| 261 | tty.println(e.getMessage()); |
| 262 | } finally { |
| 263 | if (vm != null) { |
| 264 | try { |
| 265 | vm.detach(); |
| 266 | } catch (IOException e) { |
| 267 | tty.println(e.getMessage()); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | private static void printHelp() { |
| 274 | int leftLength = "-all:minsize=1024,byname".length(); |
no test coverage detected