package book.j2se5;
import java.lang.management.ClassLoadingMXBean;
import java.lang.management.CompilationMXBean;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.util.List;
public class JDKBean {
/**
*MemoryMXBean是Java虚拟机的内存系统的管理接口
*/
public static void printMemoryMXBean(){
//获得单一实例
MemoryMXBean instance=ManagementFactory.getMemoryMXBean();
System.out.printf("%n--%s--%n",instance.getClass().getName());
//返回用于对象分配的堆的当前内存
System.out.printf("%s:%s%n","HeapMemoryUsage",instance.getHeapMemoryUsage());
//返回java虚拟机使用的非堆内存的当前使用量
System.out.printf("%s:%s%n","getNonHeapMemoryUsage",instance.getNonHeapMemoryUsage());
instance.gc();
}
/**
* ClassLoadingMXBean是Java虚拟机的类加载系统的管理接口
*Java虚拟机具有此接口的实现类的单个实例
*/
public static void printClassLoadingMXBean(){
//获得单一实例
ClassLoadingMXBean instance=ManagementFactory.getClassLoadingMXBean();
System.out.printf("%n--%s--%n",instance.getClass().getName());
//返回当前加载Java虚拟机中的类的数量
System.out.printf("%s:%s%n","LoadedClassCount",instance.getLoadedClassCount());
//返回自Java虚拟机开始执行到目前已经加载的类的总数
System.out.printf("%s:%s%n","TotalLoadedClassCount",instance.getTotalLoadedClassCount());
//返回自Java虚拟机开始执行到目前已经卸载的类的总数
System.out.printf("%s:%s%n","UnloadedClassCount",instance.getUnloadedClassCount());
}
/**
*ThreadMXBean是Java虚拟机线程系统的管理接口
*Java虚拟机具有此接口的实现类的单个实例
*/
public static void printThreadMXBean(){
//获得单一实例
ThreadMXBean instance=ManagementFactory.getThreadMXBean();
System.out.printf("%n---%s---%n",instance.getClass().getName());
//返回活动线程的当前数目,包括守护线程和非守护线程
System.out.printf("%s:%s%n","ThreadCount",instance.getThreadCount());
//返回活动线程ID
System.out.printf("%s:%n","Thread IDs");
long[] ids=instance.getAllThreadIds();
for(long id:ids){
System.out.printf("%s; ",id);
}
System.out.printf("%n");
//返回活动守护线程的当前数目
System.out.printf("%s:%s%n","DaemonThreadCount",instance.getDaemonThreadCount());
//返回自从Java虚拟机启动或峰值重置以来峰值活动线程计数
System.out.printf("%s:%s%n","PeakThreadCount",instance.getPeakThreadCount());
//返回当前线程的总CPU时间
System.out.printf("%s:%s%n","CurrentThreadCpuTime",instance.getCurrentThreadCpuTime());
//返回当前线程在用户模式中执行的CPU
System.out.printf("%s:%s%n","CurrentThreadUserTime",instance.getCurrentThreadUserTime());
}
public static void printRuntimeMXBean(){
RuntimeMXBean instance=ManagementFactory.getRuntimeMXBean();
System.out.printf("%n---%s---%n",instance.getClass().getName());
System.out.printf("%s:%s%n","BootClassPath",instance.getBootClassPath());
System.out.printf("%s:%s%n","ClassPath",instance.getClassPath());
System.out.printf("%s%n","InputArguments");
List<String> args=instance.getInputArguments();
for(String arg:args){
System.out.printf("%s; ", arg);
}
System.out.printf("%s:%s%n","LibraryPath",instance.getLibraryPath());
System.out.printf("%s:%s%n", "ManagementSpecVersion",instance.getManagementSpecVersion());
System.out.printf("%s:%s%n", "Name",instance.getName());
System.out.printf("%s:%s%n","SpecName",instance.getSpecName());
System.out.printf("%s:%s%n","SpecVersion",instance.getSpecVersion());
System.out.printf("%s:%s%n","VmlName",instance.getVmName());
System.out.printf("%s:%s%n","VmVendor",instance.getVmVendor());
System.out.printf("%s:%s%n","VmVersion",instance.getVmVersion());
System.out.printf("%s:%s%n","StartTime",instance.getStartTime());
System.out.printf("%s:%s%n","Uptime",instance.getUptime());
}
public static void printCompilationMXBean(){
CompilationMXBean instance=ManagementFactory.getCompilationMXBean();
System.out.printf("%n---%s---%n",instance.getClass().getName());
System.out.printf("%s:%s%n","Name",instance.getName());
System.out.printf("%s:%s%n","TotalCompilationTime",instance.getTotalCompilationTime());
}
public static void printGarbageCollectionMXBean(){
List<GarbageCollectorMXBean> instances=ManagementFactory.getGarbageCollectorMXBeans();
System.out.printf("%n---%s---%n",GarbageCollectorMXBean.class.getName());
for(GarbageCollectorMXBean instance:instances){
System.out.printf("***%s:%s***%n","Name",instance.getName());
System.out.printf("%s:%s%n","CollectionCount",instance.getCollectionCount());
System.out.printf("%s:%s%n","CollectionTime",instance.getCollectionTime());
}
}
public static void printOperationSystemMXBean(){
OperatingSystemMXBean instance=ManagementFactory.getOperatingSystemMXBean();
System.out.printf("%n---%s---%n",instance.getClass().getName());
System.out.printf("%s:%s%n","Arch",instance.getArch());
System.out.printf("%s:%s%n","AvailableProcessors",instance.getAvailableProcessors());
System.out.printf("%s:%s%n","Name",instance.getName());
System.out.printf("%s:%s%n","Version",instance.getVersion());
}
public static void main(String[] args) {
JDKBean.printOperationSystemMXBean();
JDKBean.printRuntimeMXBean();
JDKBean.printClassLoadingMXBean();
JDKBean.printCompilationMXBean();
JDKBean.printMemoryMXBean();
JDKBean.printThreadMXBean();
JDKBean.printGarbageCollectionMXBean();
}
}