Skip to content

Commit

Permalink
Minor optimization in MemoryPoolMeter::measure (#993)
Browse files Browse the repository at this point in the history
Change MemoryPoolMeter's measure method to return Collections.emptyList()
if the MemoryPoolMXBean is not present. Additionally, size the ArrayList for the
fixed number of measurements.
  • Loading branch information
kilink committed Sep 15, 2022
1 parent dda0e84 commit 811f9c7
Showing 1 changed file with 14 additions and 11 deletions.
Expand Up @@ -20,6 +20,7 @@
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -46,19 +47,21 @@ class MemoryPoolMeter extends AbstractMeter<MemoryPoolMXBean> {
maxId = registry.createId("jvm.memory.max").withTag("id", mbean.getName());
}

@Override public Iterable<Measurement> measure() {
final long timestamp = clock.wallTime();
@Override
public Iterable<Measurement> measure() {
final MemoryPoolMXBean mbean = ref.get();
final List<Measurement> ms = new ArrayList<>();
if (mbean != null) {
final String typeKey = "memtype";
final String type = mbean.getType().name();

final MemoryUsage usage = mbean.getUsage();
ms.add(new Measurement(usedId.withTag(typeKey, type), timestamp, usage.getUsed()));
ms.add(new Measurement(committedId.withTag(typeKey, type), timestamp, usage.getCommitted()));
ms.add(new Measurement(maxId.withTag(typeKey, type), timestamp, usage.getMax()));
if (mbean == null) {
return Collections.emptyList();
}
final long timestamp = clock.wallTime();
final List<Measurement> ms = new ArrayList<>(3);
final String typeKey = "memtype";
final String type = mbean.getType().name();

final MemoryUsage usage = mbean.getUsage();
ms.add(new Measurement(usedId.withTag(typeKey, type), timestamp, usage.getUsed()));
ms.add(new Measurement(committedId.withTag(typeKey, type), timestamp, usage.getCommitted()));
ms.add(new Measurement(maxId.withTag(typeKey, type), timestamp, usage.getMax()));
return ms;
}
}

0 comments on commit 811f9c7

Please sign in to comment.