Here you can find the source of getMemory()
private static long getMemory()
//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; } }