Programming And Stuff, You Know The Thing…

Java HotSpot VM Memory Footprint Optimization

Posted at — Mar 20, 2013

Here we show some ways to reduce the memory footprint of your JVM regarding your OS, ie. the amount of physical RAM committed to the jvm.

There is, however, one caveat: due to the nature of garbage collection in Java, there is no way to honor the MaxHeapFreeRatio parameter in a time-reliably manner. If a heap has grown in size and the jvm idles afterwards memory-wise, the OS memory commitment to the JVM may never change – except if you run System.gc(), for example through JMX or using an internal scheduler thread in your Java app. Here is our little benchmark program:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class GcReleaseToOsTest {

    static List<byte[]> a = new ArrayList<>();
    static Random r = new Random();

    public static void main(String[] args) throws InterruptedException {
        for (long i = 0; i < Long.parseLong(args[0]); i++) {
            a.add(new byte[1024 * 1024]);
        for (long i = 0; i < Long.parseLong(args[1]); i++) {
        long endtime = System.currentTimeMillis() + Long.parseLong(args[2]) * 1000L;
        long n = 0;
        while (System.currentTimeMillis() < endtime) {
            a.add(new byte[1024 * 1024]);

The Java version used for testing: