Example usage for java.lang.management GarbageCollectorMXBean getCollectionCount

List of usage examples for java.lang.management GarbageCollectorMXBean getCollectionCount

Introduction

In this page you can find the example usage for java.lang.management GarbageCollectorMXBean getCollectionCount.

Prototype

public long getCollectionCount();

Source Link

Document

Returns the total number of collections that have occurred.

Usage

From source file:org.teiid.sizing.Main.java

public static long collectionCount() {
    long totalGarbageCollections = 0;

    for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
        long count = gc.getCollectionCount();
        if (count >= 0) {
            totalGarbageCollections += count;
        }/* w w  w  .j  av a2  s . c  o  m*/
    }
    return totalGarbageCollections;
}

From source file:gridool.util.system.SystemUtils.java

public static int countGC() {
    int count = 0;
    final List<GarbageCollectorMXBean> gclist = ManagementFactory.getGarbageCollectorMXBeans();
    for (GarbageCollectorMXBean gcmx : gclist) {
        count += gcmx.getCollectionCount();
    }/*from www  .  jav  a2s .  c  om*/
    return count;
}

From source file:org.apache.flink.runtime.metrics.util.MetricUtils.java

private static void instantiateGarbageCollectorMetrics(MetricGroup metrics) {
    List<GarbageCollectorMXBean> garbageCollectors = ManagementFactory.getGarbageCollectorMXBeans();

    for (final GarbageCollectorMXBean garbageCollector : garbageCollectors) {
        MetricGroup gcGroup = metrics.addGroup(garbageCollector.getName());
        gcGroup.gauge("Count", new Gauge<Long>() {
            @Override// w w  w  .j  a va2 s .co m
            public Long getValue() {
                return garbageCollector.getCollectionCount();
            }
        });
        gcGroup.gauge("Time", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return garbageCollector.getCollectionTime();
            }
        });
    }
}

From source file:org.apache.hadoop.metrics.jvm.JvmMetrics.java

private void doGarbageCollectionUpdates() {
    List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans();
    long count = 0;
    long timeMillis = 0;
    for (GarbageCollectorMXBean gcBean : gcBeans) {
        count += gcBean.getCollectionCount();
        timeMillis += gcBean.getCollectionTime();
    }/*from   w w w.jav a2  s .c  o  m*/
    metrics.incrMetric("gcCount", (int) (count - gcCount));
    metrics.incrMetric("gcTimeMillis", (int) (timeMillis - gcTimeMillis));

    gcCount = count;
    gcTimeMillis = timeMillis;
}

From source file:org.apache.hama.monitor.plugin.JvmTask.java

private void gc(final MetricsRecord record) {
    long count = 0;
    long timeMillis = 0;
    for (GarbageCollectorMXBean gcBean : gcBeans) {
        long c = gcBean.getCollectionCount();
        long t = gcBean.getCollectionTime();
        String name = gcBean.getName();
        record.add(new Metric("GcCount" + name, c));
        record.add(new Metric("GcTimeMillis" + name, t));
        count += c;//from ww w.ja  v a2s.  co  m
        timeMillis += t;
    }
    record.add(new Metric(GcCount, count));
    record.add(new Metric(GcTimeMillis, timeMillis));

    if (LOG.isDebugEnabled()) {
        LOG.debug(GcCount.description() + ": " + count);
        LOG.debug(GcTimeMillis.description() + ": " + timeMillis);
    }
}

From source file:com.snowstore.mercury.core.metric.SystemPublicMetrics.java

/**
 * Add garbage collection metrics.//w  ww . j a  v  a  2s .c  o  m
 * 
 * @param result
 *            the result
 */
protected void addGarbageCollectionMetrics(Collection<Metric<?>> result) {
    List<GarbageCollectorMXBean> garbageCollectorMxBeans = ManagementFactory.getGarbageCollectorMXBeans();
    for (GarbageCollectorMXBean garbageCollectorMXBean : garbageCollectorMxBeans) {
        String name = beautifyGcName(garbageCollectorMXBean.getName());
        result.add(new Metric<Long>("gc." + name + ".count", garbageCollectorMXBean.getCollectionCount()));
        result.add(new Metric<Long>("gc." + name + ".time", garbageCollectorMXBean.getCollectionTime()));
    }
}

From source file:fr.inria.wimmics.coresetimer.CoreseTimer.java

private long getGcCount() {
    long sum = 0;
    for (GarbageCollectorMXBean b : ManagementFactory.getGarbageCollectorMXBeans()) {
        long count = b.getCollectionCount();
        if (count != -1) {
            sum += count;/*w w w  . j a  va 2  s .c om*/
        }
    }
    return sum;
}

From source file:ca.simplegames.micro.controllers.StatsController.java

public void execute(MicroContext context, Map configuration) throws ControllerException {
    Map<String, Object> systemInfo = new HashMap<String, Object>();
    Map<String, Object> osMap = new HashMap<String, Object>();
    MBeanServerConnection mbeanServer = ManagementFactory.getPlatformMBeanServer();

    OperatingSystemMXBean sunOperatingSystemMXBean = null;
    try {//from w w  w  .  j a va 2s  . c om
        sunOperatingSystemMXBean = ManagementFactory.newPlatformMXBeanProxy(mbeanServer,
                ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, OperatingSystemMXBean.class);
    } catch (IOException e) {
        throw new ControllerException(e.getMessage());
    }

    Runtime rt = Runtime.getRuntime();
    long totalMemory = rt.totalMemory() / MEGA_BYTE;
    long freeMemory = rt.freeMemory() / MEGA_BYTE;
    long usedMemory = totalMemory - freeMemory;

    final long p100 = (int) Math.round(((double) freeMemory / (double) totalMemory) * 100);

    Map<String, Long> memInfo = new HashMap<String, Long>();

    memInfo.put("total", totalMemory);
    memInfo.put("used", usedMemory);
    memInfo.put("free", freeMemory);
    memInfo.put("percent_free", p100);

    systemInfo.put("memory", memInfo);
    systemInfo.put("powered_by", POWERED_BY_MICRO);

    //cpu usage in milli secs
    long currentCpuUsage = sunOperatingSystemMXBean.getProcessCpuTime() / 1000000;
    osMap.put("cpu_usage", currentCpuUsage);
    osMap.put("available_processors", sunOperatingSystemMXBean.getAvailableProcessors());
    osMap.put("system_load_average", sunOperatingSystemMXBean.getSystemLoadAverage());
    osMap.put("committed_virtual_memory_size", sunOperatingSystemMXBean.getCommittedVirtualMemorySize());
    osMap.put("free_physical_memory_size", sunOperatingSystemMXBean.getFreePhysicalMemorySize());
    osMap.put("total_physical_memory_size", sunOperatingSystemMXBean.getTotalPhysicalMemorySize());
    osMap.put("free_swap_space_size", sunOperatingSystemMXBean.getFreeSwapSpaceSize());
    osMap.put("total_swap_space_size", sunOperatingSystemMXBean.getTotalSwapSpaceSize());

    systemInfo.put("os", osMap);

    List<GarbageCollectorMXBean> gc = ManagementFactory.getGarbageCollectorMXBeans();
    List<Map> gcInfo = new ArrayList<Map>();

    for (GarbageCollectorMXBean aGc : gc) {
        Map<String, Object> gcMap = new HashMap<String, Object>();
        gcMap.put("name", aGc.getName());
        gcMap.put("collection_count", aGc.getCollectionCount());
        gcMap.put("collection_time", aGc.getCollectionTime());

        gcInfo.add(gcMap);
    }

    systemInfo.put("gc", gcInfo);

    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    Map<String, Object> threadInfoMap = new HashMap<String, Object>(); // more to come ;)
    threadInfoMap.put("peak_thread_count", threadMXBean.getPeakThreadCount());
    threadInfoMap.put("thread_count", threadMXBean.getThreadCount());
    threadInfoMap.put("total_started_thread_count", threadMXBean.getTotalStartedThreadCount());

    long[] deadlockedThreads = threadMXBean.findMonitorDeadlockedThreads();
    threadInfoMap.put("dead_locked_thread_count", deadlockedThreads != null ? deadlockedThreads.length : 0);
    systemInfo.put("thread_info", threadInfoMap);

    JSONObject sysinfoJson = new JSONObject(Collections.singletonMap("system_info", systemInfo));

    String sysinfoString = null;
    try {
        sysinfoString = context.getRequest().getParameter("pretty") != null ? sysinfoJson.toString(2)
                : sysinfoJson.toString();
    } catch (JSONException e) {
        e.printStackTrace();
        throw new ControllerException(e.getMessage());
    }

    context.getRackResponse().withContentType(Mime.mimeType(JSON_TYPE)).withBody(sysinfoString)
            .withContentLength(sysinfoString.length()).with(Rack.MESSAGE_STATUS, HttpServletResponse.SC_OK);

    context.halt();
}

From source file:com.thoughtworks.go.server.service.support.ServerRuntimeInformationProvider.java

private void gcInfo(List<GarbageCollectorMXBean> garbageCollectorMXBeans, InformationStringBuilder builder) {
    builder.addSection("GC information");
    for (GarbageCollectorMXBean gcBean : garbageCollectorMXBeans) {
        builder.append(String.format("%s %s %s : %s (Count : Time)\n", gcBean.getName(),
                Arrays.toString(gcBean.getMemoryPoolNames()), gcBean.getCollectionCount(),
                gcBean.getCollectionTime()));
    }//www.  j  a va 2  s .co  m
}

From source file:VerboseGC.java

/**
 * Prints the verbose GC log to System.out to list the memory usage of all
 * memory pools as well as the GC statistics.
 *///from   w w w . j  ava  2s.c  o  m
public void printVerboseGc() {
    System.out.print("Uptime: " + formatMillis(rmbean.getUptime()));
    for (GarbageCollectorMXBean gc : gcmbeans) {
        System.out.print(" [" + gc.getName() + ": ");
        System.out.print("Count=" + gc.getCollectionCount());
        System.out.print(" GCTime=" + formatMillis(gc.getCollectionTime()));
        System.out.print("]");
    }
    System.out.println();
    for (MemoryPoolMXBean p : pools) {
        System.out.print("  [" + p.getName() + ":");
        MemoryUsage u = p.getUsage();
        System.out.print(" Used=" + formatBytes(u.getUsed()));
        System.out.print(" Committed=" + formatBytes(u.getCommitted()));
        System.out.println("]");
    }
}