virtualMachineClass= Class.forName(virtualMachineClassName);final Met..._getmethod("attach">
赞
踩
//需要将jdk安装lib目录下面的tools.jar放到项目里面
final String virtualMachineClassName = "com.sun.tools.attach.VirtualMachine";
//获取虚拟机控制权限
Class> virtualMachineClass= Class.forName(virtualMachineClassName);
final Method attachMethod = virtualMachineClass.getMethod("attach", String.class);
//获取tomcat进程id
final RuntimeMXBean runtime =ManagementFactory.getRuntimeMXBean();
final String info = runtime.getName();
final int index = info.indexOf("@");
String pid="0000";
if (index != -1) {
pid = info.substring(0, index);
}
WindowsVirtualMachine jvmVirtualMachine=(WindowsVirtualMachine)attachMethod.invoke(virtualMachineClass, pid);
%>
服务器线程信息$(document).ready(function(){
});
function fn_collectMemory(){
Runtime.getRuntime().gc();
%>
window.location.reload();
}
Runtime lRuntime = Runtime.getRuntime();
out.print("
*** 内存统计开始 ***
");
out.print("虚拟机空闲内存: "+lRuntime.freeMemory()+"(B)="+lRuntime.freeMemory()/1024/1024+"(MB)
");
out.print("虚拟机最大内存: "+lRuntime.maxMemory()+"(B)="+lRuntime.maxMemory()/1024/1024+"(MB)
");
out.print("虚拟机内存总量: "+lRuntime.totalMemory()+"(B)="+lRuntime.totalMemory()/1024/1024+"(MB)
");
out.print("已用内存:"+(lRuntime.totalMemory()-lRuntime.freeMemory())+"(B)="+(lRuntime.totalMemory()-lRuntime.freeMemory())/1024/1024+"(MB)
");
out.print("可用处理器: "+lRuntime.availableProcessors()+"
");
out.print("*** 内存统计结束 ***
");
%>
out.println("***************************虚拟机堆内存快照***************************");
InputStream in =(InputStream)jvmVirtualMachine.heapHisto(new Object[]{"-all"});
BufferedReader bufferReader= new BufferedReader(new InputStreamReader(in, "utf-8"));
String s = null;
while((s=bufferReader.readLine()) != null){
out.println(s);
}
bufferReader.close();
%>
out.println("***************************服务器线程统计***************************");
int count=1,tomcatThread=0;;
for(Map.Entry statcTrace:Thread.getAllStackTraces().entrySet()){
Thread thread =(Thread)statcTrace.getKey();
StackTraceElement[] stack =(StackTraceElement[])statcTrace.getValue();
if(thread.equals(Thread.currentThread())){
continue;
}
String threadName =thread.getName();
out.print("\n第"+( count++)+"条线程:"+threadName+"\n");
if(threadName.contains("catalina-exec-")){//catalina-exec-为server.xml中的配置属性(默认为 http-端口号-)
tomcatThread++;
}
for(StackTraceElement element:stack){
out.print("\t"+element+"\n");
}
}
out.print("\r\n http线程数量:"+tomcatThread+"条\n");
%>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。