引用tools.jar,Sun的 JDK 1.6以上運行
運行一下函數:
java
RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean(); String name = bean.getName(); int index = name.indexOf('@'); String pid = name.substring(0, index); //這裏要區分操做系統 HotSpotVirtualMachine machine = (HotSpotVirtualMachine) new sun.tools.attach.WindowsAttachProvider().attachVirtualMachine(pid); InputStream is = machine.heapHisto("-all"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int readed; byte[] buff = new byte[1024]; while((readed = is.read(buff)) > 0) os.write(buff, 0, readed); is.close(); machine.detach(); System.out.println(os);