From 018fd705eef05dbbb887f8ce18e923acf9bb9b4d Mon Sep 17 00:00:00 2001 From: Patrick Strawderman Date: Tue, 31 Oct 2023 12:03:19 -0700 Subject: [PATCH] Remove unnecessary use of ConcurrentHashMap in GcLogger (#1089) GcLogger was using a ConcurrentHashMap that was only written to in its constructor; switch to an unmodifiable HashMap. --- .../src/main/java/com/netflix/spectator/gc/GcLogger.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/spectator-ext-gc/src/main/java/com/netflix/spectator/gc/GcLogger.java b/spectator-ext-gc/src/main/java/com/netflix/spectator/gc/GcLogger.java index b3f8637af..35a4b5218 100644 --- a/spectator-ext-gc/src/main/java/com/netflix/spectator/gc/GcLogger.java +++ b/spectator-ext-gc/src/main/java/com/netflix/spectator/gc/GcLogger.java @@ -35,9 +35,10 @@ import java.lang.management.MemoryPoolMXBean; import java.lang.management.MemoryUsage; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.locks.Lock; @@ -88,7 +89,7 @@ public final class GcLogger { private final long jvmStartTime; - private final ConcurrentHashMap> gcLogs = new ConcurrentHashMap<>(); + private final Map> gcLogs; private long youngGenSizeAfter = 0L; @@ -105,10 +106,12 @@ public final class GcLogger { /** Create a new instance. */ public GcLogger() { jvmStartTime = ManagementFactory.getRuntimeMXBean().getStartTime(); + Map> gcLogs = new HashMap<>(); for (GarbageCollectorMXBean mbean : ManagementFactory.getGarbageCollectorMXBeans()) { CircularBuffer buffer = new CircularBuffer<>(BUFFER_SIZE); gcLogs.put(mbean.getName(), buffer); } + this.gcLogs = Collections.unmodifiableMap(gcLogs); for (MemoryPoolMXBean mbean : ManagementFactory.getMemoryPoolMXBeans()) { String poolName = mbean.getName();