view src/main/java/org/openjdk/gcbench/HeapSizeManager.java @ 49:d869414b90b4

Tunable min/max heap sizes. Default min heap size to 8192.
author shade
date Thu, 15 Dec 2016 19:57:37 +0100
parents src/main/java/org/openjdk/gcbench/MaxHeapDetector.java@25f22a0b9311
children eac18811b563
line wrap: on
line source

package org.openjdk.gcbench;

import org.openjdk.gcbench.util.Dummy;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.options.VerboseMode;

import java.io.PrintWriter;

public class HeapSizeManager {

    public static int MAX_HEAP;
    public static int MIN_HEAP;

    public static void override(int min, int max) {
        MAX_HEAP = max;
        MIN_HEAP = max;
    }

    public static void init(int min, PrintWriter pw) {
        int baseHeapMB = 1000;
        int latestSuccessMB = 0;
        boolean progress;
        do {
            progress = false;
            for (int incr = 1000; incr < Integer.MAX_VALUE; incr *= 2) {
                int heapGB = baseHeapMB + incr;
                pw.print(heapGB + "? ");
                pw.flush();
                Options opts = new OptionsBuilder()
                        .include(Dummy.class.getCanonicalName())
                        .threads(1)
                        .jvmArgsAppend("-Xmx" + heapGB + "m", "-Xms" + heapGB + "m", "-XX:+AlwaysPreTouch")
                        .verbosity(VerboseMode.SILENT)
                        .build();
                try {
                    new Runner(opts).runSingle();
                    latestSuccessMB = heapGB;
                    progress = true;
                } catch (RunnerException e) {
                    baseHeapMB = latestSuccessMB;
                    break;
                }
            }
        } while (progress);

        pw.println();
        pw.println("Max heap size is " + latestSuccessMB + " Mb");
        pw.println();

        MIN_HEAP = min;
        MAX_HEAP = latestSuccessMB;
    }

}