1. ManagementFactory JMXBean
  2. ---------------- GC
  3. PS-MarkSweep.count
  4. value = 1
  5. PS-MarkSweep.time
  6. value = 13
  7. PS-Scavenge.count
  8. value = 1
  9. PS-Scavenge.time
  10. value = 5
  11. ---------------- 内存
  12. heap.committed
  13. value = 257425408
  14. heap.init
  15. value = 268435456
  16. heap.max
  17. value = 3791650816
  18. heap.usage
  19. value = 0.0033580159719011427
  20. heap.used
  21. value = 12732424
  22. non-heap.committed
  23. value = 15597568
  24. non-heap.init
  25. value = 2555904
  26. non-heap.max
  27. value = -1
  28. non-heap.usage
  29. value = -1.4876568E7
  30. non-heap.used
  31. value = 14876568
  32. pools.Code-Cache.committed
  33. value = 3801088
  34. pools.Code-Cache.init
  35. value = 2555904
  36. pools.Code-Cache.max
  37. value = 251658240
  38. pools.Code-Cache.usage
  39. value = 0.014963785807291666
  40. pools.Code-Cache.used
  41. value = 3765760
  42. pools.Compressed-Class-Space.committed
  43. value = 1310720
  44. pools.Compressed-Class-Space.init
  45. value = 0
  46. pools.Compressed-Class-Space.max
  47. value = 1073741824
  48. pools.Compressed-Class-Space.usage
  49. value = 0.0011090338230133057
  50. pools.Compressed-Class-Space.used
  51. value = 1190816
  52. pools.Metaspace.committed
  53. value = 10485760
  54. pools.Metaspace.init
  55. value = 0
  56. pools.Metaspace.max
  57. value = -1
  58. pools.Metaspace.usage
  59. value = 0.9465187072753907
  60. pools.Metaspace.used
  61. value = 9924968
  62. pools.PS-Eden-Space.committed
  63. value = 67108864
  64. pools.PS-Eden-Space.init
  65. value = 67108864
  66. pools.PS-Eden-Space.max
  67. value = 1399848960
  68. pools.PS-Eden-Space.usage
  69. value = 0.005861632386396887
  70. pools.PS-Eden-Space.used
  71. value = 8205400
  72. pools.PS-Eden-Space.used-after-gc
  73. value = 0
  74. pools.PS-Old-Gen.committed
  75. value = 179306496
  76. pools.PS-Old-Gen.init
  77. value = 179306496
  78. pools.PS-Old-Gen.max
  79. value = 2843738112
  80. pools.PS-Old-Gen.usage
  81. value = 0.0015919271823579232
  82. pools.PS-Old-Gen.used
  83. value = 4527024
  84. pools.PS-Old-Gen.used-after-gc
  85. value = 4527024
  86. pools.PS-Survivor-Space.committed
  87. value = 11010048
  88. pools.PS-Survivor-Space.init
  89. value = 11010048
  90. pools.PS-Survivor-Space.max
  91. value = 11010048
  92. pools.PS-Survivor-Space.usage
  93. value = 0.0
  94. pools.PS-Survivor-Space.used
  95. value = 0
  96. pools.PS-Survivor-Space.used-after-gc
  97. value = 0
  98. total.committed
  99. value = 273022976
  100. total.init
  101. value = 270991360
  102. total.max
  103. value = 3791650815
  104. total.used
  105. value = 27624208
  106. ---------------- 线程
  107. blocked.count
  108. value = 0
  109. count
  110. value = 38
  111. daemon.count
  112. value = 17
  113. deadlock.count
  114. value = 0
  115. deadlocks
  116. value = []
  117. new.count
  118. value = 0
  119. runnable.count
  120. value = 4
  121. terminated.count
  122. value = 0
  123. timed_waiting.count
  124. value = 32
  125. waiting.count
  126. value = 2
  127. ---------------- Class数量
  128. loaded
  129. value = 1737
  130. unloaded
  131. value = 0

代码如下:

  1. <dependency>
  2. <groupId>io.dropwizard.metrics</groupId>
  3. <artifactId>metrics-jvm</artifactId>
  4. <version>4.0.3</version>
  5. </dependency>
  1. package org.test;
  2. import com.codahale.metrics.ConsoleReporter;
  3. import com.codahale.metrics.MetricRegistry;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.junit.Test;
  6. import java.lang.management.ManagementFactory;
  7. import java.lang.management.OperatingSystemMXBean;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Locale;
  11. import java.util.TimeZone;
  12. import java.util.concurrent.TimeUnit;
  13. /**
  14. * 作者: lzw<br/>
  15. * 创建时间:2018-08-22 14:03 <br/>
  16. */
  17. @Slf4j
  18. public class Test01 {
  19. @Test
  20. public void t01() {
  21. Runtime runtime = Runtime.getRuntime();
  22. log.info("======================= {} {}", runtime.freeMemory(), runtime.maxMemory());
  23. }
  24. @Test
  25. public void t02() throws InterruptedException {
  26. MetricRegistry metricRegistry = new MetricRegistry();
  27. ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(metricRegistry)
  28. // 将持续时间转换为给定时间单位
  29. .convertDurationsTo(TimeUnit.MILLISECONDS)
  30. // 将速率转换为给定时间单位
  31. .convertRatesTo(TimeUnit.SECONDS)
  32. // 国际化 - 数字格式
  33. .formattedFor(Locale.SIMPLIFIED_CHINESE)
  34. // 时间时区
  35. .formattedFor(TimeZone.getTimeZone("Asia/Shanghai"))
  36. .build();
  37. consoleReporter.start(1, TimeUnit.SECONDS);
  38. // Timer timer = metricRegistry.timer("executeTime");
  39. // for (int i = 0; i < 10; i++) {
  40. // Timer.Context context = timer.time();
  41. // Thread.sleep(200 * i);
  42. // context.stop();
  43. // }
  44. // GC 活动
  45. // GarbageCollectorMetricSet garbageCollectorMetricSet = new GarbageCollectorMetricSet();
  46. // metricRegistry.register("", garbageCollectorMetricSet);
  47. // gcAndMemory();
  48. // 内存使用
  49. // MemoryUsageGaugeSet memoryUsageGaugeSet = new MemoryUsageGaugeSet();
  50. // metricRegistry.register(null, memoryUsageGaugeSet);
  51. // gcAndMemory();
  52. // 线程状态
  53. // CachedThreadStatesGaugeSet cachedThreadStatesGaugeSet = new CachedThreadStatesGaugeSet(1, TimeUnit.SECONDS);
  54. // metricRegistry.register(null, cachedThreadStatesGaugeSet);
  55. // thread();
  56. // Class加载
  57. // ClassLoadingGaugeSet classLoadingGaugeSet = new ClassLoadingGaugeSet();
  58. // metricRegistry.register(null, classLoadingGaugeSet);
  59. // 文件句柄
  60. // FileDescriptorRatioGauge fileDescriptorRatioGauge =new FileDescriptorRatioGauge();
  61. // metricRegistry.register(null, fileDescriptorRatioGauge);
  62. // NIO 缓冲池
  63. // BufferPoolMetricSet bufferPoolMetricSet = new BufferPoolMetricSet(ManagementFactory.getPlatformMBeanServer());
  64. // metricRegistry.register(null, bufferPoolMetricSet);
  65. // JVM信息
  66. // JvmAttributeGaugeSet jvmAttributeGaugeSet = new JvmAttributeGaugeSet();
  67. // metricRegistry.register(null, jvmAttributeGaugeSet);
  68. Thread.sleep(1000 * 10);
  69. }
  70. @Test
  71. public void t03() throws InterruptedException {
  72. // CPU 信息
  73. com.sun.management.OperatingSystemMXBean sunOperatingSystemMXBean = null;
  74. OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
  75. if (operatingSystemMXBean instanceof com.sun.management.OperatingSystemMXBean) {
  76. sunOperatingSystemMXBean = (com.sun.management.OperatingSystemMXBean) operatingSystemMXBean;
  77. }
  78. if (sunOperatingSystemMXBean != null) {
  79. final com.sun.management.OperatingSystemMXBean tmp = sunOperatingSystemMXBean;
  80. Thread thread = new Thread(() -> {
  81. for (int i = 0; i < 50; i++) {
  82. try {
  83. Thread.sleep(200);
  84. // log.info("ProcessCpuLoad {}\t SystemCpuLoad {}", tmp.getProcessCpuLoad(), tmp.getSystemCpuLoad());
  85. log.info("TotalPhysicalMemorySize {}\t FreePhysicalMemorySize {}", tmp.getTotalPhysicalMemorySize(), tmp.getFreePhysicalMemorySize());
  86. } catch (InterruptedException e) {
  87. e.printStackTrace();
  88. }
  89. }
  90. });
  91. thread.start();
  92. // new Thread(() -> {
  93. // int i = 0;
  94. // while (i < 10000 * 1000) {
  95. // i++;
  96. // }
  97. // }).start();
  98. thread.join();
  99. }
  100. log.info("#---------------------------------------------------------------------------");
  101. }
  102. private void gcAndMemory() throws InterruptedException {
  103. System.gc();
  104. for (int i = 1; i < 20; i++) {
  105. Thread.sleep(1000);
  106. List<String> list = new ArrayList<>(1000 * i);
  107. for (int j = 0; j < 1000 * i; j++) {
  108. list.add("XXXXXXXXXXXX" + j);
  109. }
  110. log.info("{}", list.size());
  111. }
  112. }
  113. private void thread() throws InterruptedException {
  114. for (int i = 1; i < 80; i++) {
  115. final int j = i;
  116. Thread thread = new Thread(() -> {
  117. try {
  118. Thread.sleep(10000 * j);
  119. } catch (InterruptedException e) {
  120. e.printStackTrace();
  121. }
  122. });
  123. if (i % 3 == 0) {
  124. thread.setDaemon(true);
  125. }
  126. thread.start();
  127. Thread.sleep(100);
  128. }
  129. }
  130. }
文档更新时间: 2018-08-24 16:55   作者:lizw