### Question:
JvmMemoryMetrics extends AbstractLifecycleListener { void bindTo(final MetricRegistry registry) { final MemoryMXBean platformMXBean = ManagementFactory.getPlatformMXBean(MemoryMXBean.class); registry.add("jvm.memory.heap.used", Labels.EMPTY, new DoubleSupplier() { @Override public double get() { return platformMXBean.getHeapMemoryUsage().getUsed(); } }); registry.add("jvm.memory.heap.committed", Labels.EMPTY, new DoubleSupplier() { @Override public double get() { return platformMXBean.getHeapMemoryUsage().getCommitted(); } }); registry.add("jvm.memory.heap.max", Labels.EMPTY, new DoubleSupplier() { @Override public double get() { return platformMXBean.getHeapMemoryUsage().getMax(); } }); registry.add("jvm.memory.non_heap.used", Labels.EMPTY, new DoubleSupplier() { @Override public double get() { return platformMXBean.getNonHeapMemoryUsage().getUsed(); } }); registry.add("jvm.memory.non_heap.committed", Labels.EMPTY, new DoubleSupplier() { @Override public double get() { return platformMXBean.getNonHeapMemoryUsage().getCommitted(); } }); registry.add("jvm.memory.non_heap.max", Labels.EMPTY, new DoubleSupplier() { @Override public double get() { return platformMXBean.getNonHeapMemoryUsage().getMax(); } }); List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans(); for (final MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) { if (memoryPoolMXBean.getType() != MemoryType.HEAP) { continue; } final Labels memoryPoolTags = Labels.Mutable.of("name", memoryPoolMXBean.getName()); try { registry.add("jvm.memory.heap.pool.used", memoryPoolTags, new DoubleSupplier() { @Override public double get() { return memoryPoolMXBean.getUsage().getUsed(); } }); registry.add("jvm.memory.heap.pool.committed", memoryPoolTags, new DoubleSupplier() { @Override public double get() { return memoryPoolMXBean.getUsage().getCommitted(); } }); registry.add("jvm.memory.heap.pool.max", memoryPoolTags, new DoubleSupplier() { @Override public double get() { return memoryPoolMXBean.getUsage().getMax(); } }); } catch (Exception e) { logger.error("Cannot fetch memory metrics of memory pool " + memoryPoolMXBean.getName(), e); } } }  @Override void start(ElasticApmTracer tracer);  }### Answer:
@Test void testMetrics() { final MetricRegistry registry = new MetricRegistry(mock(ReporterConfiguration.class)); jvmMemoryMetrics.bindTo(registry); Stream.of( "heap.used", "heap.committed", "heap.max", "non_heap.used", "non_heap.committed", "non_heap.max") .forEach(s -> assertMetric(registry, "jvm.memory." + s, Labels.EMPTY) .isNotZero()); List<String> memoryPoolNames = getMemoryPoolNames(); for (String memoryPoolName : memoryPoolNames) { final Labels spaceLabel = Labels.Mutable.of("name", memoryPoolName); Stream.of( "used", "committed", "max") .forEach(s -> assertMetric(registry, "jvm.memory.heap.pool." + s, spaceLabel) .isNotNaN() .isGreaterThanOrEqualTo(s.equals("max") ? -1D : 0D)); } }