用什麼辦法能夠獲取當前jvm中全部的對象實例

引用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);
相關文章
相關標籤/搜索