我尝试使用dropWizard库获取JVM指标值(MemoryUsageGaugeSet,GarbageCollectionUsageGaugeSet)
<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.codahale.metrics.jvm.MemoryUsageGaugeSet$8@d16011,nonheap.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,dup.used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$6@203428b7,pool .PS-Old-Gen.init = com.codahale.metrics.jvm.MemoryUsageGaugeSet $ 19 @ 28b49d0c,total.committed = com.codahale.metrics.jvm.MemoryUsageGaugeSet $ 4 @5dd7cc84,pools.PS-Old-Gen.max = com .codahale.metrics.jvm.MemoryUsageGaugeSet $ 16 @ 6438401,total.init = com.codahale.metrics.jvm.MemoryUsageGaugeSet $ 1 @ 7b365ee,nonheap.max = com.codahale.metrics.jvm.MemoryUsageGaugeSet $ 12@be3d4e9,pool。 PS-Eden-Space.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$19@68339afd,pools.Compressed-Class-Space.used=com.codahale.metrics.jvm.MemoryUsageGaugeSet$17@724a6876,pools.Metaspace.usage = com.codahale.metrics.jvm.MemoryUsageGaugeSet$15@6ecaffc4, pools.Metaspace.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$16@7b1b3a68,poolrs.PS-Eden-Space.committed=com.codahale.metrics.jvm.MemoryUsageGaugeSet$18@f8ba384,poolrs.PS-Eden-Space。 max = com.codahale.metrics.jvm.MemoryUsageGaugeSet$16@2e2373db,pools.Compressed-Class-Space.usage=com.codahale.metrics.jvm.MemoryUsageGaugeSet$15@17ae8d04,dup.usage = com.codahale.metrics.jvm。 MemoryUsageGaugeSet $ 9 @ 35c43de3,pools.PS-Eden-Space.usage=com.codahale.metrics.jvm.MemoryUsageGaugeSet$15@67013f5c,pools.PS-Survivor-Space.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$16@520935cc ,pools.Code-Cache.max=com.codahale.metrics.jvm.MemoryUsageGaugeSet$16@31ac5424,pools.Compressed-Class-Space.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$19@25135cd7,pools.Metaspace.init =com.codahale.metrics.jvm.MemoryUsageGaugeSet$19@4b066cf6,pools.PS-Survivor-Space.usage=com.codahale.metrics.jvm.MemoryUsageGaugeSet$15@50ba970f,pools.PS-Old-Gen.usage = com.codahale .metrics.jvm.MemoryUsageGaugeSet $ 15 @ 3e4e448,pool s.Code-Cache.usage=com.codahale.metrics.jvm.MemoryUsageGaugeSet$15@3c2ef507,poolrs.PS-Survivor-Space.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$19@44374fa8,total.used = com。 codahale.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.codahale.metrics.jvm.MemoryUsageGaugeSet $ 5 @ 512127,nonheap.committed = com.codahale.metrics.jvm.MemoryUsageGaugeSet $ 13@1c80e011,pools.Compressed-Class-Space.committed =com.codahale.metrics.jvm.MemoryUsageGaugeSet$18@662841c5,nonheap.init=com.codahale.metrics.jvm.MemoryUsageGaugeSet$10@3dd6cf53,pools.PS-Eden-Space.used = com.codahale.metrics.jvm .MemoryUsageGaugeSet $ 17 @ 3a428c1e,nonheap.usage = com.codahale.metrics.jvm.MemoryUsageGaugeSet $ 14 @ 21d9632f,heap.max = com.codahale.metrics.jvm.MemoryUsageGaugeSet $ 7 @5db28b8,pools.PS-Survivor-Space。二手= com.codahale.metrics.jvm.MemoryUsageGaug eSet $ 17 @ efcd307,pools.Metaspace.committed = com.codahale.metrics.jvm.MemoryUsageGaugeSet $ 18 @ 20f09cba,total.max = com.codahale.metrics.jvm.MemoryUsageGaugeSet $ 3 @ 4ada5390,pools.Metaspace.used = com.codahale .metrics.jvm.MemoryUsageGaugeSet $ 17 @ 16982c9b,pools.Compressed-Class-Space.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}
我得到了对象的引用,而不是我想获取这些引用的值。该怎么做?
答案 0 :(得分:1)
要获取/打印对象的值,您需要在课程中实施toString()
而MemoryUsageGaugeSet
没有。
验证@ javadoc
一种方法是使用您自己的实现toString()
扩展该类。
另一种方法是迭代地图并打印每个值。你可能想尝试下面的东西..
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());
}
}
MemoryUsageGaugeSet
会产生Gauge<Long>
Metric
的实现。
希望这有帮助。