Here you can find the source of getMemoryUse()
public static long getMemoryUse()
//package com.java2s; //License from project: Apache License public class Main { private static long fSLEEP_INTERVAL = 100; /**/*from w w w .ja v a2 s. c o m*/ * retrieve the usage of memory. * * @return the size of memory has been used */ public static long getMemoryUse() { putOutTheGarbage(); long totalMemory = Runtime.getRuntime().totalMemory(); putOutTheGarbage(); long freeMemory = Runtime.getRuntime().freeMemory(); return (totalMemory - freeMemory); } /** * run garbage collections. */ private static void putOutTheGarbage() { collectGarbage(); collectGarbage(); } /** * run a garbage collection. */ public static void collectGarbage() { try { System.gc(); Thread.sleep(fSLEEP_INTERVAL); System.runFinalization(); Thread.sleep(fSLEEP_INTERVAL); } catch (InterruptedException ex) { ex.printStackTrace(); } } }