- 直接
new DirectMemReporter().startReport();
调用就可以了
@Slf4j
public class DirectMemReporter {
private AtomicLong directMem = new AtomicLong();
private ScheduledExecutorService executor = MoreExecutors.getExitingScheduledExecutorService(
new ScheduledThreadPoolExecutor(1), 10, TimeUnit.SECONDS);
public DirectMemReporter() {
Field field = ReflectionUtils.findField(PlatformDependent.class, "DIRECT_MEMORY_COUNTER");
field.setAccessible(true);
try {
directMem = (AtomicLong) field.get(PlatformDependent.class);
} catch (IllegalAccessException e) {}
}
public void startReport() {
executor.scheduleAtFixedRate(() -> {
log.info("netty direct memory size:{}b, max:{}", directMem.get(), PlatformDependent.maxDirectMemory());
}, 0, 1, TimeUnit.SECONDS);
}
}