Example usage for java.lang Runtime totalMemory

List of usage examples for java.lang Runtime totalMemory

Introduction

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

Prototype

public native long totalMemory();

Source Link

Document

Returns the total amount of memory in the Java virtual machine.

Usage

From source file:com.eryansky.utils.SigarUtil.java

/**
 * ??/*from   w  ww  .  j  a  va2  s  .c  o m*/
 * @throws Exception
 */
public static ServerStatus getServerStatus() throws Exception {
    ServerStatus status = new ServerStatus();
    status.setServerTime(DateFormatUtils.format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss"));
    status.setServerName(System.getenv().get("COMPUTERNAME"));

    Runtime rt = Runtime.getRuntime();
    //status.setIp(InetAddress.getLocalHost().getHostAddress());
    status.setJvmTotalMem(rt.totalMemory() / (1024 * 1024));
    status.setJvmFreeMem(rt.freeMemory() / (1024 * 1024));
    status.setJvmMaxMem(rt.maxMemory() / (1024 * 1024));
    Properties props = System.getProperties();
    status.setServerOs(props.getProperty("os.name") + " " + props.getProperty("os.arch") + " "
            + props.getProperty("os.version"));
    status.setJavaHome(props.getProperty("java.home"));
    status.setJavaVersion(props.getProperty("java.version"));
    status.setJavaTmpPath(props.getProperty("java.io.tmpdir"));

    Sigar sigar = new Sigar();
    getServerCpuInfo(sigar, status);
    getServerDiskInfo(sigar, status);
    getServerMemoryInfo(sigar, status);

    return status;
}

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

/**
 * Log the available runtime memory.// ww w.j  av  a  2  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.openkoala.koala.monitor.support.ServerStatusCollector.java

/**
 * ???/*from   w  ww  .ja  v  a  2 s  . c  o m*/
 * @return
 */
public static void getServerBaseInfo(ServerStatusVo status) {
    status.setServerTime(DateFormatUtils.format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss"));
    status.setServerName(System.getenv().get("COMPUTERNAME"));
    //      status.setJavaServer(RuntimeContext.getContext().getServerName());
    //      status.setDeployPath(RuntimeContext.getContext().getDeployPath());

    Runtime rt = Runtime.getRuntime();
    status.setJvmTotalMem(rt.totalMemory() / (1024 * 1024));
    status.setJvmFreeMem(rt.freeMemory() / (1024 * 1024));

    Properties props = System.getProperties();
    status.setServerOs(props.getProperty("os.name") + " " + props.getProperty("os.arch") + " "
            + props.getProperty("os.version"));
    status.setJavaHome(props.getProperty("java.home"));
    status.setJavaVersion(props.getProperty("java.version"));
    status.setJavaTmpPath(props.getProperty("java.io.tmpdir"));

}

From source file:net.ymate.platform.core.util.RuntimeUtils.java

/**
 * /*  ww w .j a  v a 2s  . c  om*/
 *
 * @return ??
 */
public static long gc() {
    Runtime rt = Runtime.getRuntime();
    long lastUsed = rt.totalMemory() - rt.freeMemory();
    rt.gc();
    return lastUsed - rt.totalMemory() + rt.freeMemory();
}

From source file:net.nosleep.superanalyzer.util.Misc.java

public static void printMemoryInfo(String tag) {
    Runtime rt = Runtime.getRuntime();
    long free = rt.freeMemory();
    long total = rt.totalMemory();
    long max = rt.maxMemory();

    System.out.println("Memory (" + tag + "): " + free + "/" + total + " (max " + max + ")");
}

From source file:com.dajodi.scandic.Util.java

public static void printMemory() {
    Runtime runtime = Runtime.getRuntime();

    long maxMemory = runtime.maxMemory();
    long allocatedMemory = runtime.totalMemory();
    long freeMemory = runtime.freeMemory();
    long totalFreeMemory = (freeMemory + (maxMemory - allocatedMemory)) / 1024;
    Log.d("free memory: " + freeMemory / 1024);
    Log.d("allocated memory: " + allocatedMemory / 1024);
    Log.d("max memory: " + maxMemory / 1024);
    Log.d("total free memory: " + totalFreeMemory);
}

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:com.baasbox.service.storage.StatisticsService.java

public static ImmutableMap memory() {
    if (BaasBoxLogger.isTraceEnabled())
        BaasBoxLogger.trace("Method Start");
    ImmutableMap response = null;/*from   www .  j av a2  s  . c o m*/

    if (BBConfiguration.getStatisticsSystemMemory()) {
        Runtime rt = Runtime.getRuntime();
        long maxMemory = rt.maxMemory();
        long freeMemory = rt.freeMemory();
        long totalMemory = rt.totalMemory();
        response = ImmutableMap.of("max_allocable_memory", maxMemory, "current_allocate_memory", totalMemory,
                "used_memory_in_the_allocate_memory", totalMemory - freeMemory,
                "free_memory_in_the_allocated_memory", freeMemory);
    } else {
        response = ImmutableMap.of("max_allocable_memory", 0, "current_allocate_memory", 0,
                "used_memory_in_the_allocate_memory", 0, "free_memory_in_the_allocated_memory", 0);
    }

    if (BaasBoxLogger.isTraceEnabled())
        BaasBoxLogger.trace("Method End");
    return response;
}

From source file:org.apache.flink.runtime.instance.HardwareDescriptionFactory.java

/**
 * Returns the size of free memory in bytes available to the JVM.
 * /*from  w w  w.j  a  v  a2s .c  o m*/
 * @return the size of the free memory in bytes available to the JVM or <code>-1</code> if the size cannot be
 *         determined
 */
private static long getSizeOfFreeMemory() {
    Runtime r = Runtime.getRuntime();
    long available = r.maxMemory() - r.totalMemory() + r.freeMemory();

    return available;
}

From source file:com.fizzed.rocker.ConstantPoolMain.java

static public void garbageCollectAndPrintMemory() {

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

    runtime.gc();/*from  www . ja  v a  2  s.  c  om*/

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

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

    //Print free memory
    /**
    System.out.println("Free Memory:"
    + runtime.freeMemory());
             
    //Print total available memory
    System.out.println("Total Memory:" + runtime.totalMemory());
            
    //Print Maximum available memory
    System.out.println("Max Memory:" + runtime.maxMemory());
    */
}