Skip to content

Commit

Permalink
Use netty API instead of reflecting to get used direct memory (apache…
Browse files Browse the repository at this point in the history
  • Loading branch information
Demogorgon314 authored Mar 30, 2022
1 parent fe9bb39 commit b74e65c
Showing 1 changed file with 6 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.lang.management.BufferPoolMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -37,7 +36,6 @@
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -48,19 +46,11 @@
public class JvmMetrics {

private static final Logger log = LoggerFactory.getLogger(JvmMetrics.class);
private static Field directMemoryUsage = null;
private final JvmGCMetricsLogger gcLogger;

private final String componentName;
private static final Map<String, Class<? extends JvmGCMetricsLogger>> gcLoggerMap = new HashMap<>();
static {
try {
directMemoryUsage = io.netty.util.internal.PlatformDependent.class
.getDeclaredField("DIRECT_MEMORY_COUNTER");
directMemoryUsage.setAccessible(true);
} catch (Exception e) {
log.warn("Failed to access netty DIRECT_MEMORY_COUNTER field {}", e.getMessage());
}
// GC type and implementation mapping
gcLoggerMap.put("-XX:+UseG1GC", JvmG1GCMetricsLogger.class);
}
Expand Down Expand Up @@ -143,14 +133,12 @@ public List<Metrics> generate() {
}

public static long getJvmDirectMemoryUsed() {
if (directMemoryUsage != null) {
try {
return ((AtomicLong) directMemoryUsage.get(null)).get();
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug("Failed to get netty-direct-memory used count {}", e.getMessage());
}
}
long usedDirectMemory = io.netty.util.internal.PlatformDependent.usedDirectMemory();
if (usedDirectMemory != -1L) {
return usedDirectMemory;
}
if (log.isDebugEnabled()) {
log.debug("Failed to get netty-direct-memory used count.");
}

List<BufferPoolMXBean> pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
Expand Down

0 comments on commit b74e65c

Please sign in to comment.