Example usage for java.lang Runtime maxMemory

List of usage examples for java.lang Runtime maxMemory

Introduction

In this page you can find the example usage for java.lang Runtime maxMemory.

Prototype

public native long maxMemory();

Source Link

Document

Returns the maximum amount of memory that the Java virtual machine will attempt to use.

Usage

From source file:com.weibo.api.motan.util.StatsUtil.java

public static String memoryStatistic() {
    Runtime runtime = Runtime.getRuntime();

    double freeMemory = (double) runtime.freeMemory() / (1024 * 1024);
    double maxMemory = (double) runtime.maxMemory() / (1024 * 1024);
    double totalMemory = (double) runtime.totalMemory() / (1024 * 1024);
    double usedMemory = totalMemory - freeMemory;
    double percentFree = ((maxMemory - usedMemory) / maxMemory) * 100.0;

    double percentUsed = 100 - percentFree;

    DecimalFormat mbFormat = new DecimalFormat("#0.00");
    DecimalFormat percentFormat = new DecimalFormat("#0.0");

    StringBuilder sb = new StringBuilder();
    sb.append(mbFormat.format(usedMemory)).append("MB of ").append(mbFormat.format(maxMemory)).append(" MB (")
            .append(percentFormat.format(percentUsed)).append("%) used");
    return sb.toString();
}

From source file:com.joliciel.talismane.utils.LogUtils.java

/**
 * Log the available runtime memory./* w  w w .  j a v a2 s.c  o  m*/
 */
public static void logMemory(Log logger) {
    if (logger.isTraceEnabled()) {
        //Getting the runtime reference from system
        Runtime runtime = Runtime.getRuntime();
        logger.trace("##### Heap utilization statistics [MB] #####");

        //Print used memory
        logger.trace("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / MEGABYTE);

        //Print free memory
        logger.trace("Free Memory:" + runtime.freeMemory() / MEGABYTE);

        //Print total available memory
        logger.trace("Total Memory:" + runtime.totalMemory() / MEGABYTE);

        //Print Maximum available memory
        logger.trace("Max Memory:" + runtime.maxMemory() / MEGABYTE);
    }
}

From source file:org.apache.solr.handler.admin.SystemInfoHandler.java

/**
 * Get JVM Info - including memory info/*from   w  ww  .j  a  v a2  s. c  o  m*/
 */
public static SimpleOrderedMap<Object> getJvmInfo() {
    SimpleOrderedMap<Object> jvm = new SimpleOrderedMap<Object>();
    jvm.add("version", System.getProperty("java.vm.version"));
    jvm.add("name", System.getProperty("java.vm.name"));

    Runtime runtime = Runtime.getRuntime();
    jvm.add("processors", runtime.availableProcessors());

    long used = runtime.totalMemory() - runtime.freeMemory();
    // not thread safe, but could be thread local
    DecimalFormat df = new DecimalFormat("#.#");
    double percentUsed = ((double) (used) / (double) runtime.maxMemory()) * 100;

    SimpleOrderedMap<Object> mem = new SimpleOrderedMap<Object>();
    mem.add("free", humanReadableUnits(runtime.freeMemory(), df));
    mem.add("total", humanReadableUnits(runtime.totalMemory(), df));
    mem.add("max", humanReadableUnits(runtime.maxMemory(), df));
    mem.add("used", humanReadableUnits(used, df) + " (%" + df.format(percentUsed) + ")");
    jvm.add("memory", mem);

    // JMX properties -- probably should be moved to a different handler
    SimpleOrderedMap<Object> jmx = new SimpleOrderedMap<Object>();
    try {
        RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
        jmx.add("bootclasspath", mx.getBootClassPath());
        jmx.add("classpath", mx.getClassPath());

        // the input arguments passed to the Java virtual machine
        // which does not include the arguments to the main method.
        jmx.add("commandLineArgs", mx.getInputArguments());
        // a map of names and values of all system properties.
        //jmx.add( "SYSTEM PROPERTIES", mx.getSystemProperties());

        jmx.add("startTime", new Date(mx.getStartTime()));
        jmx.add("upTimeMS", mx.getUptime());
    } catch (Exception e) {
        log.warn("Error getting JMX properties", e);
    }
    jvm.add("jmx", jmx);
    return jvm;
}

From source file:SystemUtils.java

public static double GetTotalMemory() {
    Runtime rt = Runtime.getRuntime();
    double maxMem = rt.maxMemory() / 1048576.0d;
    return maxMem;
}

From source file:jfs.sync.encryption.JFSEncryptedStream.java

public static OutputStream createOutputStream(long compressionLimit, OutputStream baseOutputStream, long length,
        Cipher cipher) throws IOException {
    OutputStream result = null;/*  w  w w.j a  va 2 s.  c  o  m*/
    Runtime runtime = Runtime.getRuntime();
    long freeMem = runtime.totalMemory() / SPACE_RESERVE;
    if (length >= freeMem) {
        if (log.isWarnEnabled()) {
            log.warn("JFSEncryptedStream.createOutputStream() GC " + freeMem + "/" + runtime.maxMemory());
        } // if
        runtime.gc();
        freeMem = runtime.totalMemory() / SPACE_RESERVE;
    } // if
    if ((length < compressionLimit) && (length < freeMem)) {
        result = new JFSEncryptedStream(baseOutputStream, cipher);
    } else {
        if (length < freeMem) {
            if (log.isInfoEnabled()) {
                log.info("JFSEncryptedStream.createOutputStream() not compressing");
            } // if
        } else {
            if (log.isWarnEnabled()) {
                log.warn("JFSEncryptedStream.createOutputStream() due to memory constraints (" + length + "/"
                        + freeMem + ") not compressing");
            } // if
        } // if
        ObjectOutputStream oos = new ObjectOutputStream(baseOutputStream);
        oos.writeByte(COMPRESSION_NONE);
        oos.writeLong(length);
        oos.flush();
        result = baseOutputStream;
        if (cipher != null) {
            result = new CipherOutputStream(result, cipher);
        } // if
    } // if
    return result;
}

From source file:org.apache.accumulo.server.gc.SimpleGarbageCollector.java

static public boolean almostOutOfMemory() {
    Runtime runtime = Runtime.getRuntime();
    return runtime.totalMemory() - runtime.freeMemory() > CANDIDATE_MEMORY_PERCENTAGE * runtime.maxMemory();
}

From source file:com.viettel.logistic.common.logs.KPILogger.java

public static void checkMemory(String functionName) {
    //Getting the runtime reference from system
    Runtime runtime = Runtime.getRuntime();
    int mb = 1024 * 1024;

    System.out.println("##### Heap utilization statistics [MB] #####");

    //Print used memory
    System.out.println(functionName + " Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / mb);

    //Print free memory
    System.out.println(functionName + " Free Memory:" + runtime.freeMemory() / mb);

    //Print total available memory
    System.out.println(functionName + " Total Memory:" + runtime.totalMemory() / mb);

    //Print Maximum available memory

    System.out.println(functionName + " Max Memory:" + runtime.maxMemory() / mb);

}

From source file:org.soitoolkit.commons.mule.util.MiscUtil.java

private static void printMemUsage() {
    int mb = 1024 * 1024;

    MemoryMXBean mxb = ManagementFactory.getMemoryMXBean();
    MemoryUsage hm = mxb.getHeapMemoryUsage();
    MemoryUsage nhm = mxb.getNonHeapMemoryUsage();
    //      int finalizable = mxb.getObjectPendingFinalizationCount();

    logger.trace("Heap Memory:  init/used/committed/max=" + hm.getInit() / mb + "/" + hm.getUsed() / mb + "/"
            + hm.getCommitted() / mb + "/" + hm.getMax() / mb);
    logger.trace("Non-Heap Mem: init/used/committed/max=" + nhm.getInit() / mb + "/" + nhm.getUsed() / mb + "/"
            + nhm.getCommitted() / mb + "/" + nhm.getMax() / mb);
    //                 logger.trace("finalizable: " + finalizable);

    //Getting the runtime reference from system
    Runtime runtime = Runtime.getRuntime();

    logger.trace("Used/Free/Total/Max:"
            //Print used memory
            + (runtime.totalMemory() - runtime.freeMemory()) / mb + "/"

            //Print free memory
            + runtime.freeMemory() / mb + "/"

            //Print total available memory
            + runtime.totalMemory() / mb + "/"

            //Print Maximum available memory
            + runtime.maxMemory() / mb);
}

From source file:SystemUtils.java

public static double GetAvailableMemory() {
    Runtime rt = Runtime.getRuntime();
    double availableMem;
    long maxMem, freeMem, usedJvmMem;

    usedJvmMem = rt.totalMemory();/* ww w  .  ja v  a2  s  .co  m*/
    maxMem = rt.maxMemory();
    freeMem = rt.freeMemory();
    availableMem = (maxMem - (usedJvmMem - freeMem)) / 1048576.0d;

    return availableMem;
}

From source file:fiftyfive.wicket.util.LoggingUtils.java

/**
 * Returns a string that describes the JVM's memory conditions in this
 * format: {@code 36M used, 29M free, 533M max}.
 *//*from   ww  w  .  j a  v a2s.co  m*/
public static String describeMemoryUsage() {
    Runtime runtime = Runtime.getRuntime();
    long free = runtime.freeMemory();

    return String.format("%sM used, %sM free, %sM max", (runtime.totalMemory() - free) / 1000000,
            free / 1000000, runtime.maxMemory() / 1000000);
}