Example usage for java.lang.management MemoryUsage getUsed

List of usage examples for java.lang.management MemoryUsage getUsed

Introduction

In this page you can find the example usage for java.lang.management MemoryUsage getUsed.

Prototype

public long getUsed() 

Source Link

Document

Returns the amount of used memory in bytes.

Usage

From source file:com.appeligo.captions.CaptionListener.java

private synchronized static void checkStats() {
    int interval = 5; // minutes
    long timestamp = new Date().getTime();
    if ((timestamp - lastWrite) > (interval * 60 * 1000)) {
        lastWrite = timestamp;/*  w w w  .  j  av  a 2s  .c  o  m*/
        String day = Utils.getDatePath(timestamp);
        if (!day.equals(currentDay)) {
            if (statsFile != null) {
                statsFile.println("</table></body></html>");
                statsFile.close();
                statsFile = null;
            }
            currentDay = day;
        }
        if (hostname == null) {
            try {
                hostname = InetAddress.getLocalHost().getHostName();
            } catch (UnknownHostException e) {
                hostname = "UnknownHost";
            }
        }
        String dirname = documentRoot + "/stats/" + currentDay + "/" + hostname;
        String statsFileName = dirname + "/searchprocstats.html";
        try {
            if (statsFile == null) {
                File dir = new File(dirname);
                if ((!dir.exists()) && (!dir.mkdirs())) {
                    throw new IOException("Error creating directory " + dirname);
                }
                File file = new File(statsFileName);
                if (file.exists()) {
                    statsFile = new PrintStream(new FileOutputStream(statsFileName, true));
                    statsFile.println("<tr><td colspan='5'>Restart</td></tr>");
                } else {
                    statsFile = new PrintStream(new FileOutputStream(statsFileName));
                    String title = "Search Process (tomcat) status for " + currentDay;
                    statsFile.println("<html><head><title>" + title + "</title></head>");
                    statsFile.println("<body><h1>" + title + "</h1>");
                    statsFile.println("<table border='1'>");
                    statsFile.println("<tr>");
                    statsFile.println("<th colspan='2'>" + interval + " Minute Intervals</th>"
                            + "<th colspan='3'>Mem Pre GC</th>" + "<th>GC</th>"
                            + "<th colspan='3'>Mem Post GC</th>");
                    statsFile.println("</tr>");
                    statsFile.println("<tr>");
                    statsFile.println("<th>Timestamp</th>");
                    statsFile.println("<th>Time</th>");
                    statsFile.println("<th>Used</th>");
                    statsFile.println("<th>Committed</th>");
                    statsFile.println("<th>Max</th>");
                    statsFile.println("<th>Millis</th>");
                    statsFile.println("<th>Used</th>");
                    statsFile.println("<th>Committed</th>");
                    statsFile.println("<th>Max</th>");
                    statsFile.println("</tr>");
                }
            }
            Calendar cal = Calendar.getInstance();
            cal.setTimeZone(TimeZone.getTimeZone("GMT"));
            cal.setTimeInMillis(timestamp);
            String time = String.format("%1$tH:%1$tM:%1$tS", cal);
            MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
            MemoryUsage memory = memoryBean.getHeapMemoryUsage();
            statsFile.print("<tr>");
            statsFile.print("<td>" + timestamp + "</td>");
            statsFile.print("<td>" + time + "</td>");
            statsFile.format("<td>%,d</td>", memory.getUsed());
            statsFile.format("<td>%,d</td>", memory.getCommitted());
            statsFile.format("<td>%,d</td>", memory.getMax());
            long beforeGC = System.currentTimeMillis();
            System.gc();
            long elapsed = System.currentTimeMillis() - beforeGC;
            statsFile.format("<td>%,d</td>", (int) elapsed);
            memoryBean = ManagementFactory.getMemoryMXBean();
            memory = memoryBean.getHeapMemoryUsage();
            statsFile.format("<td>%,d</td>", memory.getUsed());
            statsFile.format("<td>%,d</td>", memory.getCommitted());
            statsFile.format("<td>%,d</td>", memory.getMax());
            statsFile.println("</tr>");
        } catch (IOException e) {
            log.error("Error opening or writing to " + statsFileName, e);
        }
    }
}

From source file:org.jahia.bin.errors.ErrorFileDumper.java

private static void printMemoryUsage(String type, MemoryUsage usage, PrintWriter strOut) {
    strOut.println();/* www  .j  a v  a  2s  .co  m*/
    strOut.print(type);
    strOut.print(" : ");
    strOut.print(Math.round((float) usage.getUsed() / (float) usage.getMax() * 100f));
    strOut.println("% used");
    strOut.println("---------------");
    strOut.print("Used      : ");
    strOut.println(org.jahia.utils.FileUtils.humanReadableByteCount(usage.getUsed(), true));
    strOut.print("Committed : ");
    strOut.println(org.jahia.utils.FileUtils.humanReadableByteCount(usage.getCommitted(), true));
    strOut.print("Max       : ");
    strOut.println(org.jahia.utils.FileUtils.humanReadableByteCount(usage.getMax(), true));
}

From source file:com.datumbox.common.utilities.ProcessStatistics.java

public long peakMemoryBytes() {
    long maxBytes = Long.MIN_VALUE;

    List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean pool : pools) {
        MemoryUsage peak = pool.getPeakUsage();

        long peakUsed = peak.getUsed();
        if (maxBytes < peakUsed) {
            maxBytes = peakUsed;//from   www  . j av  a  2 s .  c o m
        }
    }

    return maxBytes;
}

From source file:test.integ.be.fedict.hsm.MonitoringTest.java

@Test
public void testJMXConnection() throws Exception {
    JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:remoting-jmx://localhost:9999");
    JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxServiceURL);
    MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();
    MemoryMXBean memoryMXBean = ManagementFactory.newPlatformMXBeanProxy(connection,
            ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);
    MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
    LOG.debug("used heap memory: " + heapMemoryUsage.getUsed());
}

From source file:org.uncommons.watchmaker.swing.evolutionmonitor.JVMView.java

private void addMemoryDataPoint() {
    MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage();
    double usedMegabytes = (double) heapUsage.getUsed() / MEGABYTE;
    Second second = new Second();
    memoryUsageSeries.add(second, usedMegabytes);
    heapSizeSeries.add(second, (double) heapUsage.getCommitted() / MEGABYTE);
}

From source file:com.chinamobile.bcbsp.comm.io.util.MemoryAllocator.java

/** Refresh memory status with current JVM status. */
private void refreshMemoryStatus() {
    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
    used = memoryUsage.getUsed();
    committed = memoryUsage.getCommitted();
    maxHeapSize = memoryUsage.getMax();/*from   w  w  w . jav a2s.co  m*/
    remain = maxHeapSize - used;
}

From source file:com.chinamobile.bcbsp.comm.io.util.MemoryAllocator.java

/** Show the information of memory used. */
public void PrintMemoryInfo(Log LOG) {
    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
    long usedNow = memoryUsage.getUsed();
    long committedNow = memoryUsage.getCommitted();
    long maxHeapSizeNow = memoryUsage.getMax();
    long remainNow = maxHeapSize - used;
    LOG.info("Memory Usage **********************************************");
    LOG.info("Memory  Max ****************** " + maxHeapSizeNow);
    LOG.info("Memory Commit **************** " + committedNow + "-----Percents: "
            + committedNow * 1.0f / maxHeapSizeNow);
    LOG.info("Memory Used ****************** " + usedNow + "-----Percents: " + usedNow * 1.0f / maxHeapSizeNow);
    LOG.info("Memory Remain **************** " + remainNow + "-----Percents: "
            + remainNow * 1.0f / maxHeapSizeNow);
}

From source file:org.craftercms.commons.monitoring.MemoryMonitor.java

/**
 * Private Constructor of the MemoryMonitor POJO
 * @param memName Type of MemoryMonitor to get the information.
 * @param memoryUsage {@link MemoryUsage} bean where the information is taken from.
 *//*from  w  w w.  j a  v a  2s.co  m*/
private MemoryMonitor(String memName, MemoryUsage memoryUsage) {
    this.name = memName;
    this.init = FileUtils.byteCountToDisplaySize(memoryUsage.getInit());
    this.used = FileUtils.byteCountToDisplaySize(memoryUsage.getUsed());
    this.committed = FileUtils.byteCountToDisplaySize(memoryUsage.getCommitted());
    this.max = FileUtils.byteCountToDisplaySize(memoryUsage.getMax());
}

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

private String format(MemoryUsage memoryUsage) {
    long init = memoryUsage.getInit();
    long used = memoryUsage.getUsed();
    long committed = memoryUsage.getCommitted();
    long max = memoryUsage.getMax();

    return String.format("  init:      %s\n  used:      %s\n  committed: %s\n  max:       %s\n", init, used,
            committed, max);/*from w w w. ja v a2s  .c o  m*/
}

From source file:ch.admin.suis.msghandler.servlet.PingServlet.java

private String calcHeapSpace() {
    MemoryUsage heap = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
    long max = heap.getMax();
    long used = heap.getUsed();
    int percent = (int) Math.round(100.0 * used / max);
    return used + ":" + max + ":" + percent;
}