Java Utililty Methods Memory Information

List of utility methods to do Memory Information

Description

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

Method

longgetMemoryIncrease()
Will calculate the increase in memory compared to the moment of the most recent startMeasurement() call.
if (before == null || after == null) {
    throw new IllegalStateException(
            "You must call startMeasurement() at least once before calling this method");
hardClean();
after = getMemoryUsage();
long diff = after - before;
return (diff);
...
StringgetMemoryInfo()
get Memory Info
return Runtime.getRuntime().totalMemory() / 1024L / 1024L + "M/"
        + Runtime.getRuntime().maxMemory() / 1024L / 1024L + "M";
longgetMemoryLimit()
Get maximum memory which can be allocated by jvm.
return Runtime.getRuntime().maxMemory();
intgetMemoryLimitMinSize()
get Memory Limit Min Size
return memoryLimitMinSize.intValue();
StringgetMemoryStats()
get Memory Stats
Runtime r = Runtime.getRuntime();
System.gc();
int MB = 1024 * 1024;
return r.freeMemory() / MB + " MB free, " + (r.totalMemory() / MB - r.freeMemory() / MB) + " MB used, "
        + r.maxMemory() / MB + " MB max, " + r.totalMemory() / MB + " MB total";
intgetMemoryStats(int mode)
Returns the desired information about the game's memory
switch (mode) {
case TOTAL_MEMORY:
    return (int) (runtime.totalMemory() / 1024 / 1024);
case USED_MEMORY:
    return (int) ((runtime.totalMemory() - runtime.freeMemory()) / 1024 / 1024);
case FREE_MEMORY:
    return (int) (runtime.freeMemory() / 1024 / 1024);
return 0;
StringgetMemoryStatus(boolean preRunGarbageCollector)
get Memory Status
getRuntimeAndRunGC(preRunGarbageCollector);
return "# Memory(MB): Total=" + getTotalMemory(false) + ", Used=" + getConsumedMemory(false) + ", Free="
        + getFreeMemory(false);
StringgetMemoryString()
Gets a String containing info of available and used memory of this JVM.
return "Mem Total in JVM: " + (Runtime.getRuntime().totalMemory() / FACTOR_MB) + " Free in JVM: "
        + (Runtime.getRuntime().freeMemory() / FACTOR_MB) + " Max Limit: "
        + (Runtime.getRuntime().maxMemory() / FACTOR_MB);
StringgetMemoryString()
get Memory String
Runtime rt = Runtime.getRuntime();
long free = rt.freeMemory();
long total = rt.totalMemory();
long max = rt.maxMemory();
long used = total - free;
long percent1 = used * 100L / total;
long percent2 = used * 100L / max;
return "Working memory: " + percent1 + "% (" + used + "/" + total + ")" + "  VM Max: " + percent2 + "% ("
...
floatgetMemoryUtilizationPercent()
Get the amount of memory used by the current JVM as a percentage of the maximum amount of memory it is allowed to use (via the -Xmx parameter)
final Runtime rt = Runtime.getRuntime();
final float usedMemory = rt.totalMemory() - rt.freeMemory();
return usedMemory / rt.maxMemory();