netty堆外内存监控

  • 直接new DirectMemReporter().startReport();调用就可以了
  • netty4.1x默认使用池化的bytebuf,每个池子初始16mb,使用直接内存的最大池子数默认16,由jvm启动参数决定-Dio.netty.allocator.numDirectArenas。默认最大堆外内存默认3817865216 bytes,由-Dio.netty.maxDirectMemory决定

    @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);
        }
    }
    

猜你喜欢

转载自blog.csdn.net/liyantianmin/article/details/86380859