Here you can find the source of getMemoryUsed()
public static int getMemoryUsed()
//package com.java2s; /*/*from w ww. j av a 2 s. co m*/ * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, * and the EPL 1.0 (http://h2database.com/html/license.html). * Initial Developer: H2 Group */ public class Main { private static final int GC_DELAY = 50; private static final int MAX_GC = 8; private static long lastGC; /** * Get the used memory in KB. * This method possibly calls System.gc(). * * @return the used memory */ public static int getMemoryUsed() { collectGarbage(); Runtime rt = Runtime.getRuntime(); long mem = rt.totalMemory() - rt.freeMemory(); return (int) (mem >> 10); } private static synchronized void collectGarbage() { Runtime runtime = Runtime.getRuntime(); long total = runtime.totalMemory(); long time = System.currentTimeMillis(); if (lastGC + GC_DELAY < time) { for (int i = 0; i < MAX_GC; i++) { runtime.gc(); long now = runtime.totalMemory(); if (now == total) { lastGC = System.currentTimeMillis(); break; } total = now; } } } }