private static void triggerGc(Integer pid) {
		VirtualMachine vm = null;
		try {
			vm = VirtualMachine.attach(String.valueOf(pid));
			HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
			try (InputStream in = hvm.executeJCmd("GC.run");) {
				byte b[] = new byte[256];
				int n;
				do {
					n = in.read(b);
					if (n > 0) {
						String s = new String(b, 0, n, "UTF-8");
						tty.print(s);
					}
				} while (n > 0);
				tty.println();
			}
		} catch (Exception e) {
			tty.println(e.getMessage());
		} finally {
			if (vm != null) {
				try {
					vm.detach();
				} catch (IOException e) {
					tty.println(e.getMessage());
				}
			}
		}
	}
private static void triggerGc ( Integer pid ) { VirtualMachine vm = null ; try { vm = VirtualMachine . attach ( String . valueOf ( pid ) ) ; HotSpotVirtualMachine hvm = ( HotSpotVirtualMachine ) vm ; try ( InputStream in = hvm . executeJCmd ( "GC.run" ) ; ) { byte b [ ] = new byte [ 256 ] ; int n ; do { n = in . read ( b ) ; if ( n > 0 ) { String s = new String ( b , 0 , n , "UTF-8" ) ; tty . print ( s ) ; } } while ( n > 0 ) ; tty . println ( ) ; } } catch ( Exception e ) { tty . println ( e . getMessage ( ) ) ; } finally { if ( vm != null ) { try { vm . detach ( ) ; } catch ( IOException e ) { tty . println ( e . getMessage ( ) ) ; } } } }
Trigger a remote gc using HotSpotVirtualMachine, inspired by jcmd's source code.

@param pid