Java Memory simpleMemory()

Here you can find the source of simpleMemory()

Description

Return the memory in a simple format.

License

LGPL

Return

The simple memory.

Declaration

public static String simpleMemory() 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**/*  ww w.  j a  va2  s  . c o  m*/
     * Return the memory in a simple format.
     * The format is used memory/total memory/max memory.
     * Each memory is formatted using {@link #toSimpleBytes(long)}.
     * 
     * @return The simple memory.
     * @author shinobu
     * @since RELEASE6-0
     */
    public static String simpleMemory() {
        StringBuilder memory = new StringBuilder();

        Runtime runtime = Runtime.getRuntime();
        long maxMemory = runtime.maxMemory();
        long totalMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();
        long usedMemory = totalMemory - freeMemory;

        memory.append(toSimpleBytes(usedMemory));
        memory.append("/");
        memory.append(toSimpleBytes(totalMemory));
        memory.append("/");
        memory.append(toSimpleBytes(maxMemory));

        return memory.toString();
    }

    /**
     * Formats bytes to a simple format.
     * The number of bytes will be suffixed by "B", "MB", "GB".
     * The number will always be 0 ~ 2048.
     * 
     * @param bytes The bytes.
     * @return The simple bytes.
     * @author shinobu
     * @since RELEASE6-0
     */
    private static String toSimpleBytes(long bytes) {
        String suffix = "B";
        if (bytes > 2048) {
            bytes /= 1024;
            suffix = "KB";

            if (bytes > 2048) {
                bytes /= 1024;
                suffix = "MB";

                if (bytes > 2048) {
                    bytes /= 1024;
                    suffix = "GB";
                }
            }
        }

        return String.format("%1$d%2$s", bytes, suffix);
    }
}

Related

  1. normalizeMemoryMeasure(String memory)
  2. percentMemoryFull()
  3. removeSliceFromMemory(String schemaName, String cubeName, String loadName)
  4. reportMemory()
  5. reportMemory(double val)
  6. testMemory(long bytesToTest)
  7. throwOutOfMemoryError()
  8. unlimitMemory()
  9. waitForMemory(double memory)