Search in sources :

Example 1 with MemoryMXBean

use of java.lang.management.MemoryMXBean 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 2 with MemoryMXBean

use of java.lang.management.MemoryMXBean in project flink by apache.

the class TaskExecutorMetricsInitializer method instantiateMemoryMetrics.

private static void instantiateMemoryMetrics(MetricGroup metrics) {
    final MemoryMXBean mxBean = ManagementFactory.getMemoryMXBean();
    MetricGroup heap = metrics.addGroup("Heap");
    heap.<Long, Gauge<Long>>gauge("Used", new Gauge<Long>() {

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

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getCommitted();
        }
    });
    heap.<Long, Gauge<Long>>gauge("Max", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getMax();
        }
    });
    MetricGroup nonHeap = metrics.addGroup("NonHeap");
    nonHeap.<Long, Gauge<Long>>gauge("Used", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getUsed();
        }
    });
    nonHeap.<Long, Gauge<Long>>gauge("Committed", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getCommitted();
        }
    });
    nonHeap.<Long, Gauge<Long>>gauge("Max", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getMax();
        }
    });
    final MBeanServer con = ManagementFactory.getPlatformMBeanServer();
    final String directBufferPoolName = "java.nio:type=BufferPool,name=direct";
    try {
        final ObjectName directObjectName = new ObjectName(directBufferPoolName);
        MetricGroup direct = metrics.addGroup("Direct");
        direct.<Long, Gauge<Long>>gauge("Count", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, directObjectName, "Count", -1L));
        direct.<Long, Gauge<Long>>gauge("MemoryUsed", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, directObjectName, "MemoryUsed", -1L));
        direct.<Long, Gauge<Long>>gauge("TotalCapacity", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, directObjectName, "TotalCapacity", -1L));
    } catch (MalformedObjectNameException e) {
        LOG.warn("Could not create object name {}.", directBufferPoolName, e);
    }
    final String mappedBufferPoolName = "java.nio:type=BufferPool,name=mapped";
    try {
        final ObjectName mappedObjectName = new ObjectName(mappedBufferPoolName);
        MetricGroup mapped = metrics.addGroup("Mapped");
        mapped.<Long, Gauge<Long>>gauge("Count", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, mappedObjectName, "Count", -1L));
        mapped.<Long, Gauge<Long>>gauge("MemoryUsed", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, mappedObjectName, "MemoryUsed", -1L));
        mapped.<Long, Gauge<Long>>gauge("TotalCapacity", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, mappedObjectName, "TotalCapacity", -1L));
    } catch (MalformedObjectNameException e) {
        LOG.warn("Could not create object name {}.", mappedBufferPoolName, e);
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) MemoryMXBean(java.lang.management.MemoryMXBean) MetricGroup(org.apache.flink.metrics.MetricGroup) Gauge(org.apache.flink.metrics.Gauge) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 3 with MemoryMXBean

use of java.lang.management.MemoryMXBean in project midpoint by Evolveum.

the class WebComponentUtil method getRamUsage.

public static double getRamUsage() {
    int MB = 1024 * 1024;
    MemoryMXBean mBean = ManagementFactory.getMemoryMXBean();
    long usedHead = mBean.getHeapMemoryUsage().getUsed();
    long usedNonHeap = mBean.getNonHeapMemoryUsage().getUsed();
    return (usedHead + usedNonHeap) / MB;
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean)

Example 4 with MemoryMXBean

use of java.lang.management.MemoryMXBean in project adempiere by adempiere.

the class MSystem method info.

//	getDBInfoSQL
/**
	 * 	Print info
	 */
public void info() {
    if (!CLogMgt.isLevelFine())
        return;
    //	OS
    //	OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
    //	log.fine(os.getName() + " " + os.getVersion() + " " + os.getArch() 
    //		+ " Processors=" + os.getAvailableProcessors());
    //	Runtime
    RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
    //	Memory
    if (CLogMgt.isLevelFiner()) {
        List<MemoryPoolMXBean> list = ManagementFactory.getMemoryPoolMXBeans();
        Iterator<MemoryPoolMXBean> it = list.iterator();
        while (it.hasNext()) {
            MemoryPoolMXBean pool = (MemoryPoolMXBean) it.next();
        /*
				log.finer(pool.getName() + " " + pool.getType() 
					+ ": " + new CMemoryUsage(pool.getUsage()));
				*/
        }
    } else {
        MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
    // log.fine("VM: " + new CMemoryUsage(memory.getNonHeapMemoryUsage()));
    // log.fine("Heap: " + new CMemoryUsage(memory.getHeapMemoryUsage()));
    }
    //	Thread
    ThreadMXBean th = ManagementFactory.getThreadMXBean();
/*
		log.fine("Threads=" + th.getThreadCount()
			+ ", Peak=" + th.getPeakThreadCount()
			+ ", Demons=" + th.getDaemonThreadCount()
			+ ", Total=" + th.getTotalStartedThreadCount()
		);
		*/
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) MemoryMXBean(java.lang.management.MemoryMXBean) RuntimeMXBean(java.lang.management.RuntimeMXBean) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 5 with MemoryMXBean

use of java.lang.management.MemoryMXBean in project adempiere by adempiere.

the class AdempiereMonitor method createLogMgtPage.

//	createSummaryPage
/**
	 * 	Add Log Management to page
	 *	@param bb body
	 */
private void createLogMgtPage(body bb) {
    bb.addElement(new hr());
    //	Ini Parameters
    table table = new table();
    table.setBorder(1);
    table.setCellSpacing(2);
    table.setCellPadding(2);
    //
    Properties ctx = new Properties();
    MSystem system = MSystem.get(ctx);
    tr line = new tr();
    line.addElement(new th().addElement(system.getDBAddress()));
    line.addElement(new td().addElement(Ini.getAdempiereHome()));
    table.addElement(line);
    //	OS + Name
    line = new tr();
    String info = System.getProperty("os.name") + " " + System.getProperty("os.version");
    String s = System.getProperty("sun.os.patch.level");
    if (s != null && s.length() > 0)
        info += " (" + s + ")";
    line.addElement(new th().addElement(info));
    info = system.getName();
    if (system.getCustomPrefix() != null)
        info += " (" + system.getCustomPrefix() + ")";
    line.addElement(new td().addElement(info));
    table.addElement(line);
    //	Java + email
    line = new tr();
    info = System.getProperty("java.vm.name") + " " + System.getProperty("java.vm.version");
    line.addElement(new th().addElement(info));
    line.addElement(new td().addElement(system.getUserName()));
    table.addElement(line);
    //	DB + Instance
    line = new tr();
    CConnection cc = CConnection.get();
    AdempiereDatabase db = cc.getDatabase();
    info = db.getDescription();
    line.addElement(new th().addElement(info));
    line.addElement(new td().addElement(cc.getConnectionURL()));
    //		line.addElement(new td().addElement(system.getDBInstance()));
    table.addElement(line);
    //	Processors/Support
    line = new tr();
    line.addElement(new th().addElement("Processor/Support"));
    line.addElement(new td().addElement(system.getNoProcessors() + "/" + system.getSupportUnits()));
    table.addElement(line);
    //	Memory
    line = new tr();
    MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
    line.addElement(new th().addElement("VM Memory"));
    line.addElement(new td().addElement(new CMemoryUsage(memory.getNonHeapMemoryUsage()).toString()));
    table.addElement(line);
    line = new tr();
    line.addElement(new th().addElement("Heap Memory"));
    line.addElement(new td().addElement(new CMemoryUsage(memory.getHeapMemoryUsage()).toString()));
    table.addElement(line);
    //	Runtime
    line = new tr();
    RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
    line.addElement(new th().addElement("Runtime " + rt.getName()));
    line.addElement(new td().addElement(TimeUtil.formatElapsed(rt.getUptime())));
    table.addElement(line);
    //	Threads
    line = new tr();
    ThreadMXBean th = ManagementFactory.getThreadMXBean();
    line.addElement(new th().addElement("Threads " + th.getThreadCount()));
    line.addElement(new td().addElement("Peak=" + th.getPeakThreadCount() + ", Demons=" + th.getDaemonThreadCount() + ", Total=" + th.getTotalStartedThreadCount()));
    table.addElement(line);
    //Transactions
    Trx[] trxs = Trx.getActiveTransactions();
    for (Trx trx : trxs) {
        if (trx != null && trx.isActive()) {
            line = new tr();
            line.addElement(new th().addElement("Active Transaction "));
            line.addElement(new td().addElement("Name=" + trx.getTrxName() + ", StartTime=" + trx.getStartTime()));
            table.addElement(line);
        }
    }
    //	Cache Reset
    line = new tr();
    line.addElement(new th().addElement(CacheMgt.get().toStringX()));
    line.addElement(new td().addElement(new a("adempiereMonitor?CacheReset=Yes", "Reset Cache")));
    table.addElement(line);
    //	Trace Level
    line = new tr();
    line.addElement(new th().addElement(new label("TraceLevel").addElement("Trace Log Level")));
    form myForm = new form("adempiereMonitor", form.METHOD_POST, form.ENC_DEFAULT);
    //	LogLevel Selection
    option[] options = new option[CLogMgt.LEVELS.length];
    for (int i = 0; i < options.length; i++) {
        options[i] = new option(CLogMgt.LEVELS[i].getName());
        options[i].addElement(CLogMgt.LEVELS[i].getName());
        if (CLogMgt.LEVELS[i] == CLogMgt.getLevel())
            options[i].setSelected(true);
    }
    select sel = new select("TraceLevel", options);
    myForm.addElement(sel);
    myForm.addElement(new input(input.TYPE_SUBMIT, "Set", "Set"));
    line.addElement(new td().addElement(myForm));
    table.addElement(line);
    //
    line = new tr();
    CLogFile fileHandler = CLogFile.get(true, null, false);
    line.addElement(new th().addElement("Trace File"));
    line.addElement(new td().addElement(new a("adempiereMonitor?Trace=" + fileHandler.getFileName(), "Current")));
    table.addElement(line);
    //
    line = new tr();
    line.addElement(new td().addElement(new a("adempiereMonitor?Trace=ROTATE", "Rotate Trace Log")));
    line.addElement(new td().addElement(new a("adempiereMonitor?Trace=DELETE", "Delete all Trace Logs")));
    table.addElement(line);
    //
    bb.addElement(table);
    //	List Log Files
    p p = new p();
    p.addElement(new b("All Log Files: "));
    //	All in dir
    File logDir = fileHandler.getLogDirectory();
    if (logDir != null && logDir.isDirectory()) {
        File[] logs = logDir.listFiles();
        for (int i = 0; i < logs.length; i++) {
            // Skip if is not a file - teo_sarca [ 1726066 ]
            if (!logs[i].isFile())
                continue;
            if (i != 0)
                p.addElement(" - ");
            String fileName = logs[i].getAbsolutePath();
            a link = new a("adempiereMonitor?Trace=" + fileName, fileName);
            p.addElement(link);
            int size = (int) (logs[i].length() / 1024);
            if (size < 1024)
                p.addElement(" (" + size + "k)");
            else
                p.addElement(" (" + size / 1024 + "M)");
        }
    }
    bb.addElement(p);
    //	Clients and Web Stores
    table = new table();
    table.setBorder(1);
    table.setCellSpacing(2);
    table.setCellPadding(2);
    //	
    line = new tr();
    MClient[] clients = MClient.getAll(ctx);
    line.addElement(new th().addElement("Client #" + clients.length + " - EMail Test:"));
    p = new p();
    for (int i = 0; i < clients.length; i++) {
        MClient client = clients[i];
        if (i > 0)
            p.addElement(" - ");
        p.addElement(new a("adempiereMonitor?EMail=" + client.getAD_Client_ID(), client.getName()));
    }
    if (clients.length == 0)
        p.addElement("&nbsp;");
    line.addElement(new td().addElement(p));
    table.addElement(line);
    //	
    line = new tr();
    MStore[] wstores = MStore.getActive();
    line.addElement(new th().addElement("Active Web Stores #" + wstores.length));
    p = new p();
    for (int i = 0; i < wstores.length; i++) {
        MStore store = wstores[i];
        if (i > 0)
            p.addElement(" - ");
        a a = new a(store.getWebContext(), store.getName());
        a.setTarget("t" + i);
        p.addElement(a);
    }
    if (wstores.length == 0)
        p.addElement("&nbsp;");
    line.addElement(new td().addElement(p));
    table.addElement(line);
    //
    bb.addElement(table);
}
Also used : org.apache.ecs.xhtml.select(org.apache.ecs.xhtml.select) org.apache.ecs.xhtml.hr(org.apache.ecs.xhtml.hr) Properties(java.util.Properties) MClient(org.compiere.model.MClient) MemoryMXBean(java.lang.management.MemoryMXBean) Trx(org.compiere.util.Trx) MSystem(org.compiere.model.MSystem) org.apache.ecs.xhtml.table(org.apache.ecs.xhtml.table) ThreadMXBean(java.lang.management.ThreadMXBean) org.apache.ecs.xhtml.a(org.apache.ecs.xhtml.a) CLogFile(org.compiere.util.CLogFile) org.apache.ecs.xhtml.b(org.apache.ecs.xhtml.b) RuntimeMXBean(java.lang.management.RuntimeMXBean) org.apache.ecs.xhtml.label(org.apache.ecs.xhtml.label) org.apache.ecs.xhtml.td(org.apache.ecs.xhtml.td) CConnection(org.compiere.db.CConnection) org.apache.ecs.xhtml.p(org.apache.ecs.xhtml.p) Timestamp(java.sql.Timestamp) org.apache.ecs.xhtml.input(org.apache.ecs.xhtml.input) AdempiereDatabase(org.compiere.db.AdempiereDatabase) org.apache.ecs.xhtml.th(org.apache.ecs.xhtml.th) org.apache.ecs.xhtml.form(org.apache.ecs.xhtml.form) CMemoryUsage(org.compiere.util.CMemoryUsage) MStore(org.compiere.model.MStore) CLogFile(org.compiere.util.CLogFile) File(java.io.File) org.apache.ecs.xhtml.tr(org.apache.ecs.xhtml.tr) org.apache.ecs.xhtml.option(org.apache.ecs.xhtml.option)

Aggregations

MemoryMXBean (java.lang.management.MemoryMXBean)84 MemoryUsage (java.lang.management.MemoryUsage)30 RuntimeMXBean (java.lang.management.RuntimeMXBean)18 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)12 IOException (java.io.IOException)11 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)11 ThreadMXBean (java.lang.management.ThreadMXBean)11 NotificationEmitter (javax.management.NotificationEmitter)10 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)9 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 Map (java.util.Map)7 Test (org.junit.Test)7 Test (org.testng.annotations.Test)7 ObjectName (javax.management.ObjectName)6 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)5 Properties (java.util.Properties)5 MalformedObjectNameException (javax.management.MalformedObjectNameException)5 Notification (javax.management.Notification)5 MemoryMXBeanImpl (com.ibm.java.lang.management.internal.MemoryMXBeanImpl)4