Here you can find the source of getMemoryUsage()
public static long getMemoryUsage()
//package com.java2s; //License from project: Apache License public class Main { public static long getMemoryUsage() { takeOutGarbage();/* www .ja va2s. c om*/ long totalMemory = Runtime.getRuntime().totalMemory(); takeOutGarbage(); long freeMemory = Runtime.getRuntime().freeMemory(); return (totalMemory - freeMemory); } private static void takeOutGarbage() { collectGarbage(); collectGarbage(); } private static void collectGarbage() { try { System.gc(); Thread.currentThread().sleep(100); System.runFinalization(); Thread.currentThread().sleep(100); } catch (Exception ex) { ex.printStackTrace(); } } }