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);
它会生成:
num #instances #bytes class name
----------------------------------------------
1: 3981 651584 [C
2: 479 326304 [B
3: 1196 321488 <symbolKlass>
4: 48 79384 [I
5: 2580 61920 java.lang.String
6: 340 40232 <constMethodKlass>
7: 340 27536 <methodKlass>
这样的格式,说明不同Class的实例数、占用内存等
上面代码必须引用tools.jar,必须是Sun的 JDK 1.6以上运行
http://topic.youkuaiyun.com/u/20111101/10/f02a1f7f-6e1b-473b-b02e-08bca6af5b55.html?43331
86万+

被折叠的 条评论
为什么被折叠?



