Java Memory Used usedMemory()

Here you can find the source of usedMemory()

Description

Return the total memory currently in use.

License

Open Source License

Declaration

public static long usedMemory() 

Method Source Code

//package com.java2s;
/*//from w ww.  j av a 2s.  c o  m
 * Copyright 2005--2008 Helsinki Institute for Information Technology
 *
 * This file is a part of Fuego middleware.  Fuego middleware is free
 * software; you can redistribute it and/or modify it under the terms
 * of the MIT license, included as the file MIT-LICENSE in the Fuego
 * middleware source distribution.  If you did not receive the MIT
 * license with the distribution, write to the Fuego Core project at
 * fuego-core-users@googlegroups.com.
 */

public class Main {
    private static final Runtime runtime = Runtime.getRuntime();

    /**
     * Return the total memory currently in use. This method returns the
     * amount of memory (in bytes) currently used of the heap. The
     * possibility of heap size increase is taken into account.
     * The measurement is fast but may be inaccurate.
     * @see <a
     *      href="http://www.javaworld.com/javaworld/javatips/jw-javatip130.html">Java
     *      Tip 130</a>. 
     *      
     */
    public static long usedMemory() {
        return runtime.totalMemory() - runtime.freeMemory();
    }

    /** Return the total memory currently in use. This method returns the
     * amount of memory (in bytes) currently used of the heap. The
     * possibility of heap size increase is taken into account.
     * @see <a
     *      href="http://www.javaworld.com/javaworld/javatips/jw-javatip130.html">Java
     *      Tip 130</a>
     * 
     * @param accurate flag to indicate if an accurate measure should be 
     * attempted (e.g., by looping <code>gc()</code>). An accurate measurement
     * may be much slower than an inaccurate one. 
     * @return used memory
     */
    public static long usedMemory(boolean accurate) {
        if (accurate)
            runGc();
        return runtime.totalMemory() - runtime.freeMemory();
    }

    /**
     * Run the garbage collector. This method runs calls the garbage
     * collection several times to try to make sure all garbage is actually
     * collected after the method finishes.
     * @see <a
     *      href="http://www.javaworld.com/javaworld/javatips/jw-javatip130.html">Java
     *      Tip 130</a>
     */
    public static void runGc() {
        for (int i = 0; i < 4; i++) {
            _runGc();
        }
    }

    private static void _runGc() {
        long isFree = runtime.freeMemory();
        long wasFree;
        do {
            wasFree = isFree;
            runtime.runFinalization();
            runtime.gc();
            Thread.yield();
            isFree = runtime.freeMemory();
        } while (isFree > wasFree);
    }
}

Related

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