Java Memory Information getMemory()

Here you can find the source of getMemory()

Description

get Memory

License

Apache License

Declaration

private static long getMemory() 

Method Source Code

//package com.java2s;
/**/*from w w  w  . jav a  2 s. com*/
 * This file is part of ObjectFabric (http://objectfabric.org).
 *
 * ObjectFabric is licensed under the Apache License, Version 2.0, the terms
 * of which may be found at http://www.apache.org/licenses/LICENSE-2.0.html.
 * 
 * Copyright ObjectFabric Inc.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

public class Main {
    private static final int MAX_MEMORY = (int) 10e6;

    private static long getMemory() {
        long used = 0;

        /*
         * Retries a few times because memory could get allocated by another thread
         * between GC and reads.
         */
        for (int i = 0; i < 10; i++) {
            System.gc();

            long total = Runtime.getRuntime().totalMemory();
            long free = Runtime.getRuntime().freeMemory();
            used = total - free;

            if (used < MAX_MEMORY)
                return used;
        }

        return used;
    }
}

Related

  1. getConsumedMemory(boolean preRunGarbageCollector)
  2. getDenotationOfJVMMemorySettings(String stringMapJavaOpts)
  3. getInMemoryURL(String dbName)
  4. getJavaMemorySize(String string)
  5. getJVMMemoryMB()
  6. getMemory()
  7. getMemory(String memory)
  8. getMemoryConsumedSincePreviousCall()
  9. getMemoryIncrease()