Prints the memory usage as used by various commands. - Java java.lang

Java examples for java.lang:Runtime

Description

Prints the memory usage as used by various commands.

Demo Code


import java.io.PrintStream;

public class Main{
    /**/*from   w  w w .  j  a  v  a 2s.  co  m*/
     * Prints the memory usage as used by various commands.
     *
     * @param out the stream
     * @return the free memory
     */
    public static long printMemoryUsage(PrintStream out) {
        Runtime runtime = Runtime.getRuntime();

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

        out.printf("Free Memory:       %13s\n",
                Utils.formatBytes(freeMemory));
        out.printf("Used Memory:       %13s\n",
                Utils.formatBytes(usedMemory));
        out.printf("Total Memory:      %13s\n",
                Utils.formatBytes(totalMemory));
        out.printf("Maximum Memory:    %13s\n",
                Utils.formatBytes(maxMemory));
        out.printf("Number of Threads: %,8d     \n", Thread.activeCount());

        return freeMemory;
    }
}

Related Tutorials