public void sense()

in metrics-core/src/main/java/software/amazon/swage/metrics/jmx/sensor/HeapMemoryAfterGCSensor.java [32:57]


    public void sense(final MetricContext metricContext) {
        final List<MemoryPoolMXBean> memoryPoolMXBeans = memoryPoolMXBeanSupplier.get();
        if (memoryPoolMXBeans == null) {
            throw new IllegalStateException("No memory pool MX beans available");
        }

        // Get the used and available old gen and survivor space after the last GC to calculate the heap usage.
        long usedKb = 0, totalKb = 0;
        for (final MemoryPoolMXBean memoryPool : memoryPoolMXBeanSupplier.get()) {
            if (isLongLivedMemoryPool(memoryPool)) {
                final MemoryUsage memoryPoolData = memoryPool.getCollectionUsage();

                // .getCollectionUsage() is not supported for all memory pools:
                // https://docs.oracle.com/javase/7/docs/api/java/lang/management/MemoryPoolMXBean.html#getCollectionUsage()
                if (memoryPoolData != null) {
                    final long used = memoryPoolData.getUsed();
                    usedKb += used / 1024;

                    final long max = memoryPoolData.getMax();
                    // Max can be undefined (-1) http://docs.oracle.com/javase/7/docs/api/java/lang/management/MemoryUsage.html
                    totalKb += max == -1 ? 0 : max / 1024;
                }
            }
        }
        recordHeapAfterGcUsePercentageMetric(metricContext, usedKb, totalKb);
    }