【问题标题】:How to get minor and major garbage collection count in JDK 7 and JDK 8如何在 JDK 7 和 JDK 8 中获取次要和主要垃圾收集计数
【发布时间】:2023-04-03 14:09:01
【问题描述】:

我正在尝试发布我的服务的垃圾收集统计信息。很少有人在 JDK 7 上运行,很少有人在 JDK8 上运行。

我在问题中读到:Can you get basic GC stats in Java? 我们可以通过 MBean 获得总垃圾收集计数。但我试图看一个稍微不同的画面。

我正在尝试分别发布主要和次要收藏计数。上述方法将获得总集合计数,而不是分为次要和主要。

我尝试过做什么?

List<GarbageCollectorMXBean> listOfGarbageCollectionMBeans = ManagementFactory.getGarbageCollectorMXBeans();
Stats majorGCStats= null;
Stats minorGcStats = null;
for (GarbageCollectorMXBean gcMBean: listOfGarbageCollectionMBeans ) {
Stats stats = new Stats();
// in the method isMajorCollector, trying to identify if the collector is for major or minor depending on pool name
  if (isMajorCollector(gcMBean.getMemoryPoolNames())) {
    majorGCStats= stats;
  }
  else{
     minorGCStats= stats;
  }
  //Collection_MAP is a HashMap of minorGC vs Stats and majorGC vs Stats
  COLLECTION_MAP.put(stats.getName(), stats);
}

public boolean isMajorCollector(String[] poolNames){
    for (final String pool : pools) {
      if (pool.contains("Perm")|| pool.contains("Old")) {
        return true;
      }
    }
    return false;
   }
}


// An inner class to hold the time and name
class Stats{
 private String name;
 private long time;

 public void setTime(final long time){
   this.time= time;
 }

 public void setName(final String name){
   this.name=name;
 }

 public String getName(){
  return this.name;
 }

 public long getTime(){
   return this.time;
  }
 }

我得到的收藏家是:PS Scavenge 和 PS MarkSweep。

对于 PS Scavenge,池名称为:PS Eden Space、PS Survivor Space。

对于 PS MarkSweep,池名称为:PS Eden Space、PS Survivor Space、PS Old Gen、PS Perm Gen

我正在使用的JVM参数:

-XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+DisableExplicitGC.

没有提到使用cmsinitiatingoccupancyonly和cmsinitiatingoccupancyfraction。

提前致谢。

【问题讨论】:

    标签: java performance garbage-collection instrumentation


    【解决方案1】:
     List<GarbageCollectorMXBean> gcMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
     System.out.println("Minor GC count = " + gcMXBeans.get(0).getCollectionCount());
     System.out.println("Major GC count = " + gcMXBeans.get(1).getCollectionCount());
    

    这适用于所有典型收集器的 JDK 7 和 JDK 8:

    • -XX:+UseConcMarkSweepGC
    • -XX:+UseParallelGC
    • -XX:+UseSerialGC
    • -XX:+使用G1GC

    【讨论】:

    • 非常感谢您对此的回答。这是否意味着 PS Scavenge 适合年轻一代而 PS MarkSweep 适合老一代?另外还有一个奇怪的事情是我经常发生major GC,其时间非常短(大约300-500 micros)。
    • @DipeshGupta 粗略地说是的。名为“PS MarkSweep”的 MXBean 将计算完整集合。要查找 GC 的原因,请使用-XX:+PrintGCDetails
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    相关资源
    最近更新 更多