Calculate total, free & used memory of JVM heap store – Java runtime (example)

  • Given a java program, where we are allocating memory in heap store.
    • We will allocate array (array of long primitives).
  • We will analyze (or calculate) the memory consumption of JVM heap store.
    • We will use java runtime to get JVM total memory, free memory and used memory.

1. Program flow to analyze total memory, free & used memory.

  1. Calculate total memory, used memory & free memory using java runtime.
  2. Allocate array of longs.
    • We have allocate big enough buffer, so that we can easily make out the difference in memory consumption.
  3. AgainĀ calculate total memory, used memory & free memory using java runtime.
    • Used memory should be increased.
  4. Allocate array of integers.
  5. Perform step 1 again.
    • Used memory will increase.
  6. Clean up the both arrays (Step 2 & Step 4) and request GC to collect the memory.
  7. Perform Step 1,

2. Get total memory, free memory & used memory in java (JVM heap store).

package org.learn;

public class JVMHeapStore {

    public static void main(String[] args) {
        long totalMemory ;
        long freeMemory;
        long usedMemory;
        int MB = 1024 *1024 ;

        Runtime runtime = Runtime.getRuntime();
        runtime.gc();
        totalMemory = runtime.totalMemory() / MB;
        freeMemory = runtime.freeMemory() / MB;
        usedMemory = totalMemory - freeMemory;

        System.out.printf("1. Total memory=%dMB, Free memory=%dMB, Used memory=%dMB\n",
                                totalMemory,freeMemory,usedMemory);

        System.out.println("2. Allocating space for 1000000 longs");
        long[] arrLong = new long[1000000];
        for(int index = 0; index < arrLong.length;index++) {
            arrLong[index] = index;
        }
        totalMemory = runtime.totalMemory() / MB;
        freeMemory = runtime.freeMemory() / MB;
        usedMemory = totalMemory - freeMemory;

        System.out.printf("3. Total memory=%dMB, Free memory=%dMB, Used memory=%dMB",
                totalMemory,freeMemory,usedMemory);

        System.out.println("\n4. Allocating space for another 1000000 longs");
        long[] anotherLongArray = new long[1000000];
        for(int index = 0; index < anotherLongArray.length;index++) {
            anotherLongArray[index] = index;
        }

        totalMemory = runtime.totalMemory() / MB;
        freeMemory = runtime.freeMemory() / MB;
        usedMemory = totalMemory - freeMemory;

        System.out.printf("5. Total memory=%dMB, Free memory=%dMB, Used memory=%dMB",
                totalMemory,freeMemory,usedMemory);


        System.out.println("\n6. Invalidate array's memory & request GC to collect");
        //clean up both arrays
        anotherLongArray = null;
        arrLong = null;

        //Run gc collect
        runtime.gc();

        totalMemory = runtime.totalMemory() / MB;
        freeMemory = runtime.freeMemory() / MB;
        usedMemory = totalMemory - freeMemory;

        System.out.printf("7. Total memory=%dMB, Free memory=%dMB, Used memory=%dMB",
                totalMemory,freeMemory,usedMemory);
    }
}

Output: total memory, free & used memory of java application (JVM).

1. Total memory=241MB, Free memory=241MB, Used memory=0MB
2. Allocating space for 1000000 longs
3. Total memory=241MB, Free memory=229MB, Used memory=12MB
4. Allocating space for another 1000000 longs
5. Total memory=241MB, Free memory=222MB, Used memory=19MB
6. Invalidate array's memory & request GC to collect
7. Total memory=241MB, Free memory=241MB, Used memory=0MB
Scroll to Top