博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java使用java.lang.management监视和管理 Java 虚拟机
阅读量:6208 次
发布时间:2019-06-21

本文共 8116 字,大约阅读时间需要 27 分钟。

软件包 java.lang.management

提供管理接口,用于监视和管理 Java 虚拟机以及 Java 虚拟机在其上运行的操作系统。

 

接口摘要

用于 Java 虚拟机的类加载系统的管理接口。
用于 Java 虚拟机的编译系统的管理接口。
用于 Java 虚拟机的垃圾回收的管理接口。
内存管理器的管理接口。
Java 虚拟机的内存系统的管理接口。
内存池的管理接口。
用于操作系统的管理接口,Java 虚拟机在此操作系统上运行。
Java 虚拟机的运行时系统的管理接口。

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.MemoryManagerMXBean;import java.lang.management.MemoryPoolMXBean;import java.lang.management.MemoryUsage;import java.lang.management.OperatingSystemMXBean;import java.lang.management.RuntimeMXBean;import java.lang.management.ThreadMXBean;import java.util.List;import javax.management.MBeanServerConnection;public class MBeanDemo {    public static void main(String[] args) {        showJvmInfo();        showMemoryInfo();        showSystem();        showClassLoading();        showCompilation();        showThread();        showGarbageCollector();        showMemoryManager();        showMemoryPool();    }    /**     * Java 虚拟机的运行时系统     */    public static void showJvmInfo() {        RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();        String vendor = mxbean.getVmVendor();        System.out.println("jvm name:" + mxbean.getVmName());        System.out.println("jvm version:" + mxbean.getVmVersion());        System.out.println("jvm bootClassPath:" + mxbean.getBootClassPath());        System.out.println("jvm start time:" + mxbean.getStartTime());    }    /**     * Java 虚拟机的内存系统     */    public static void showMemoryInfo() {        MemoryMXBean mem = ManagementFactory.getMemoryMXBean();        MemoryUsage heap = mem.getHeapMemoryUsage();        System.out.println("Heap committed:" + heap.getCommitted() + " init:" + heap.getInit() + " max:"                           + heap.getMax() + " used:" + heap.getUsed());    }    /**     * Java 虚拟机在其上运行的操作系统     */    public static void showSystem() {        OperatingSystemMXBean op = ManagementFactory.getOperatingSystemMXBean();        System.out.println("Architecture: " + op.getArch());        System.out.println("Processors: " + op.getAvailableProcessors());        System.out.println("System name: " + op.getName());        System.out.println("System version: " + op.getVersion());        System.out.println("Last minute load: " + op.getSystemLoadAverage());    }        /**     * Java 虚拟机的类加载系统     */    public static void showClassLoading(){        ClassLoadingMXBean cl = ManagementFactory.getClassLoadingMXBean();        System.out.println("TotalLoadedClassCount: " + cl.getTotalLoadedClassCount());        System.out.println("LoadedClassCount" + cl.getLoadedClassCount());        System.out.println("UnloadedClassCount:" + cl.getUnloadedClassCount());    }        /**     * Java 虚拟机的编译系统     */    public static void showCompilation(){        CompilationMXBean com = ManagementFactory.getCompilationMXBean();        System.out.println("TotalCompilationTime:" + com.getTotalCompilationTime());        System.out.println("name:" + com.getName());    }        /**     * Java 虚拟机的线程系统     */    public static void showThread(){        ThreadMXBean thread = ManagementFactory.getThreadMXBean();        System.out.println("ThreadCount" + thread.getThreadCount());        System.out.println("AllThreadIds:" + thread.getAllThreadIds());        System.out.println("CurrentThreadUserTime" + thread.getCurrentThreadUserTime());        //......还有其他很多信息    }        /**     * Java 虚拟机中的垃圾回收器。     */    public static void showGarbageCollector(){        List
gc = ManagementFactory.getGarbageCollectorMXBeans(); for(GarbageCollectorMXBean GarbageCollectorMXBean : gc){ System.out.println("name:" + GarbageCollectorMXBean.getName()); System.out.println("CollectionCount:" + GarbageCollectorMXBean.getCollectionCount()); System.out.println("CollectionTime" + GarbageCollectorMXBean.getCollectionTime()); } } /** * Java 虚拟机中的内存管理器 */ public static void showMemoryManager(){ List
mm = ManagementFactory.getMemoryManagerMXBeans(); for(MemoryManagerMXBean eachmm: mm){ System.out.println("name:" + eachmm.getName()); System.out.println("MemoryPoolNames:" + eachmm.getMemoryPoolNames().toString()); } } /** * Java 虚拟机中的内存池 */ public static void showMemoryPool(){ List
mps = ManagementFactory.getMemoryPoolMXBeans(); for(MemoryPoolMXBean mp : mps){ System.out.println("name:" + mp.getName()); System.out.println("CollectionUsage:" + mp.getCollectionUsage()); System.out.println("type:" + mp.getType()); } } /** * 访问 MXBean 的方法的三种方法 */ public static void visitMBean(){ //第一种直接调用同一 Java 虚拟机内的 MXBean 中的方法。 RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean(); String vendor1 = mxbean.getVmVendor(); System.out.println("vendor1:" + vendor1); //第二种通过一个连接到正在运行的虚拟机的平台 MBeanServer 的 MBeanServerConnection。 MBeanServerConnection mbs = null; // Connect to a running JVM (or itself) and get MBeanServerConnection // that has the JVM MXBeans registered in it /* try { // Assuming the RuntimeMXBean has been registered in mbs ObjectName oname = new ObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME); String vendor2 = (String) mbs.getAttribute(oname, "VmVendor"); System.out.println("vendor2:" + vendor2); } catch (Exception e) { e.printStackTrace(); } */ //第三种使用 MXBean 代理// MBeanServerConnection mbs3 = null;// RuntimeMXBean proxy;// try {// proxy = ManagementFactory.newPlatformMXBeanProxy(mbs3,ManagementFactory.RUNTIME_MXBEAN_NAME,// RuntimeMXBean.class);// String vendor = proxy.getVmVendor();// } catch (IOException e) {// e.printStackTrace();// } }}
输出:
   
   
   
jvm name:Java HotSpot(TM) Client VMjvm version:1.6.0-b105jvm bootClassPath:C:\Program Files\Java\jdk1.6.0\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.6.0\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0\jre\lib\jce.jar;C:\Program Files\Java\jdk1.6.0\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.6.0\jre\classesjvm start time:1307440032774Heap committed:5177344 init:0 max:66650112 used:632640Architecture: x86Processors: 2System name: Windows XPSystem version: 5.1Last minute load: -1.0TotalLoadedClassCount: 381LoadedClassCount381UnloadedClassCount:0TotalCompilationTime:3name:HotSpot Client CompilerThreadCount5AllThreadIds:[J@47b480CurrentThreadUserTime15625000name:CopyCollectionCount:0CollectionTime0name:MarkSweepCompactCollectionCount:0CollectionTime0name:CodeCacheManagerMemoryPoolNames:[Ljava.lang.String;@1389e4name:CopyMemoryPoolNames:[Ljava.lang.String;@c20e24name:MarkSweepCompactMemoryPoolNames:[Ljava.lang.String;@2e7263name:Code CacheCollectionUsage:nulltype:Non-heap memoryname:Eden SpaceCollectionUsage:init = 917504(896K) used = 0(0K) committed = 0(0K) max = 4194304(4096K)type:Heap memoryname:Survivor SpaceCollectionUsage:init = 65536(64K) used = 0(0K) committed = 0(0K) max = 458752(448K)type:Heap memoryname:Tenured GenCollectionUsage:init = 4194304(4096K) used = 0(0K) committed = 0(0K) max = 61997056(60544K)type:Heap memoryname:Perm GenCollectionUsage:init = 12582912(12288K) used = 0(0K) committed = 0(0K) max = 67108864(65536K)type:Non-heap memoryname:Perm Gen [shared-ro]CollectionUsage:init = 8388608(8192K) used = 0(0K) committed = 0(0K) max = 8388608(8192K)type:Non-heap memoryname:Perm Gen [shared-rw]CollectionUsage:init = 12582912(12288K) used = 0(0K) committed = 0(0K) max = 12582912(12288K)type:Non-heap memory

转载于:https://www.cnblogs.com/secbook/archive/2012/05/31/2655183.html

你可能感兴趣的文章
弃 Java 而使用 Kotlin 的你后悔了吗?| kotlin将会是最好的开发语言
查看>>
JavaScript 数据类型
查看>>
量子通信和大数据最有市场突破前景
查看>>
StringBuilder用法小结
查看>>
android studio :cannot resolve symbol R
查看>>
paper 20 :color moments
查看>>
代码大全
查看>>
DataTable.ImportRow()与DataTable.Rows.Add()的区别
查看>>
程序集、应用程序配置及App.config和YourSoft.exe.config .
查看>>
二叉树的基本操作及应用(三)
查看>>
朱晔和你聊Spring系列S1E3:Spring咖啡罐里的豆子
查看>>
IOS CALayer的属性和使用
查看>>
温故而知新:柯里化 与 bind() 的认知
查看>>
Django REST framework
查看>>
CSS 如何让Table的里面TD全有边框 而Table的右左边框没有
查看>>
apache 虚拟主机详细配置:http.conf配置详解
查看>>
BABOK - 开篇:业务分析知识体系介绍
查看>>
Java入门系列-22-IO流
查看>>
MySQL:Innodb page clean 线程 (二) :解析
查看>>
垃圾回收算法优缺点对比
查看>>