简体   繁体   中英

A question about ManagementFactory.getGarbageCollectorMXBeans

i want to count the number of fullgc, this is my code

Thread t = new Thread(new Runnable() {
    @Override
    public void run() {
        long fullGcCount = 0;
        for(GarbageCollectorMXBean bean: ManagementFactory.getGarbageCollectorMXBeans()) {
            System.out.println(bean.getName());
            if(bean.getName().equals("ConcurrentMarkSweep")) {
                logger.info("init fullgc count:{}", bean.getCollectionCount());
                while (true) {
                    long temGcCount = bean.getCollectionCount();
                    if(temGcCount == fullGcCount) {
                        continue;
                    }else {
                        System.out.println(String.format("before the FullGC, sum of fullgc:%s, add FullGC count:%s", fullGcCount, temGcCount - fullGcCount));
                        fullGcCount = temGcCount;
                    }
                }
            }
        }
    }
});
t.start();

i run jmap -histo:live 2 times. this is the first gc log:

2023-01-10T19:18:03.496+0800: 80595.099: [Full GC (Heap Inspection Initiated GC) 80595.099: [CMS[YG occupancy: 168131 K (188736 K)]80595.174: [weak refs processing, 0.0015483 secs]80595.176: [class unloading, 0.0253856 secs]80595.201: [scrub symbol table, 0.0237105 secs]80595.225: [scrub string table, 0.0020402 secs]: 112558K->83931K(838912K), 0.2212898 secs] 280689K->252063K(1027648K), [Metaspace: 117424K->117424K(1155072K)], 0.2218481 secs] [Times: user=0.39 sys=0.04, real=0.22 secs] 

this is the second gc log:

2023-01-10T19:18:29.536+0800: 80621.139: [Full GC (Heap Inspection Initiated GC) 80621.139: [CMS: 83931K->85173K(838912K), 0.3054333 secs] 108263K->85173K(1027648K), [Metaspace: 117022K->117022K(1155072K)], 0.3058419 secs] [Times: user=0.29 sys=0.01, real=0.31 secs] 

It looks like one at a time. But my program thinks the first one is 2 fullgc.

......
before the FullGC, sum of fullgc:1, add FullGC count:2
before the FullGC, sum of fullgc:3, add FullGC count:1
......

but i do not know why. please help!

i use the google but not find the answer.

the cmsgc of 'jmap -histo:live' is foreground. in this model, the gccount will add 1 when 'Initial Mark', 'CMS remark' and 'Sweep'. According to the log, the first gc trigger 'CMS remark' and 'sweep', the second gc trigger 'sweep'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM