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

 

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

                   }

}

 

 

 

时间: 2024-10-23 01:27:55

JDK5.0新特性系列---10.监控与管理虚拟机的相关文章

JDK5.0新特性系列---目录

  JDK5.0新特性系列---目录   JDK5.0新特性系列---1.自动装箱和拆箱   JDK5.0新特性系列---2.新的for循环 JDK5.0新特性系列---3.枚举类型 JDK5.0新特性系列---4.静态导入 JDK5.0新特性系列---5.可变长参数Varargs JDK5.0新特性系列---6.格式化输出 JDK5.0新特性系列---7.使用ProcessBuilder执行本地命令 JDK5.0新特性系列---8.泛型编程 JDK5.0新特性系列---9.注释功能Annota

JDK5.0新特性系列---11.6线程 BlockingQueue

  importjava.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;   /**     本例介绍一个特殊的队列:BlockingQueue,如果BlockQueue是空的,从BlockingQueue取东西的操作将会

JDK5.0新特性系列---11.3线程 锁Lock

  import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.Reent

JDK5.0新特性系列---11.1线程 Callable和Future

  import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future;   /**     从本节开始,主要介绍J2SE5.0与线程相关的新特性,新的线程类主要集中在java.util.concurrent 包中,本节实例将介绍如何使用java.uti

JDK5.0新特性系列---11.5.1线程 同步装置之Semaphore

    import java.util.ArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;   /**

JDK5.0新特性系列---2.新的for循环

  import java.util.ArrayList; import java.util.List;   /**  * 新的for循环,格式为for(type x:type y)  * 表示遍历数组或集合y的元素,把元素值赋给x  */ public class ForEach {        /**对整数数组求和*/        public static long getSum(int[] nums) throws Exception{               if(nums =

JDK5.0新特性系列---11.2线程 任务执行架构

  import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecu

JDK5.0新特性系列---8.泛型编程

  import java.util.ArrayList; import java.util.Collection; import java.util.List;   /**  *泛型编程关键掌握两点:  *1.在方法参数中使用通配符  *2.在方法的参数类型和返回类型中使用通用类型  */ /**关键技术  *1.通配符问号(?)表示任意类型.如"List<?>"表示可以存放任意对象类型的List  *2.通配符可以接extends和super,表示有限制的通配符.如&q

JDK5.0新特性系列---5.可变长参数Varargs

      /**  * 在J2SE5.0之前,当传入到方法的参数个数不固定时,经常采用数组的方式传递参数  * 在J2SE5.0之后,可以使用可变长参数的我给方法传递参数  */ /**  * 在参数类型和参数名之间使用"..."(三个英文的点),表示该参数为可变长的  * 通过新的for循环读取可变长参数中的值  * 一个方法里最多只能有一个变长参数,而且这个变长参数一定要放在参数表的最后一个参数 */ import static java.lang.System.*; publi