public static byte[] getThreadDump(int pid) throws AttachNotSupportedException, IOException { HotSpotVirtualMachine virtualMachine = null; try { virtualMachine = (HotSpotVirtualMachine) VirtualMachine.attach(String.valueOf(pid)); try (InputStream in = virtualMachine.remoteDataDump()) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int count; byte[] buf = new byte[256]; while ((count = in.read(buf)) != -1) { baos.write(buf, 0, count); } return baos.toByteArray(); } } finally { if (virtualMachine != null) { try { virtualMachine.detach(); } catch (IOException ignore) { } } } } public static String getThreadDumpAsString(int pid) throws AttachNotSupportedException, IOException {