Here you can find the source of usedMemory()
public static long usedMemory()
//package com.java2s; public class Main { /**/*from w ww . j a v a 2s . co m*/ * Returns the amount of memory currently used by the Java Virtual Machine. * The value returned by this method may vary over time. Calling the * {@link Runtime#gc()} method may result in a decreasing the value return * by usedMemory. * * @return an approximation to the amount of memory currently used by * allocated objects, measured in bytes. */ public static long usedMemory() { return allocatedMemory() - freeAllocatedMemory(); } /** * Returns the total amount of memory in the Java virtual machine. The value * returned by this method may vary over time, depending on the host * environment. * * @return total amount of memory currently available for current and future * objects, measured in bytes. */ public static long allocatedMemory() { return Runtime.getRuntime().totalMemory(); } /** * Returns the amount of free memory in the Java Virtual Machine. Calling * the {@link Runtime#gc()} method may result in increasing the value * returned by freeMemory. * * @return an approximation to the total amount of memory currently * available for future allocated objects, measured in bytes. */ public static long freeAllocatedMemory() { return Runtime.getRuntime().freeMemory(); } }