Java Memory Available getAvailableMemory()

Here you can find the source of getAvailableMemory()

Description

TODO TEST!!

License

Open Source License

Return

total available memory, in bytes. Does not run GC, so this is a fast call.

Declaration

public static long getAvailableMemory() 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from  w  w w . j  av a2  s.  c  o  m
     * TODO TEST!! This has changed
     * 
     * @return total available memory, in bytes. Does not run GC, so this is a fast
     *         call.
     * @see Runtime#freeMemory() -- which ignores the heap's capacity to grow, so is less
     * useful! Runtime#freeMemory() can be thought of as "fast memory".
     */
    public static long getAvailableMemory() {
        Runtime rt = Runtime.getRuntime();
        long maxMem = rt.maxMemory();
        long freeMem = rt.freeMemory();
        long totalMem = rt.totalMemory();
        long used = totalMem - freeMem;
        long available = maxMem - used;
        return available;
    }
}

Related

  1. availableMemory()
  2. availableMemory()
  3. availableMemoryPercent()
  4. getAvailableMemory()
  5. getAvailableMemory()
  6. getAvailableMemory()
  7. getAvailableMemoryFraction()
  8. getAvailableMemorySize()