0
点赞
收藏
分享

微信扫一扫

netty堆外内存监控

芭芭蘑菇 2021-09-28 阅读 52
码哥netty
  • 直接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);
    }
}
举报

相关推荐

0 条评论