returns some statistics for RAM usage - Java java.lang

Java examples for java.lang:Runtime

Description

returns some statistics for RAM usage

Demo Code


//package com.java2s;
import java.text.NumberFormat;

public class Main {
    /**//  ww w  .j av  a2s  . c  o m
     * returns some statistics for RAM usage
     * @return
     */
    public static String getSystemUsage() {

        Runtime runtime = Runtime.getRuntime();

        NumberFormat format = NumberFormat.getInstance();

        String nl = System.getProperty("line.separator");

        StringBuilder sb = new StringBuilder();
        long maxMemory = runtime.maxMemory();
        long allocatedMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();

        sb.append("free memory: " + format.format(freeMemory / 1024) + nl);
        sb.append("allocated memory: "
                + format.format(allocatedMemory / 1024) + nl);
        sb.append("max memory: " + format.format(maxMemory / 1024) + nl);
        sb.append("total free memory: "
                + format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024)
                + nl);

        return sb.toString();

    }
}

Related Tutorials