Search in sources :

Example 6 with MemoryUsage

use of java.lang.management.MemoryUsage in project tomcat by apache.

the class StatusTransformer method writeVMState.

/**
     * Write the VM state.
     * @param writer The output writer
     * @param mode Mode <code>0</code> will generate HTML.
     *   Mode <code>1</code> will generate XML.
     * @throws Exception Propagated JMX error
     */
public static void writeVMState(PrintWriter writer, int mode) throws Exception {
    SortedMap<String, MemoryPoolMXBean> memoryPoolMBeans = new TreeMap<>();
    for (MemoryPoolMXBean mbean : ManagementFactory.getMemoryPoolMXBeans()) {
        String sortKey = mbean.getType() + ":" + mbean.getName();
        memoryPoolMBeans.put(sortKey, mbean);
    }
    if (mode == 0) {
        writer.print("<h1>JVM</h1>");
        writer.print("<p>");
        writer.print(" Free memory: ");
        writer.print(formatSize(Long.valueOf(Runtime.getRuntime().freeMemory()), true));
        writer.print(" Total memory: ");
        writer.print(formatSize(Long.valueOf(Runtime.getRuntime().totalMemory()), true));
        writer.print(" Max memory: ");
        writer.print(formatSize(Long.valueOf(Runtime.getRuntime().maxMemory()), true));
        writer.print("</p>");
        writer.write("<table border=\"0\"><thead><tr><th>Memory Pool</th><th>Type</th><th>Initial</th><th>Total</th><th>Maximum</th><th>Used</th></tr></thead><tbody>");
        for (MemoryPoolMXBean memoryPoolMBean : memoryPoolMBeans.values()) {
            MemoryUsage usage = memoryPoolMBean.getUsage();
            writer.write("<tr><td>");
            writer.print(memoryPoolMBean.getName());
            writer.write("</td><td>");
            writer.print(memoryPoolMBean.getType());
            writer.write("</td><td>");
            writer.print(formatSize(Long.valueOf(usage.getInit()), true));
            writer.write("</td><td>");
            writer.print(formatSize(Long.valueOf(usage.getCommitted()), true));
            writer.write("</td><td>");
            writer.print(formatSize(Long.valueOf(usage.getMax()), true));
            writer.write("</td><td>");
            writer.print(formatSize(Long.valueOf(usage.getUsed()), true));
            if (usage.getMax() > 0) {
                writer.write(" (" + (usage.getUsed() * 100 / usage.getMax()) + "%)");
            }
            writer.write("</td></tr>");
        }
        writer.write("</tbody></table>");
    } else if (mode == 1) {
        writer.write("<jvm>");
        writer.write("<memory");
        writer.write(" free='" + Runtime.getRuntime().freeMemory() + "'");
        writer.write(" total='" + Runtime.getRuntime().totalMemory() + "'");
        writer.write(" max='" + Runtime.getRuntime().maxMemory() + "'/>");
        for (MemoryPoolMXBean memoryPoolMBean : memoryPoolMBeans.values()) {
            MemoryUsage usage = memoryPoolMBean.getUsage();
            writer.write("<memorypool");
            writer.write(" name='" + memoryPoolMBean.getName() + "'");
            writer.write(" type='" + memoryPoolMBean.getType() + "'");
            writer.write(" usageInit='" + usage.getInit() + "'");
            writer.write(" usageCommitted='" + usage.getCommitted() + "'");
            writer.write(" usageMax='" + usage.getMax() + "'");
            writer.write(" usageUsed='" + usage.getUsed() + "'/>");
        }
        writer.write("</jvm>");
    }
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) TreeMap(java.util.TreeMap) MemoryUsage(java.lang.management.MemoryUsage)

Example 7 with MemoryUsage

use of java.lang.management.MemoryUsage in project metrics by dropwizard.

the class MemoryUsageGaugeSet method getMetrics.

@Override
public Map<String, Metric> getMetrics() {
    final Map<String, Metric> gauges = new HashMap<String, Metric>();
    gauges.put("total.init", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getInit() + mxBean.getNonHeapMemoryUsage().getInit();
        }
    });
    gauges.put("total.used", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getUsed() + mxBean.getNonHeapMemoryUsage().getUsed();
        }
    });
    gauges.put("total.max", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getMax() + mxBean.getNonHeapMemoryUsage().getMax();
        }
    });
    gauges.put("total.committed", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getCommitted() + mxBean.getNonHeapMemoryUsage().getCommitted();
        }
    });
    gauges.put("heap.init", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getInit();
        }
    });
    gauges.put("heap.used", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getUsed();
        }
    });
    gauges.put("heap.max", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getMax();
        }
    });
    gauges.put("heap.committed", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getCommitted();
        }
    });
    gauges.put("heap.usage", new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            final MemoryUsage usage = mxBean.getHeapMemoryUsage();
            return Ratio.of(usage.getUsed(), usage.getMax());
        }
    });
    gauges.put("non-heap.init", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getInit();
        }
    });
    gauges.put("non-heap.used", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getUsed();
        }
    });
    gauges.put("non-heap.max", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getMax();
        }
    });
    gauges.put("non-heap.committed", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getCommitted();
        }
    });
    gauges.put("non-heap.usage", new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            final MemoryUsage usage = mxBean.getNonHeapMemoryUsage();
            return Ratio.of(usage.getUsed(), usage.getMax());
        }
    });
    for (final MemoryPoolMXBean pool : memoryPools) {
        final String poolName = name("pools", WHITESPACE.matcher(pool.getName()).replaceAll("-"));
        gauges.put(name(poolName, "usage"), new RatioGauge() {

            @Override
            protected Ratio getRatio() {
                MemoryUsage usage = pool.getUsage();
                return Ratio.of(usage.getUsed(), usage.getMax() == -1 ? usage.getCommitted() : usage.getMax());
            }
        });
        gauges.put(name(poolName, "max"), new Gauge<Long>() {

            @Override
            public Long getValue() {
                return pool.getUsage().getMax();
            }
        });
        gauges.put(name(poolName, "used"), new Gauge<Long>() {

            @Override
            public Long getValue() {
                return pool.getUsage().getUsed();
            }
        });
        gauges.put(name(poolName, "committed"), new Gauge<Long>() {

            @Override
            public Long getValue() {
                return pool.getUsage().getCommitted();
            }
        });
        // Only register GC usage metrics if the memory pool supports usage statistics.
        if (pool.getCollectionUsage() != null) {
            gauges.put(name(poolName, "used-after-gc"), new Gauge<Long>() {

                @Override
                public Long getValue() {
                    return pool.getCollectionUsage().getUsed();
                }
            });
        }
        gauges.put(name(poolName, "init"), new Gauge<Long>() {

            @Override
            public Long getValue() {
                return pool.getUsage().getInit();
            }
        });
    }
    return Collections.unmodifiableMap(gauges);
}
Also used : RatioGauge(com.codahale.metrics.RatioGauge) Metric(com.codahale.metrics.Metric) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) MemoryUsage(java.lang.management.MemoryUsage)

Example 8 with MemoryUsage

use of java.lang.management.MemoryUsage in project jetty.project by eclipse.

the class MemoryUsageTest method testMemoryUsage.

@SuppressWarnings("unused")
@Test
public void testMemoryUsage() throws Exception {
    int sessionCount = 1000;
    Session[] sessions = new Session[sessionCount];
    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    System.gc();
    MemoryUsage heapBefore = memoryMXBean.getHeapMemoryUsage();
    MemoryUsage nonHeapBefore = memoryMXBean.getNonHeapMemoryUsage();
    URI uri = URI.create("ws://localhost:" + connector.getLocalPort());
    final CountDownLatch latch = new CountDownLatch(sessionCount);
    for (int i = 0; i < sessionCount; ++i) {
        sessions[i] = client.connectToServer(new EndpointAdapter() {

            @Override
            public void onMessage(String message) {
                latch.countDown();
            }
        }, uri);
    }
    for (int i = 0; i < sessionCount; ++i) {
        sessions[i].getBasicRemote().sendText("OK");
    }
    latch.await(5 * sessionCount, TimeUnit.MILLISECONDS);
    System.gc();
    MemoryUsage heapAfter = memoryMXBean.getHeapMemoryUsage();
    MemoryUsage nonHeapAfter = memoryMXBean.getNonHeapMemoryUsage();
    long heapUsed = heapAfter.getUsed() - heapBefore.getUsed();
    long nonHeapUsed = nonHeapAfter.getUsed() - nonHeapBefore.getUsed();
    System.out.println("heapUsed = " + heapUsed);
    System.out.println("nonHeapUsed = " + nonHeapUsed);
    //        new CountDownLatch(1).await();
    // Assume no more than 25 KiB per session pair (client and server).
    long expected = 25 * 1024 * sessionCount;
    Assert.assertThat("heap used", heapUsed, lessThan(expected));
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) CountDownLatch(java.util.concurrent.CountDownLatch) MemoryUsage(java.lang.management.MemoryUsage) URI(java.net.URI) Endpoint(javax.websocket.Endpoint) BasicEchoEndpoint(org.eclipse.jetty.websocket.jsr356.server.samples.echo.BasicEchoEndpoint) Session(javax.websocket.Session) Test(org.junit.Test)

Example 9 with MemoryUsage

use of java.lang.management.MemoryUsage in project elasticsearch by elastic.

the class JvmStats method jvmStats.

public static JvmStats jvmStats() {
    MemoryUsage memUsage = memoryMXBean.getHeapMemoryUsage();
    long heapUsed = memUsage.getUsed() < 0 ? 0 : memUsage.getUsed();
    long heapCommitted = memUsage.getCommitted() < 0 ? 0 : memUsage.getCommitted();
    long heapMax = memUsage.getMax() < 0 ? 0 : memUsage.getMax();
    memUsage = memoryMXBean.getNonHeapMemoryUsage();
    long nonHeapUsed = memUsage.getUsed() < 0 ? 0 : memUsage.getUsed();
    long nonHeapCommitted = memUsage.getCommitted() < 0 ? 0 : memUsage.getCommitted();
    List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
    List<MemoryPool> pools = new ArrayList<>();
    for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
        try {
            MemoryUsage usage = memoryPoolMXBean.getUsage();
            MemoryUsage peakUsage = memoryPoolMXBean.getPeakUsage();
            String name = GcNames.getByMemoryPoolName(memoryPoolMXBean.getName(), null);
            if (name == null) {
                // if we can't resolve it, its not interesting.... (Per Gen, Code Cache)
                continue;
            }
            pools.add(new MemoryPool(name, usage.getUsed() < 0 ? 0 : usage.getUsed(), usage.getMax() < 0 ? 0 : usage.getMax(), peakUsage.getUsed() < 0 ? 0 : peakUsage.getUsed(), peakUsage.getMax() < 0 ? 0 : peakUsage.getMax()));
        } catch (Exception ex) {
        /* ignore some JVMs might barf here with:
                 * java.lang.InternalError: Memory Pool not found
                 * we just omit the pool in that case!*/
        }
    }
    Mem mem = new Mem(heapCommitted, heapUsed, heapMax, nonHeapCommitted, nonHeapUsed, Collections.unmodifiableList(pools));
    Threads threads = new Threads(threadMXBean.getThreadCount(), threadMXBean.getPeakThreadCount());
    List<GarbageCollectorMXBean> gcMxBeans = ManagementFactory.getGarbageCollectorMXBeans();
    GarbageCollector[] collectors = new GarbageCollector[gcMxBeans.size()];
    for (int i = 0; i < collectors.length; i++) {
        GarbageCollectorMXBean gcMxBean = gcMxBeans.get(i);
        collectors[i] = new GarbageCollector(GcNames.getByGcName(gcMxBean.getName(), gcMxBean.getName()), gcMxBean.getCollectionCount(), gcMxBean.getCollectionTime());
    }
    GarbageCollectors garbageCollectors = new GarbageCollectors(collectors);
    List<BufferPool> bufferPoolsList = Collections.emptyList();
    try {
        List<BufferPoolMXBean> bufferPools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
        bufferPoolsList = new ArrayList<>(bufferPools.size());
        for (BufferPoolMXBean bufferPool : bufferPools) {
            bufferPoolsList.add(new BufferPool(bufferPool.getName(), bufferPool.getCount(), bufferPool.getTotalCapacity(), bufferPool.getMemoryUsed()));
        }
    } catch (Exception e) {
    // buffer pools are not available
    }
    Classes classes = new Classes(classLoadingMXBean.getLoadedClassCount(), classLoadingMXBean.getTotalLoadedClassCount(), classLoadingMXBean.getUnloadedClassCount());
    return new JvmStats(System.currentTimeMillis(), runtimeMXBean.getUptime(), mem, threads, garbageCollectors, bufferPoolsList, classes);
}
Also used : ArrayList(java.util.ArrayList) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) MemoryUsage(java.lang.management.MemoryUsage) IOException(java.io.IOException) BufferPoolMXBean(java.lang.management.BufferPoolMXBean) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 10 with MemoryUsage

use of java.lang.management.MemoryUsage in project sonarqube by SonarSource.

the class ProcessStateSystemInfo method toProtobuf.

// Visible for testing
ProtobufSystemInfo.Section toProtobuf(MemoryMXBean memoryBean) {
    ProtobufSystemInfo.Section.Builder builder = ProtobufSystemInfo.Section.newBuilder();
    builder.setName(name);
    MemoryUsage heap = memoryBean.getHeapMemoryUsage();
    addAttributeInMb(builder, "Heap Committed (MB)", heap.getCommitted());
    addAttributeInMb(builder, "Heap Init (MB)", heap.getInit());
    addAttributeInMb(builder, "Heap Max (MB)", heap.getMax());
    addAttributeInMb(builder, "Heap Used (MB)", heap.getUsed());
    MemoryUsage nonHeap = memoryBean.getNonHeapMemoryUsage();
    addAttributeInMb(builder, "Non Heap Committed (MB)", nonHeap.getCommitted());
    addAttributeInMb(builder, "Non Heap Init (MB)", nonHeap.getInit());
    addAttributeInMb(builder, "Non Heap Max (MB)", nonHeap.getMax());
    addAttributeInMb(builder, "Non Heap Used (MB)", nonHeap.getUsed());
    ThreadMXBean thread = ManagementFactory.getThreadMXBean();
    builder.addAttributesBuilder().setKey("Thread Count").setLongValue(thread.getThreadCount()).build();
    return builder.build();
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) MemoryUsage(java.lang.management.MemoryUsage)

Aggregations

MemoryUsage (java.lang.management.MemoryUsage)63 MemoryMXBean (java.lang.management.MemoryMXBean)13 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)11 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)6 RuntimeMXBean (java.lang.management.RuntimeMXBean)6 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)4 ThreadMXBean (java.lang.management.ThreadMXBean)4 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 NotNull (org.jetbrains.annotations.NotNull)3 ApplicationInfo (com.intellij.openapi.application.ApplicationInfo)2 GarbageCollectionNotificationInfo (com.sun.management.GarbageCollectionNotificationInfo)2 BufferPoolMXBean (java.lang.management.BufferPoolMXBean)2 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)2 Date (java.util.Date)2 CacheServiceMBean (org.apache.cassandra.service.CacheServiceMBean)2 Metric (com.codahale.metrics.Metric)1 RatioGauge (com.codahale.metrics.RatioGauge)1