Skip to content

Commit

Permalink
[JVM] Fix maximum/total memory calculation (#3125)
Browse files Browse the repository at this point in the history
Fixes #2609
  • Loading branch information
the-thing committed Feb 12, 2023
1 parent a9868ff commit f5a6c5c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Expand Up @@ -47,8 +47,8 @@ public Map<String, Metric> getMetrics() {
mxBean.getNonHeapMemoryUsage().getInit());
gauges.put("total.used", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getUsed() +
mxBean.getNonHeapMemoryUsage().getUsed());
gauges.put("total.max", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getMax() +
mxBean.getNonHeapMemoryUsage().getMax());
gauges.put("total.max", (Gauge<Long>) () -> mxBean.getNonHeapMemoryUsage().getMax() == -1 ?
-1 : mxBean.getHeapMemoryUsage().getMax() + mxBean.getNonHeapMemoryUsage().getMax());
gauges.put("total.committed", (Gauge<Long>) () -> mxBean.getHeapMemoryUsage().getCommitted() +
mxBean.getNonHeapMemoryUsage().getCommitted());

Expand All @@ -72,7 +72,7 @@ protected Ratio getRatio() {
@Override
protected Ratio getRatio() {
final MemoryUsage usage = mxBean.getNonHeapMemoryUsage();
return Ratio.of(usage.getUsed(), usage.getMax());
return Ratio.of(usage.getUsed(), usage.getMax() == -1 ? usage.getCommitted() : usage.getMax());
}
});

Expand Down
Expand Up @@ -130,6 +130,16 @@ public void hasAGaugeForTotalMax() {
.isEqualTo(44L);
}

@Test
public void hasAGaugeForTotalMaxWhenNonHeapMaxUndefined() {
when(nonHeap.getMax()).thenReturn(-1L);

final Gauge gauge = (Gauge) gauges.getMetrics().get("total.max");

assertThat(gauge.getValue())
.isEqualTo(-1L);
}

@Test
public void hasAGaugeForHeapCommitted() {
final Gauge gauge = (Gauge) gauges.getMetrics().get("heap.committed");
Expand Down Expand Up @@ -210,6 +220,15 @@ public void hasAGaugeForNonHeapUsage() {
.isEqualTo(0.75);
}

@Test
public void hasAGaugeForNonHeapUsageWhenNonHeapMaxUndefined() {
when(nonHeap.getMax()).thenReturn(-1L);
final Gauge gauge = (Gauge) gauges.getMetrics().get("non-heap.usage");

assertThat(gauge.getValue())
.isEqualTo(3.0);
}

@Test
public void hasAGaugeForMemoryPoolUsage() {
final Gauge gauge = (Gauge) gauges.getMetrics().get("pools.Big-Pool.usage");
Expand Down

0 comments on commit f5a6c5c

Please sign in to comment.