2017년 2월 4일 토요일

GC Monitoring

[GarbageCollectorMXBean api 사용]

        for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) {

            // 마지막 gc 정보
            System.out.println("gc count : " + gcBean.getCollectionCount());

            com.sun.management.GarbageCollectorMXBean sunGcBean = (com.sun.management.GarbageCollectorMXBean) gcBean;

            // 마지막 gc duration time
            System.out.println(sunGcBean.getName() + "last gc time : " +  sunGcBean.getLastGcInfo().getDuration());

            // 마지막 gc duration time = getLastGcInfo().getDuration()
            System.out.println(sunGcBean.getLastGcInfo().getEndTime() - sunGcBean.getLastGcInfo().getStartTime());

            // 마지막 gc 실행 정보
            System.out.println(sunGcBean.getLastGcInfo());
        }

[jmx api 사용]

        try { 
            MBeanServerConnection server = ManagementFactory.getPlatformMBeanServer();
            ObjectName name = new ObjectName("java.lang:type=GarbageCollector,name=PS MarkSweep");

            MBeanInfo info = null; 
            info = this.server.getMBeanInfo(objectName);
            List names = Arrays.stream(Optional.ofNullable(info).map(MBeanInfo::getAttributes).orElse(new MBeanAttributeInfo[]{null})).map(MBeanAttributeInfo::getName).collect(toList());

            names.forEach(n -> System.out.println(n + " : " + server.getAttribute(name, n)));
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        }

[ result ]
PS Scavenge - young gc
PS MarkSweep - full gc


※ 정리 필요함

댓글 없음:

댓글 쓰기

Intelij 설정 및 plugin

1. preferences... (settings...) Appearance & Behavior > Appearance - Window Options        ✓   Show memory indicator Editor &...