Pacific-Design.com

    
Home Index

1. Linux

2. Memory Check

+ hilite.me

Linux / Memory Check /

Memory Info for JVM


import java.util.ArrayList;

public class MemoryUtil {

    private static final int MegaBytes = 10241024;

    public static void main(String args[]) {

        long freeMemory  = Runtime.getRuntime().freeMemory()  / MegaBytes;
        long totalMemory = Runtime.getRuntime().totalMemory() / MegaBytes;
        long maxMemory   = Runtime.getRuntime().maxMemory()   / MegaBytes;

        System.out.println("------------------------------------------");
        System.out.println("Free Memory JVM           = " + freeMemory);
        System.out.println("Heap size of JVM          = " + totalMemory);
        System.out.println("Maximum heap size of JVM  = " + maxMemory);

        ArrayList objects = new ArrayList();

        for (int i = 0; i < 10000000; i++) {
            objects.add(("" + 10 * 2710));
        }

        freeMemory = Runtime.getRuntime().freeMemory()   / MegaBytes;
        totalMemory = Runtime.getRuntime().totalMemory() / MegaBytes;
        maxMemory = Runtime.getRuntime().maxMemory()     / MegaBytes;

        System.out.println("------------------------------------------");
        System.out.println("Used Memory in JVM        = " + (maxMemory - freeMemory));
        System.out.println("Free Memory in JVM        = " + freeMemory);
        System.out.println("Current size of java heap = " + totalMemory);
        System.out.println("maxMemory in JVM          = " + maxMemory);
        System.out.println("------------------------------------------");

    }
}