【转】java使用java.lang.management监视和管理 Java 虚拟机

原文地址:https://blog.csdn.net/zhongweijian/article/details/7619383

软件包 java.lang.management

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

接口摘要

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

Java 虚拟机线程系统的管理接口。


 
  1. import java.lang.management.ClassLoadingMXBean;

  2. import java.lang.management.CompilationMXBean;

  3. import java.lang.management.GarbageCollectorMXBean;

  4. import java.lang.management.ManagementFactory;

  5. import java.lang.management.MemoryMXBean;

  6. import java.lang.management.MemoryManagerMXBean;

  7. import java.lang.management.MemoryPoolMXBean;

  8. import java.lang.management.MemoryUsage;

  9. import java.lang.management.OperatingSystemMXBean;

  10. import java.lang.management.RuntimeMXBean;

  11. import java.lang.management.ThreadMXBean;

  12. import java.util.List;

  13. import javax.management.MBeanServerConnection;

  14. public class MBeanDemo {

  15. public static void main(String[] args) {

  16. showJvmInfo();

  17. showMemoryInfo();

  18. showSystem();

  19. showClassLoading();

  20. showCompilation();

  21. showThread();

  22. showGarbageCollector();

  23. showMemoryManager();

  24. showMemoryPool();

  25. }

  26. /**

  27. * Java 虚拟机的运行时系统

  28. */

  29. public static void showJvmInfo() {

  30. RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();

  31. String vendor = mxbean.getVmVendor();

  32. System.out.println("jvm name:" + mxbean.getVmName());

  33. System.out.println("jvm version:" + mxbean.getVmVersion());

  34. System.out.println("jvm bootClassPath:" + mxbean.getBootClassPath());

  35. System.out.println("jvm start time:" + mxbean.getStartTime());

  36. }

  37. /**

  38. * Java 虚拟机的内存系统

  39. */

  40. public static void showMemoryInfo() {

  41. MemoryMXBean mem = ManagementFactory.getMemoryMXBean();

  42. MemoryUsage heap = mem.getHeapMemoryUsage();

  43. System.out.println("Heap committed:" + heap.getCommitted() + " init:" + heap.getInit() + " max:"

  44. + heap.getMax() + " used:" + heap.getUsed());

  45. }

  46. /**

  47. * Java 虚拟机在其上运行的操作系统

  48. */

  49. public static void showSystem() {

  50. OperatingSystemMXBean op = ManagementFactory.getOperatingSystemMXBean();

  51. System.out.println("Architecture: " + op.getArch());

  52. System.out.println("Processors: " + op.getAvailableProcessors());

  53. System.out.println("System name: " + op.getName());

  54. System.out.println("System version: " + op.getVersion());

  55. System.out.println("Last minute load: " + op.getSystemLoadAverage());

  56. }

  57. /**

  58. * Java 虚拟机的类加载系统

  59. */

  60. public static void showClassLoading(){

  61. ClassLoadingMXBean cl = ManagementFactory.getClassLoadingMXBean();

  62. System.out.println("TotalLoadedClassCount: " + cl.getTotalLoadedClassCount());

  63. System.out.println("LoadedClassCount" + cl.getLoadedClassCount());

  64. System.out.println("UnloadedClassCount:" + cl.getUnloadedClassCount());

  65. }

  66. /**

  67. * Java 虚拟机的编译系统

  68. */

  69. public static void showCompilation(){

  70. CompilationMXBean com = ManagementFactory.getCompilationMXBean();

  71. System.out.println("TotalCompilationTime:" + com.getTotalCompilationTime());

  72. System.out.println("name:" + com.getName());

  73. }

  74. /**

  75. * Java 虚拟机的线程系统

  76. */

  77. public static void showThread(){

  78. ThreadMXBean thread = ManagementFactory.getThreadMXBean();

  79. System.out.println("ThreadCount" + thread.getThreadCount());

  80. System.out.println("AllThreadIds:" + thread.getAllThreadIds());

  81. System.out.println("CurrentThreadUserTime" + thread.getCurrentThreadUserTime());

  82. //......还有其他很多信息

  83. }

  84. /**

  85. * Java 虚拟机中的垃圾回收器。

  86. */

  87. public static void showGarbageCollector(){

  88. List<GarbageCollectorMXBean> gc = ManagementFactory.getGarbageCollectorMXBeans();

  89. for(GarbageCollectorMXBean GarbageCollectorMXBean : gc){

  90. System.out.println("name:" + GarbageCollectorMXBean.getName());

  91. System.out.println("CollectionCount:" + GarbageCollectorMXBean.getCollectionCount());

  92. System.out.println("CollectionTime" + GarbageCollectorMXBean.getCollectionTime());

  93. }

  94. }

  95. /**

  96. * Java 虚拟机中的内存管理器

  97. */

  98. public static void showMemoryManager(){

  99. List<MemoryManagerMXBean> mm = ManagementFactory.getMemoryManagerMXBeans();

  100. for(MemoryManagerMXBean eachmm: mm){

  101. System.out.println("name:" + eachmm.getName());

  102. System.out.println("MemoryPoolNames:" + eachmm.getMemoryPoolNames().toString());

  103. }

  104. }

  105. /**

  106. * Java 虚拟机中的内存池

  107. */

  108. public static void showMemoryPool(){

  109. List<MemoryPoolMXBean> mps = ManagementFactory.getMemoryPoolMXBeans();

  110. for(MemoryPoolMXBean mp : mps){

  111. System.out.println("name:" + mp.getName());

  112. System.out.println("CollectionUsage:" + mp.getCollectionUsage());

  113. System.out.println("type:" + mp.getType());

  114. }

  115. }

  116. /**

  117. * 访问 MXBean 的方法的三种方法

  118. */

  119. public static void visitMBean(){

  120. //第一种直接调用同一 Java 虚拟机内的 MXBean 中的方法。

  121. RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();

  122. String vendor1 = mxbean.getVmVendor();

  123. System.out.println("vendor1:" + vendor1);

  124. //第二种通过一个连接到正在运行的虚拟机的平台 MBeanServer 的 MBeanServerConnection。

  125. MBeanServerConnection mbs = null;

  126. // Connect to a running JVM (or itself) and get MBeanServerConnection

  127. // that has the JVM MXBeans registered in it

  128. /*

  129. try {

  130. // Assuming the RuntimeMXBean has been registered in mbs

  131. ObjectName oname = new ObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME);

  132. String vendor2 = (String) mbs.getAttribute(oname, "VmVendor");

  133. System.out.println("vendor2:" + vendor2);

  134. } catch (Exception e) {

  135. e.printStackTrace();

  136. }

  137. */

  138. //第三种使用 MXBean 代理

  139. // MBeanServerConnection mbs3 = null;

  140. // RuntimeMXBean proxy;

  141. // try {

  142. // proxy = ManagementFactory.newPlatformMXBeanProxy(mbs3,ManagementFactory.RUNTIME_MXBEAN_NAME,

  143. // RuntimeMXBean.class);

  144. // String vendor = proxy.getVmVendor();

  145. // } catch (IOException e) {

  146. // e.printStackTrace();

  147. // }

  148. }

  149. }

输出:

   
   
   

 
  1. jvm name:Java HotSpot(TM) Client VM

  2. jvm version:1.6.0-b105

  3. jvm 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\classes

  4. jvm start time:1307440032774

  5. Heap committed:5177344 init:0 max:66650112 used:632640

  6. Architecture: x86

  7. Processors: 2

  8. System name: Windows XP

  9. System version: 5.1

  10. Last minute load: -1.0

  11. TotalLoadedClassCount: 381

  12. LoadedClassCount381

  13. UnloadedClassCount:0

  14. TotalCompilationTime:3

  15. name:HotSpot Client Compiler

  16. ThreadCount5

  17. AllThreadIds:[J@47b480

  18. CurrentThreadUserTime15625000

  19. name:Copy

  20. CollectionCount:0

  21. CollectionTime0

  22. name:MarkSweepCompact

  23. CollectionCount:0

  24. CollectionTime0

  25. name:CodeCacheManager

  26. MemoryPoolNames:[Ljava.lang.String;@1389e4

  27. name:Copy

  28. MemoryPoolNames:[Ljava.lang.String;@c20e24

  29. name:MarkSweepCompact

  30. MemoryPoolNames:[Ljava.lang.String;@2e7263

  31. name:Code Cache

  32. CollectionUsage:null

  33. type:Non-heap memory

  34. name:Eden Space

  35. CollectionUsage:init = 917504(896K) used = 0(0K) committed = 0(0K) max = 4194304(4096K)

  36. type:Heap memory

  37. name:Survivor Space

  38. CollectionUsage:init = 65536(64K) used = 0(0K) committed = 0(0K) max = 458752(448K)

  39. type:Heap memory

  40. name:Tenured Gen

  41. CollectionUsage:init = 4194304(4096K) used = 0(0K) committed = 0(0K) max = 61997056(60544K)

  42. type:Heap memory

  43. name:Perm Gen

  44. CollectionUsage:init = 12582912(12288K) used = 0(0K) committed = 0(0K) max = 67108864(65536K)

  45. type:Non-heap memory

  46. name:Perm Gen [shared-ro]

  47. CollectionUsage:init = 8388608(8192K) used = 0(0K) committed = 0(0K) max = 8388608(8192K)

  48. type:Non-heap memory

  49. name:Perm Gen [shared-rw]

  50. CollectionUsage:init = 12582912(12288K) used = 0(0K) committed = 0(0K) max = 12582912(12288K)

  51. type:Non-heap memory

上一篇:select2 javascript控件 如何设置指定的值


下一篇:zkw模板