diff --git a/src/main/java/de/steamwar/Agent.java b/src/main/java/de/steamwar/Agent.java index 025854c..1e2aa26 100644 --- a/src/main/java/de/steamwar/Agent.java +++ b/src/main/java/de/steamwar/Agent.java @@ -1,6 +1,12 @@ package de.steamwar; +import com.ibm.jvm.Dump; +import com.ibm.jvm.InvalidDumpOptionException; + +import java.io.File; import java.lang.instrument.Instrumentation; +import java.util.logging.Level; +import java.util.logging.Logger; public class Agent { private Agent() {} @@ -24,6 +30,13 @@ public class Agent { sampler.stop(); sampler = null; break; + case "heap": + try { + Dump.heapDumpToFile(new File(Main.jarPath.getParentFile(), "heap.phd").getPath()); + } catch (InvalidDumpOptionException e) { + Logger.getGlobal().log(Level.WARNING, "Could not perform heap dump", e); + } + break; } } } diff --git a/src/main/java/de/steamwar/Main.java b/src/main/java/de/steamwar/Main.java index 319b4c6..48f2483 100644 --- a/src/main/java/de/steamwar/Main.java +++ b/src/main/java/de/steamwar/Main.java @@ -28,10 +28,13 @@ public class Main { System.out.println(vmd.id() + " " + vmd.displayName()); } } - }else if(args.length == 1) { - try { - VirtualMachine vm = VirtualMachine.attach(args[0]); + return; + } + try { + VirtualMachine vm = VirtualMachine.attach(args[0]); + + if(args.length == 1 || !"heap".equals(args[1])) { vm.loadAgent(jarPath.getAbsolutePath(), "start"); System.out.println("Press keyboard to stop sampling..."); @@ -39,13 +42,15 @@ public class Main { vm.loadAgent(jarPath.getAbsolutePath(), "stop"); - vm.detach(); - new ProcessBuilder("xdot", "samples.dot").directory(jarPath.getParentFile()).start(); - } catch (AttachNotSupportedException | IOException | AgentLoadException | AgentInitializationException e) { - e.printStackTrace(); + } else { + vm.loadAgent(jarPath.getAbsolutePath(), "heap"); } + vm.detach(); + + } catch (AttachNotSupportedException | IOException | AgentLoadException | AgentInitializationException e) { + e.printStackTrace(); } } }