Java Code Examples for java.lang.management.GarbageCollectorMXBean#getCollectionCount()

The following examples show how to use java.lang.management.GarbageCollectorMXBean#getCollectionCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RuntimeInfoUtils.java    From linden with Apache License 2.0 6 votes vote down vote up
public static JVMInfo getJVMInfo() {
  JVMInfo jvmInfo = new JVMInfo();

  Runtime runtime = Runtime.getRuntime();
  jvmInfo.setFreeMemory(runtime.freeMemory());
  jvmInfo.setTotalMemory(runtime.totalMemory());
  jvmInfo.setMaxMemory(runtime.maxMemory());

  int gcCount = 0;
  long gcTime = 0;
  for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
    long count = gc.getCollectionCount();
    if (count >= 0) {
      gcCount += count;
    }
    long time = gc.getCollectionTime();
    if (time >= 0) {
      gcTime += time;
    }
  }
  jvmInfo.setGcCount(gcCount);
  jvmInfo.setGcTime(gcTime);
  List<String> args = ManagementFactory.getRuntimeMXBean().getInputArguments();
  jvmInfo.setArguments(joiner.join(args));
  return jvmInfo;
}
 
Example 2
Source File: TestCMSClassUnloadingEnabledHWM.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String [] args) throws Exception {
  if (args.length != 1) {
    throw new IllegalArgumentException("Usage: <MetaspaceSize>");
  }

  WhiteBox wb = WhiteBox.getWhiteBox();

  // Allocate past the MetaspaceSize limit.
  long metaspaceSize = Long.parseLong(args[0]);
  long allocationBeyondMetaspaceSize  = metaspaceSize * 2;
  long metaspace = wb.allocateMetaspace(null, allocationBeyondMetaspaceSize);

  // Wait for at least one GC to occur. The caller will parse the log files produced.
  GarbageCollectorMXBean cmsGCBean = getCMSGCBean();
  while (cmsGCBean.getCollectionCount() == 0) {
    Thread.sleep(100);
  }

  wb.freeMetaspace(null, metaspace, metaspace);
}
 
Example 3
Source File: MemoryStats.java    From Cubert with Apache License 2.0 6 votes vote down vote up
public static void printGCStats()
{
    long totalGarbageCollections = 0;
    long garbageCollectionTime = 0;

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

        long count = gc.getCollectionCount();

        if(count >= 0)
        {
            totalGarbageCollections += count;
        }

        long time = gc.getCollectionTime();

        if(time >= 0)
        {
            garbageCollectionTime += time;
        }
    }

    print.f("MemoryStats: #GC calls: %d Total GC Time: %d ms", totalGarbageCollections, garbageCollectionTime);
}
 
Example 4
Source File: GCMetrics.java    From watcher with Apache License 2.0 6 votes vote down vote up
public Object doMonitor(Map<String, Object> params) {
	Map<String, Object> map = Maps.newHashMap();
	List<GarbageCollectorMXBean> mxBeans = ManagementFactory.getGarbageCollectorMXBeans();
	if (mxBeans == null || mxBeans.isEmpty()) {
		return map;
	}
	long totalTime = 0;
	long totalCount = 0;
	for (GarbageCollectorMXBean mxBean : mxBeans) {
		final String name = WHITESPACE.matcher(mxBean.getName()).replaceAll("-");
		long time = mxBean.getCollectionTime();
		long count = mxBean.getCollectionCount();
		totalCount += count;
		totalTime += time;
		map.put(name + ".count", mxBean.getCollectionCount());
		map.put(name + ".time", mxBean.getCollectionTime());
	}
	map.put("totalTime", totalTime);
	map.put("totalCount", totalCount);
	return map;
}
 
Example 5
Source File: JvmMetrics.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private void getGcUsage(MetricsRecordBuilder rb) {
  long count = 0;
  long timeMillis = 0;
  for (GarbageCollectorMXBean gcBean : gcBeans) {
    long c = gcBean.getCollectionCount();
    long t = gcBean.getCollectionTime();
    MetricsInfo[] gcInfo = getGcInfo(gcBean.getName());
    rb.addCounter(gcInfo[0], c).addCounter(gcInfo[1], t);
    count += c;
    timeMillis += t;
  }
  rb.addCounter(GcCount, count)
    .addCounter(GcTimeMillis, timeMillis);
  
  if (pauseMonitor != null) {
    rb.addCounter(GcNumWarnThresholdExceeded,
        pauseMonitor.getNumGcWarnThreadholdExceeded());
    rb.addCounter(GcNumInfoThresholdExceeded,
        pauseMonitor.getNumGcInfoThresholdExceeded());
    rb.addCounter(GcTotalExtraSleepTime,
        pauseMonitor.getTotalGcExtraSleepTime());
  }
}
 
Example 6
Source File: Statistics.java    From systemds with Apache License 2.0 5 votes vote down vote up
public static long getJVMgcCount(){
	long ret = 0; 
	
	List<GarbageCollectorMXBean> gcxs = ManagementFactory.getGarbageCollectorMXBeans();
	
	for( GarbageCollectorMXBean gcx : gcxs )
		ret += gcx.getCollectionCount();
	if( ret>0 )
		ret += jvmGCCount;
	
	return ret;
}
 
Example 7
Source File: GarbageCollectorMetric.java    From neural with MIT License 5 votes vote down vote up
/**
 * ps_scavenge.count:新生代PS(并行扫描)次数
 * ps_scavenge.time:单位:秒,新生代PS(并行扫描)时间
 * ps_marksweep.count:老年代CMS(并行标记清扫)次数
 * ps_marksweep_time:单位:秒,老年代CMS(并行标记清扫)时间
 * <p>
 * ps_scavenge_diff_count:新生代PS(并行扫描)变化次数
 * ps_scavenge_diff_time: 单位:秒,新生代PS(并行扫描)变化时间
 * ps_marksweep_diff_count: 老年代CMS(并行标记清扫)变化次数
 * ps_marksweep_diff_time: 单位:秒,老年代CMS(并行标记清扫)变化时间
 */
public Map<String, Double> getData() {
    final Map<String, Double> gauges = new LinkedHashMap<String, Double>();

    for (final GarbageCollectorMXBean gc : garbageCollectors) {
        final String name = "gc_" + WHITESPACE.matcher(gc.getName()).replaceAll("_").toLowerCase();

        String lastCountKey = name + "_diff_count";
        Object lastCountVal = lastMetric.get(lastCountKey);
        lastCountVal = (lastCountVal == null) ? 0 : lastCountVal;
        long lastCountCurrent = gc.getCollectionCount();
        long lastCountKv = lastCountCurrent - Long.valueOf(lastCountVal + "");
        lastMetric.put(lastCountKey, lastCountCurrent);
        gauges.put(lastCountKey, (double) lastCountKv);

        String lastTimeKey = name + "_diff_time";
        Object lastTimeVal = lastMetric.get(lastTimeKey);
        lastTimeVal = (lastTimeVal == null) ? 0 : lastTimeVal;
        Double lastTimeCurrent = (double) gc.getCollectionTime();
        double lastTimeKv = lastTimeCurrent - Double.valueOf(lastTimeVal + "");
        lastMetric.put(lastTimeKey, lastTimeCurrent);
        gauges.put(lastTimeKey, Double.valueOf(String.format("%.3f", lastTimeKv / 1000)));

        gauges.put(name + "_count", (double) lastCountCurrent);
        // 单位:从毫秒转换为秒
        gauges.put(name + "_time", Double.valueOf(String.format("%.3f", lastTimeCurrent / 1000)));
    }

    return gauges;
}
 
Example 8
Source File: TestMetaspacePerfCounters.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static long currentGCCount() {
    long gcCount = 0;
    for (GarbageCollectorMXBean bean : gcBeans) {
        gcCount += bean.getCollectionCount();
    }
    return gcCount;
}
 
Example 9
Source File: GarbageCollectionMetrics.java    From vespa with Apache License 2.0 5 votes vote down vote up
private void collectGcStatistics(Instant now) {
    for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) {
        String gcName = gcBean.getName().replace(" ", "");
        GcStats stats = new GcStats(now, gcBean.getCollectionCount(), Duration.ofMillis(gcBean.getCollectionTime()));

        LinkedList<GcStats> window = gcStatistics.computeIfAbsent(gcName, anyName -> new LinkedList<>());
        window.addLast(stats);
    }
}
 
Example 10
Source File: $.java    From pysonar2 with Apache License 2.0 5 votes vote down vote up
public static String getGCStats() {
    long totalGC = 0;
    long gcTime = 0;

    for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
        long count = gc.getCollectionCount();

        if (count >= 0) {
            totalGC += count;
        }

        long time = gc.getCollectionTime();

        if (time >= 0) {
            gcTime += time;
        }
    }

    StringBuilder sb = new StringBuilder();

    sb.append(banner("memory stats"));
    sb.append("\n- total collections: " + totalGC);
    sb.append("\n- total collection time: " + formatTime(gcTime));

    Runtime runtime = Runtime.getRuntime();
    sb.append("\n- total memory: " + $.printMem(runtime.totalMemory()));

    return sb.toString();
}
 
Example 11
Source File: MemoryProfiler.java    From NNAnalytics with Apache License 2.0 5 votes vote down vote up
private static long countGc(final List<GarbageCollectorMXBean> gcBeans) {
  long gcCount = 0;
  for (GarbageCollectorMXBean bean : gcBeans) {
    gcCount += bean.getCollectionCount();
  }
  return gcCount;
}
 
Example 12
Source File: Classifier.java    From SFA with GNU General Public License v3.0 5 votes vote down vote up
private long getGcCount() {
  long sum = 0;
  for (GarbageCollectorMXBean b : ManagementFactory.getGarbageCollectorMXBeans()) {
    long count = b.getCollectionCount();
    if (count != -1) {
      sum += count;
    }
  }
  return sum;
}
 
Example 13
Source File: GCState.java    From jstorm with Apache License 2.0 5 votes vote down vote up
public static long getOldGenCollectionCount(){
    long fullGCCounts = 0L;
    try {
        List<GarbageCollectorMXBean> list = ManagementFactory.getGarbageCollectorMXBeans();
        for (GarbageCollectorMXBean gmx : list) {
            if(OldGenCollectorNames.contains(gmx.getName())){
                fullGCCounts += gmx.getCollectionCount();
            }
        }
    }catch (Throwable e){
        LOG.error("error !!!", e);
    }
    return fullGCCounts;
}
 
Example 14
Source File: FlowController.java    From nifi with Apache License 2.0 5 votes vote down vote up
public List<GarbageCollectionStatus> getGarbageCollectionStatus() {
    final List<GarbageCollectionStatus> statuses = new ArrayList<>();

    final Date now = new Date();
    for (final GarbageCollectorMXBean mbean : ManagementFactory.getGarbageCollectorMXBeans()) {
        final String managerName = mbean.getName();
        final long count = mbean.getCollectionCount();
        final long millis = mbean.getCollectionTime();
        final GarbageCollectionStatus status = new StandardGarbageCollectionStatus(managerName, now, count, millis);
        statuses.add(status);
    }

    return statuses;
}
 
Example 15
Source File: ServerSchemaTablesServiceImpl.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public GroupScan getGroupScan(VirtualAdapter adapter, Group group) {
    Iterator<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans().iterator();
    return new SimpleVirtualGroupScan<GarbageCollectorMXBean>(group.getAIS(), getName(), collectors) {
        @Override
        protected Object[] createRow(GarbageCollectorMXBean data, int hiddenPk) {
            return new Object[] {
                    data.getName(),
                    data.getCollectionCount(),
                    data.getCollectionTime(),
                    hiddenPk
            };
        }
    };
}
 
Example 16
Source File: JvmPauseMonitor.java    From hadoop with Apache License 2.0 4 votes vote down vote up
private GcTimes(GarbageCollectorMXBean gcBean) {
  gcCount = gcBean.getCollectionCount();
  gcTimeMillis = gcBean.getCollectionTime();
}
 
Example 17
Source File: VMInfo.java    From DataLink with Apache License 2.0 4 votes vote down vote up
public synchronized void getDelta(boolean print) {

        try {
            if (VMInfo.isSunOsMBean(osMXBean)) {
                long curUptime = runtimeMXBean.getUptime();
                long curProcessTime = getLongFromOperatingSystem(osMXBean, "getProcessCpuTime");
                //百分比, uptime是ms,processTime是nano
                if ((curUptime > lastUpTime) && (curProcessTime >= lastProcessCpuTime)) {
                    float curDeltaCpu = (float) (curProcessTime - lastProcessCpuTime) / ((curUptime - lastUpTime) * totalProcessorCount * 10000);
                    processCpuStatus.setMaxMinCpu(curDeltaCpu);
                    processCpuStatus.averageCpu = (float) curProcessTime / (curUptime * totalProcessorCount * 10000);

                    lastUpTime = curUptime;
                    lastProcessCpuTime = curProcessTime;
                }
            }

            for (GarbageCollectorMXBean garbage : garbageCollectorMXBeanList) {

                GCStatus gcStatus = processGCStatus.gcStatusMap.get(garbage.getName());
                if (gcStatus == null) {
                    gcStatus = new GCStatus();
                    gcStatus.name = garbage.getName();
                    processGCStatus.gcStatusMap.put(garbage.getName(), gcStatus);
                }

                long curTotalGcCount = garbage.getCollectionCount();
                gcStatus.setCurTotalGcCount(curTotalGcCount);

                long curtotalGcTime = garbage.getCollectionTime();
                gcStatus.setCurTotalGcTime(curtotalGcTime);
            }

            if (memoryPoolMXBeanList != null && !memoryPoolMXBeanList.isEmpty()) {
                for (MemoryPoolMXBean pool : memoryPoolMXBeanList) {

                    MemoryStatus memoryStatus = processMomoryStatus.memoryStatusMap.get(pool.getName());
                    if (memoryStatus == null) {
                        memoryStatus = new MemoryStatus();
                        memoryStatus.name = pool.getName();
                        processMomoryStatus.memoryStatusMap.put(pool.getName(), memoryStatus);
                    }
                    memoryStatus.commitedSize = pool.getUsage().getCommitted();
                    memoryStatus.setMaxMinUsedSize(pool.getUsage().getUsed());
                    long maxMemory = memoryStatus.commitedSize > 0 ? memoryStatus.commitedSize : memoryStatus.maxSize;
                    memoryStatus.setMaxMinPercent(maxMemory > 0 ? (float) 100 * memoryStatus.usedSize / maxMemory : -1);
                }
            }

            if (print) {
                LOG.info(processCpuStatus.getDeltaString() + processMomoryStatus.getDeltaString() + processGCStatus.getDeltaString());
            }

        } catch (Exception e) {
            LOG.warn("no need care, the fail is ignored : vmInfo getDelta failed " + e.getMessage(), e);
        }
    }
 
Example 18
Source File: BrokerAttributeInjector.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
public static long getGCCollectionCount(Broker<?> broker, GarbageCollectorMXBean garbageCollectorMXBean)
{
    return garbageCollectorMXBean.getCollectionCount();
}
 
Example 19
Source File: JvmPauseMonitor.java    From tajo with Apache License 2.0 4 votes vote down vote up
private GcTimes(GarbageCollectorMXBean gcBean) {
  gcCount = gcBean.getCollectionCount();
  gcTimeMillis = gcBean.getCollectionTime();
}
 
Example 20
Source File: GarbageCollectorStatistics.java    From spark with GNU General Public License v3.0 4 votes vote down vote up
public GarbageCollectorStatistics(GarbageCollectorMXBean bean) {
    this(bean.getCollectionCount(), bean.getCollectionTime());
}