JDK5.0新特性系列---10.监控与管理虚拟机

简介: import java.lang.management.ClassLoadingMXBean; import java.lang.management.CompilationMXBean; import java.

 

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;

 

/**

        本例介绍如何监控和管理Java虚拟机,包括获取Java虚拟机的内存使用情况、线程数、运行时间等。

        J2SE5.0中使用Bean监控和管理Java虚拟机,java.lang.management.ManagementFactory是管理Bean的工厂类,

通过它的get系列方法能够获得不同的管理Bean的实例。

1. java.lang.management.MemoryMXBean:                                Bean用于管理Java虚拟机的内存系统。

                                                                                                                               一个Java虚拟机具有一个实例。

2.java.lang.management.ClassLoadingMXBean:                     Bean用于管理Java虚拟机的类加载系统。

                                                                                                                               一个Java虚拟机具有一个实例。

3.java.lang.management.TreadMXBean:                                       Bean用于管理Java虚拟机的线程系统。

                                                                                                                                一个Java虚拟机具有一个实例。

4.java.lang.management.RuntimeMXBean:                                 Bean用于管理Java虚拟机的线程系统。

                                                                                                                                一个Java虚拟机具有一个实例。

5.java.lang.management.OperatingSystemMXBean:            Bean用于管理操作系统。

                                                                                                                                一个Java虚拟机具有一个实例。

6.java.lang.management.CompilationMXBean:                        Bean用于管理Java虚拟机的编译系统。

                                                                                                                                 一个Java虚拟机具有一个实例。

7.java.lang.management.GarbageCollectorMXBean:             Bean用于管理Java虚拟机的垃圾回收系统。

                                                                                                                                 一个Java虚拟机具有一个或者多个实例。

 */

public class JDKMBean {

                   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();

                   }

                  

                   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","UnloadedLoadedClassCount",instance.getUnloadedClassCount());

                   }

                  

                   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.println();

                                      //返回活动守护线程的当前数目

                                      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","CurrentCpuTime",instance.getCurrentThreadCpuTime());

                                      //返回当前线程在用户模式中执行的CPU时间

                                      System.out.printf("%s: %s%n","CurrentThreadCpuTime",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());

                                      //返回系统类加载器用于搜索类文件的Java类路径

                                      System.out.printf("%s: %s%n","ClassPath",instance.getClassPath());

                                      //引用传递给Java虚拟机的输入变量,其中不包括传递给main方法的变量

                                      System.out.printf("%s: %n","InputArguments");

                                      List<String> args = instance.getInputArguments();

                                      for(String arg : args){

                                                         System.out.printf("%s;  ",arg);

                                      }

                                      //返回Java库路径

                                      System.out.printf("%s: %s%n","LibraryPath",instance.getLibraryPath());

                                      //返回正在运行的Java虚拟机实现的管理接口的规范版本

                                      System.out.printf("%s: %s%n","ManagementSpecVersion",instance.getManagementSpecVersion());

                                      //返回正在运行的Java虚拟机的名称

                                      System.out.printf("%s: %s%n","Name",instance.getName());

                                     

                                      //返回Java虚拟机规范名称

                                      System.out.printf("%s: %s%n","SpecName",instance.getSpecName());

                                      //返回Java虚拟机规范提供商

                                      System.out.printf("%s: %s%n","SpecVendor",instance.getSpecVendor());

                                      //返回Java虚拟机规范版本

                                      System.out.printf("%s: %s%n","SpecVersion",instance.getSpecVersion());

                                     

                                      //返回Java虚拟机实现名称

                                      System.out.printf("%s: %s%n","VmName",instance.getVmName());

                                      //返回Java虚拟机实现提供商

                                      System.out.printf("%s: %s%n","VmVendor",instance.getVmVendor());

                                      //返回Java虚拟机实现版本

                                      System.out.printf("%s: %s%n","VmVersion",instance.getVmVersion());

                                     

                                      //返回Java虚拟机的启动时间

                                      System.out.printf("%s: %s%n","startTime",instance.getStartTime());

                                      //返回Java虚拟机的正常运行时间

                                      System.out.printf("%s: %s%n","Uptime",instance.getUptime());

                   }

                  

                   public static void printOperatingSystemMXBean(){

                                      //获得单一实例

                                      OperatingSystemMXBean instance = ManagementFactory.getOperatingSystemMXBean();

                                      System.out.printf("%n---%s---%n",instance.getClass().getName());

                                      //返回操作系统的架构

                                      System.out.printf("%s: %s%n","Arch",instance.getArch());

                                      //返回Java虚拟机可以使用的处理器数目

                                      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 printCompilationMXBean(){

                                      //获得单一实例

                                      CompilationMXBean instance = ManagementFactory.getCompilationMXBean();

                                      System.out.printf("%n---%s---%n",instance.getClass().getName());

                                      //返回即时(JIT)编译器的名称

                                      System.out.printf("%s: %s%n","JIT",instance.getName());

                                      //返回在编译上花费的累积耗费时间的近似值

                                      System.out.printf("%s: %s%n","TotalCompilationTime",instance.getTotalCompilationTime());

                   }

                  

                   public static void printGargageCollectorMXBean(){

                                      //获得单一实例

                                      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 main(String[] args){

                                      JDKMBean.printMemoryMXBean();

                                      JDKMBean.printClassLoadingMXBean();

                                      JDKMBean.printThreadMXBean();

                                      JDKMBean.printRuntimeMXBean();

                                      JDKMBean.printOperatingSystemMXBean();

                                      JDKMBean.printCompilationMXBean();

                                      JDKMBean.printGargageCollectorMXBean();

                   }

}

 

 

 

目录
相关文章
|
8天前
|
容器
jdk8新特性-详情查看文档
jdk8新特性-详情查看文档
24 7
|
2月前
|
监控 数据可视化 Java
如何使用JDK自带的监控工具JConsole来监控线程池的内存使用情况?
如何使用JDK自带的监控工具JConsole来监控线程池的内存使用情况?
|
3月前
|
容器
jdk8新特性-详情查看文档
jdk8新特性-详情查看文档
46 3
|
2月前
|
存储 安全 Java
JDK1.8 新的特性
JDK1.8 新的特性
25 0
|
3月前
|
监控 数据可视化 Java
使用JDK自带的监控工具JConsole来监控线程池的内存使用情况
使用JDK自带的监控工具JConsole来监控线程池的内存使用情况
|
3月前
|
编解码 安全 Java
jdk8新特性-接口和日期处理
jdk8新特性-接口和日期处理
|
4月前
|
Java API
JDK8到JDK25版本升级的新特性问题之使用Collectors.teeing()来计算一个列表中学生的平均分和总分如何操作
JDK8到JDK25版本升级的新特性问题之使用Collectors.teeing()来计算一个列表中学生的平均分和总分如何操作
|
4月前
|
Java API Apache
JDK8到JDK24版本升级的新特性问题之在Java中,HttpURLConnection有什么局限性,如何解决
JDK8到JDK24版本升级的新特性问题之在Java中,HttpURLConnection有什么局限性,如何解决
|
4月前
|
Oracle Java 关系型数据库
JDK8到JDK29版本升级的新特性问题之未来JDK的升级是否会成为必然趋势,如何理解
JDK8到JDK29版本升级的新特性问题之未来JDK的升级是否会成为必然趋势,如何理解
|
4月前
|
Oracle 安全 Java
JDK8到JDK28版本升级的新特性问题之在Java 15及以后的版本中,密封类和密封接口是怎么工作的
JDK8到JDK28版本升级的新特性问题之在Java 15及以后的版本中,密封类和密封接口是怎么工作的