Java Memory Used usedMemory()

Here you can find the source of usedMemory()

Description

Returns the amount of memory currently used by the Java Virtual Machine.

License

Open Source License

Return

an approximation to the amount of memory currently used by allocated objects, measured in bytes.

Declaration

public static long usedMemory() 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   w  ww . j  a v a 2s . co  m*/
     * Returns the amount of memory currently used by the Java Virtual Machine.
     * The value returned by this method may vary over time. Calling the
     * {@link Runtime#gc()} method may result in a decreasing the value return
     * by usedMemory.
     *
     * @return an approximation to the amount of memory currently used by
     * allocated objects, measured in bytes.
     */
    public static long usedMemory() {
        return allocatedMemory() - freeAllocatedMemory();
    }

    /**
     * Returns the total amount of memory in the Java virtual machine. The value
     * returned by this method may vary over time, depending on the host
     * environment.
     *
     * @return total amount of memory currently available for current and future
     * objects, measured in bytes.
     */
    public static long allocatedMemory() {
        return Runtime.getRuntime().totalMemory();
    }

    /**
     * Returns the amount of free memory in the Java Virtual Machine. Calling
     * the {@link Runtime#gc()} method may result in increasing the value
     * returned by freeMemory.
     *
     * @return an approximation to the total amount of memory currently
     * available for future allocated objects, measured in bytes.
     */
    public static long freeAllocatedMemory() {
        return Runtime.getRuntime().freeMemory();
    }
}

Related

  1. memoryUsed()
  2. memoryUsedInMB()
  3. stringMemoryUsedInMB()
  4. usedMemory()
  5. usedMemory()
  6. usedMemory()
  7. usedMemory()
  8. usedMemory()
  9. usedMemory(Runtime runtime)