java.lang.management

  1.   java.lang.management 提供管理接口,用於監視和管理 Java 虛擬機以及 Java 虛擬機在其上運行的操做系統。它同時容許從本地和遠程對正在運行的 Java 虛擬機進行監視和管理。java

  2. 主要接口:jvm

    ClassLoadingMXBean  Java虛擬機的類加載系統。spa

    CompilationMXBean  Java虛擬機的編譯系統。操作系統

    MemoryMXBean  Java虛擬機的內存系統。線程

    ThreadMXBean  Java虛擬機的線程系統。代理

    RuntimeMXBean  Java虛擬機的運行時系統。code

    OperatingSystemMXBean  Java虛擬機在其上運行的操做系統。orm

    GarbageCollectorMXBean  Java虛擬機中的垃圾回收器。htm

    MemoryManagerMXBean  Java虛擬機中的內存管理器。接口

    MemoryPoolMXBean   Java虛擬機中的的內存池。 

  3. 代碼示例:

package management;
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<GarbageCollectorMXBean> 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<MemoryManagerMXBean> 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<MemoryPoolMXBean> 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();
//        }

    }

}

4.控制檯打印信息:

jvm name:Java HotSpot(TM) 64-Bit Server VM
jvm version:24.75-b04
jvm bootClassPath:C:\Program Files\Java\jdk1.7.0_75\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_75\jre\classes
jvm start time:1439797054990
Heap committed:127926272 init:132884352 max:1890582528 used:2045136
Architecture: amd64
Processors: 2
System name: Windows 7
System version: 6.1
Last minute load: -1.0
TotalLoadedClassCount: 443
LoadedClassCount443
UnloadedClassCount:0
TotalCompilationTime:3
name:HotSpot 64-Bit Tiered Compilers
ThreadCount5
AllThreadIds:[J@2b51574b
CurrentThreadUserTime78000500
name:PS Scavenge
CollectionCount:0
CollectionTime0
name:PS MarkSweep
CollectionCount:0
CollectionTime0
name:CodeCacheManager
MemoryPoolNames:[Ljava.lang.String;@397dea61
name:PS Scavenge
MemoryPoolNames:[Ljava.lang.String;@79a6a3c3
name:PS MarkSweep
MemoryPoolNames:[Ljava.lang.String;@2b49959a
name:Code Cache
CollectionUsage:null
type:Non-heap memory
name:PS Eden Space
CollectionUsage:init = 34078720(33280K) used = 0(0K) committed = 0(0K) max = 698351616(681984K)
type:Heap memory
name:PS Survivor Space
CollectionUsage:init = 5242880(5120K) used = 0(0K) committed = 0(0K) max = 5242880(5120K)
type:Heap memory
name:PS Old Gen
CollectionUsage:init = 88604672(86528K) used = 0(0K) committed = 0(0K) max = 1417674752(1384448K)
type:Heap memory
name:PS Perm Gen
CollectionUsage:init = 22020096(21504K) used = 0(0K) committed = 0(0K) max = 85983232(83968K)
type:Non-heap memory
相關文章
相關標籤/搜索