代码之家  ›  专栏  ›  技术社区  ›  uttam

使用DropWizard度量Jvm获取JMX值

  •  2
  • uttam  · 技术社区  · 9 年前

    我尝试使用dropWizard库获取JVM度量值(MemoryUsageGaugeSet、GarbageCollectionUsageGauge Set)

     <dependency>
        <groupId>io.dropwizard.metrics</groupId>
        <artifactId>metrics-jvm</artifactId>
        <version>3.1.2</version>
     </dependency>
    

    代码实现。

      public static void main(String[] args ){
      MemoryUsageGaugeSet memory = new MemoryUsageGaugeSet();
      Map<String, Metric> memoryMap = memory.getMetrics();
      system.out.println( memoryMap);}
    

    我得到的结果是

    {heap.committed=com.codahal.metrics.jvm.MemoryUsageGaugeSet$8@d16011,non-heap.used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$11@cc09904,pools.Code Cache.used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$17@748032cc,pools.Code Cache.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$18@2b6e0423,heap.used=com.codahal.metrics.jvm.MemoryUsageGaugeSet$6@203428b7,pools.PS-Old-Gen.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$19@28b49d0c,total.committed=com.codahal.metrics.jvm.MemoryUsageGaugeSet$4@5dd7cc84,pools.PS-Old-Gen.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$16@6438401,total.init=com.codahal.metrics.jvm.MemoryUsageGaugeSet$1@7b365ee,非堆.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$12@be3d4e9,pools.PS Eden Space.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$19@68339afd,池。压缩类空间。used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$17@724a6876,pools.Metaspace.use=com.codahale.metrics.jvm.MemoryUsageGaugeSet$15@6ecaffc4,pools.Metaspace.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$16@7b1b3a68,pools.PS Eden Space.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$18@f8ba384,pools.PS Eden Space.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$16@2e2373db,pools.压缩类空间.用法=com.codahale.metrics.jvm.MemoryUsageGaugeSet$15@17ae8d04,heap.usage=com.codahale.metrics.jvm.MemoryUsageGaugeSet$9@35c43de3,pools.PS Eden Space.use=com.codahale.metrics.jvm.MemoryUsageGaugeSet$15@67013f5c,pools.PS Survivor Space.max=com.codahal.metrics.jvm.MemoryUsageGaugeSet$16@520935cc,pools.Code Cache.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$16@31ac5424,pools.压缩类空间.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$19@25135cd7,pools.Metaspace.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$19@4b066cf6,pools.PS Survivor Space.use=com.codahale.metrics.jvm.MemoryUsageGaugeSet$15@50ba970f,pools.PS-Old-Gen.usage=com.codahale.metrics.jvm.MemoryUsageGaugeSet$15@3e4e448,pools.Code Cache.use=com.codahale.metrics.jvm.MemoryUsageGaugeSet$15@3c2ef507,pools.PS Survivor Space.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$19@44374fa8,total.used=com.codahal.metrics.jvm.MemoryUsageGaugeSet$2@40b1aace,pools.Code Cache.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$19@6453d034,pools.PS-Old-Gen.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$18@2f9e8774,heap.init=com.codahal.metrics.jvm.MemoryUsageGaugeSet$5@512ad720,non-heap.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$13@1c80e011,pools.Compressed Class Space.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$18@662841c5,non-heap.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$10@3dd6cf53,pools.PS Eden Space.used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$17@3a428c1e,非堆。用法=com.codahale.metrics.jvm.MemoryUsageGaugeSet$14@21d9632f,heap.max=com.codahal.metrics.jvm.MemoryUsageGaugeSet$7@5db28b8,pools.PS Survivor Space.used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$17@efcd307,pools.Metaspace.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$18@20f09cba,total.max=com.codahal.metrics.jvm.MemoryUsageGaugeSet$3@4ada5390,pools.Metaspace.used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$17@16982c9b,pools.压缩类空间.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$16@7e94b1a4,pools.PS Survivor Space.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$18@5aa5f780,pools.PS-Old-Gen.used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$17@427b7997}

    我得到了对象的引用,而我想得到这些引用的值。该怎么办?

    1 回复  |  直到 9 年前
        1
  •  2
  •   Milli    9 年前

    要获取/打印对象的值,您需要 toString() 在类中实现,并且 MemoryUsageGaugeSet 没有。 验证@ javadoc

    一种方法是用自己的实现扩展类 到字符串() .

    另一种方法是遍历地图并打印每个值。你可能想试试下面的方法。。

    Map<String, Metric> memoryMap = memory.getMetrics();
    
    for (String key : memoryMap.keySet()) {
        Metric m = memoryMap.get(key);
        System.out.println(m.getClass());
    
        if (m instanceof Gauge) {
            System.out.println(((Gauge<Long>) m).getValue());
        }
    }
    

    内存使用量表集 结果 Gauge<Long> 这是 Metric .

    希望这有帮助。