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:org.apache.hama.monitor.plugin.JvmTask.java

private void memory(final MetricsRecord record) {
    final MemoryUsage memNonHeap = memoryMXBean.getNonHeapMemoryUsage();
    final MemoryUsage memHeap = memoryMXBean.getHeapMemoryUsage();
    record.add(new Metric(MemNonHeapUsedM, memNonHeap.getUsed() / M));
    record.add(new Metric(MemNonHeapCommittedM, memNonHeap.getCommitted() / M));
    record.add(new Metric(MemHeapUsedM, memHeap.getUsed() / M));
    record.add(new Metric(MemHeapCommittedM, memHeap.getCommitted() / M));

    if (LOG.isDebugEnabled()) {
        LOG.debug(MemNonHeapUsedM.description() + ": " + memNonHeap.getUsed() / M);
        LOG.debug(MemNonHeapCommittedM.description() + ": " + memNonHeap.getCommitted() / M);
        LOG.debug(MemHeapUsedM.description() + ": " + memHeap.getUsed() / M);
        LOG.debug(MemHeapCommittedM.description() + ": " + memHeap.getCommitted() / M);
    }/*from  w w  w  .j  av a  2s. c o m*/
}

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

private void doMemoryUpdates() {
    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    MemoryUsage memNonHeap = memoryMXBean.getNonHeapMemoryUsage();
    MemoryUsage memHeap = memoryMXBean.getHeapMemoryUsage();
    Runtime runtime = Runtime.getRuntime();

    metrics.setMetric("memNonHeapUsedM", memNonHeap.getUsed() / M);
    metrics.setMetric("memNonHeapCommittedM", memNonHeap.getCommitted() / M);
    metrics.setMetric("memHeapUsedM", memHeap.getUsed() / M);
    metrics.setMetric("memHeapCommittedM", memHeap.getCommitted() / M);
    metrics.setMetric("maxMemoryM", runtime.maxMemory() / M);
}

From source file:com.all.ultrapeer.UltrapeerMonitor.java

private double getHeapUsage() {
    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
    return ((double) heapMemoryUsage.getUsed()) / ((double) heapMemoryUsage.getMax()) * 100.0;
}

From source file:com.janrain.backplane.server.metrics.MetricsAccumulator.java

private Map<String, Object> outputJVMUsage() {

    long mb = 1048576;

    Map<String, Object> out = new LinkedHashMap<String, Object>();

    long startTime = ManagementFactory.getRuntimeMXBean().getStartTime();
    int totalLiveThreads = ManagementFactory.getThreadMXBean().getThreadCount();
    double loadAverage = ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage();
    MemoryUsage mu = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();

    String startTimeString = BackplaneConfig.ISO8601.format(new Date(startTime));

    out.put("type", "jvm");
    out.put("unit", "mb");
    out.put("heap_used", mu.getUsed() / mb);
    out.put("heap_free", (mu.getMax() - mu.getUsed()) / mb);
    out.put("heap_max", mu.getMax() / mb);
    out.put("jvm_start_time", startTimeString);
    out.put("total_live_threads", totalLiveThreads);
    out.put("load_average_minute", String.format("%2.2f", loadAverage));

    return out;//www.  j a  va2  s.  co  m

}

From source file:org.jahia.modules.serversettings.memoryThread.MemoryThreadInformationManagement.java

public MemoryThreadInformationManagement refresh() {
    MemoryUsage usage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
    committedMemory = FileUtils.humanReadableByteCount(usage.getCommitted(), true);
    maxMemory = FileUtils.humanReadableByteCount(usage.getMax(), true);
    usedMemory = FileUtils.humanReadableByteCount(usage.getUsed(), true);
    memoryUsage = Math.round((double) usage.getUsed() / (double) usage.getMax() * 100d);
    return this;
}

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

/**
 * Add JVM heap metrics.//from www .  j av  a 2s. c  o m
 * 
 * @param result
 *            the result
 */
protected void addHeapMetrics(Collection<Metric<?>> result) {
    MemoryUsage memoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
    result.add(new Metric<Long>("heap.committed", memoryUsage.getCommitted() / 1024));
    result.add(new Metric<Long>("heap.init", memoryUsage.getInit() / 1024));
    result.add(new Metric<Long>("heap.used", memoryUsage.getUsed() / 1024));
    result.add(new Metric<Long>("heap", memoryUsage.getMax() / 1024));
}

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

/**
 * Add JVM heap metrics.//  w ww. j ava2s .co  m
 * 
 * @param result
 *            the result
 */
protected void addNoHeapMetrics(Collection<Metric<?>> result) {
    MemoryUsage memoryUsage = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
    result.add(new Metric<Long>("noheap.committed", memoryUsage.getCommitted() / 1024));
    result.add(new Metric<Long>("noheap.init", memoryUsage.getInit() / 1024));
    result.add(new Metric<Long>("noheap.used", memoryUsage.getUsed() / 1024));
    result.add(new Metric<Long>("noheap", memoryUsage.getMax() / 1024));
}

From source file:com.chinamobile.bcbsp.comm.RPCSingleSendSlave.java

/** Run method of Thread. */
public void run() {
    while (true) {
        while (this.idle) {
            if (this.completed) {
                return;
            }/* ww  w  .  j  a v a  2s.  c  o  m*/
            if (this.noMoreMessagesFlag) {
                this.superStepCounter++;
                this.noMoreMessagesFlag = false;
            }
            try {
                Thread.sleep(this.sleepTime);
            } catch (InterruptedException e) {
                LOG.error("[RPCSingleSendSlave] to " + this.hostNameAndPort + " has been interrupted for ", e);
                return;
            }
        }
        if (this.hostNameAndPort == null) {
            LOG.error("Destination hostname is null.");
            return;
        }
        if (this.messageQueue == null) {
            LOG.error("Message queue for ProducerTool is null.");
            return;
        }
        this.messageCount = 0;
        this.connectTime = 0;
        this.sendTime = 0;
        while (true) {
            if (this.reconnectCount == RPCSingleSendSlave.RECONNECTTHRESHOLD) {
                break;
            }
            try {
                if (this.newHostNameAndPort) { // Should create new connection
                    if (senderProtocol != null) {
                        LOG.info("Sender RPC protocol is not null.");
                    } else {
                        LOG.info("Sender RPC protocol is null and it will instanced now.");
                    }
                    long start = System.currentTimeMillis();
                    String[] tmp = this.hostNameAndPort.split(":");
                    String hostname = tmp[0];
                    int portNum = Integer.parseInt(tmp[1]);
                    this.senderProtocol = (RPCCommunicationProtocol) RPC.waitForProxy(
                            RPCCommunicationProtocol.class, RPCCommunicationProtocol.protocolVersion,
                            new InetSocketAddress(hostname, portNum), new Configuration());
                    this.connectTime += (System.currentTimeMillis() - start);
                    this.newHostNameAndPort = false;
                }
                // Start sending messages
                this.sendPacked();
                this.idle = true;
                break;
            } catch (Exception e) {
                this.reconnectCount++;
                if (this.reconnectCount == 1) {
                    LOG.error("[SingRPCSendSlave] to " + this.hostNameAndPort + " caught: ", e);
                }
                LOG.info("[SingRPCSendSlave] to " + this.hostNameAndPort + " is reconnecting for "
                        + this.reconnectCount + "th time.");
                LOG.info("---------------- Memory Info ------------------");
                MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
                MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
                long used = memoryUsage.getUsed();
                long committed = memoryUsage.getCommitted();
                LOG.info("[JVM Memory used] = " + used / MB_SIZE + "MB");
                LOG.info("[JVM Memory committed] = " + committed / MB_SIZE + "MB");
                LOG.info("-----------------------------------------------");
                try {
                    Thread.sleep(this.sleepTime);
                } catch (InterruptedException e1) {
                    LOG.error("[SingRPCSendSlave] caught: ", e1);
                }
            }
        }
        LOG.info("[SingRPCSendSlave] to " + this.hostNameAndPort + " has sent " + this.messageCount
                + " messages totally! (with " + this.messageQueue.size() + " messages lost!)");
        this.sender.addConnectTime(this.connectTime);
        /* Clock */
        this.sender.addSendTime(this.sendTime);
        if (this.reconnectCount == RPCSingleSendSlave.RECONNECTTHRESHOLD) {
            LOG.info("[ProducerTool] to " + this.hostNameAndPort + " has reconnected for " + this.reconnectCount
                    + " times but failed!");
            this.isFailed = true;
            break;
        }
    }
}

From source file:com.alibaba.jstorm.utils.JStormUtils.java

public static Double getMemUsage() {
    if (OSInfo.isLinux() == true) {
        try {/*from ww  w . j  ava  2 s .  c  o  m*/
            Double value = 0.0;
            String pid = JStormUtils.process_pid();
            String command = String.format("top -b -n 1 -p %s | grep -w %s", pid, pid);
            String output = SystemOperation.exec(command);

            int m = 0;
            String[] strArray = output.split(" ");
            for (int i = 0; i < strArray.length; i++) {
                String info = strArray[i];
                if (info.trim().length() == 0) {
                    continue;
                }
                if (m == 5) {
                    // memory
                    String unit = info.substring(info.length() - 1);

                    if (unit.equalsIgnoreCase("g")) {
                        value = Double.parseDouble(info.substring(0, info.length() - 1));
                        value *= 1000000000;
                    } else if (unit.equalsIgnoreCase("m")) {
                        value = Double.parseDouble(info.substring(0, info.length() - 1));
                        value *= 1000000;
                    } else {
                        value = Double.parseDouble(info);
                    }

                    //LOG.info("!!!! Get Memory Size:{}, info:{}", value, info);
                    return value;
                }
                if (m == 8) {
                    // cpu usage

                }
                if (m == 9) {
                    // memory ratio

                }
                m++;
            }
        } catch (Exception e) {
            LOG.warn("Failed to get memory usage .");

        }
    }

    // this will be incorrect
    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();

    return Double.valueOf(memoryUsage.getUsed());
}

From source file:org.romaframework.core.config.RomaApplicationContext.java

private void logMemoryUsage() {
    MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
    MemoryUsage heapMemory = memBean.getHeapMemoryUsage();
    MemoryUsage nonHeapMemory = memBean.getNonHeapMemoryUsage();

    NumberFormat nf = NumberFormat.getInstance();
    log.info("--------------- MEMORY USAGE ---------------");
    log.info("HEAP INIT MEMORY:       " + nf.format(heapMemory.getInit()) + " bytes");
    log.info("HEAP USED MEMORY:       " + nf.format(heapMemory.getUsed()) + " bytes");
    log.info("HEAP COMMITTED MEMORY:  " + nf.format(heapMemory.getCommitted()) + " bytes");
    log.info("HEAP MAX MEMORY:        " + nf.format(heapMemory.getMax()) + " bytes");
    log.info(" ");
    log.info("NON HEAP INIT MEMORY:       " + nf.format(nonHeapMemory.getInit()) + " bytes");
    log.info("NON HEAP USED MEMORY:       " + nf.format(nonHeapMemory.getUsed()) + " bytes");
    log.info("NON HEAP COMMITTED MEMORY:  " + nf.format(nonHeapMemory.getCommitted()) + " bytes");
    log.info("NON HEAP MAX MEMORY:        " + nf.format(nonHeapMemory.getMax()) + " bytes");
    log.info("--------------------------------------------");
}