get Available Memory - Java java.lang

Java examples for java.lang:Runtime

Description

get Available Memory

Demo Code


//package com.java2s;

public class Main {
    public static long getAvailableMemory() {
        long freeMemory = Runtime.getRuntime().freeMemory();
        return getMaximumMemory() - freeMemory;
    }// w  w  w.  j a  v  a  2 s  . co  m

    public static long getMaximumMemory() {

        long maximumMemory = Runtime.getRuntime().maxMemory();
        long totalMemory = Runtime.getRuntime().totalMemory();

        if ((maximumMemory <= 0) || (maximumMemory == Long.MAX_VALUE)) {
            // if maximum memory can not be determined or isn't defined, then default to the totalMemory
            maximumMemory = totalMemory;
        }
        return maximumMemory;
    }
}

Related Tutorials