Java Utililty Methods Memory Used

List of utility methods to do Memory Used

Description

The list of methods to do Memory Used are organized into topic(s).

Method

longcalcUsedMemorySize()
calculate used memory size.
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
voiddisposeUnusedMemory()
Freeing memory that is available for the application by running the garbage collector.
Runtime.getRuntime().gc();
longgetAvailableUnusedMemory()
Find out how much unused memory (in bytes) is still available for the JVM to use.
Runtime r = Runtime.getRuntime();
return r.maxMemory() 
        - r.totalMemory() 
        + r.freeMemory(); 
intgetMemoryInUse()
Returns the number of megabytes (MB) of memory in use.
Runtime runtime = Runtime.getRuntime();
long mb = 1024 * 1024;
long total = runtime.totalMemory();
long free = runtime.freeMemory();
return (int) ((total - free) / mb);
longgetMemoryUse()
retrieve the usage of memory.
putOutTheGarbage();
long totalMemory = Runtime.getRuntime().totalMemory();
putOutTheGarbage();
long freeMemory = Runtime.getRuntime().freeMemory();
return (totalMemory - freeMemory);
longgetMemoryUsed()
get Memory Used
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
intgetMemoryUsed()
get Memory Used
int mb = 1024 * 1024;
Runtime runtime = Runtime.getRuntime();
int memoryUsed = (int) ((runtime.totalMemory() - runtime.freeMemory()) / mb);
return memoryUsed;
intgetMemoryUsed()
Get the used memory in KB.
collectGarbage();
Runtime rt = Runtime.getRuntime();
long mem = rt.totalMemory() - rt.freeMemory();
return (int) (mem >> 10);
longgetMemoryUsedBytes()
get Memory Used Bytes
final Runtime runtime = Runtime.getRuntime();
return runtime.totalMemory() - runtime.freeMemory();
StringgetMemoryUsege()
get Memory Usege
return String.format("memory={total: %d MB, free: %d MB, used: %d MB, max: %d MB}",
        (Runtime.getRuntime().totalMemory() / 1000 / 1000),
        (Runtime.getRuntime().freeMemory() / 1000 / 1000),
        ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000 / 1000),
        (Runtime.getRuntime().maxMemory() / 1000 / 1000));