import java.lang.management.*;

// java --enable-preview -Xmx200M -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -XX:CompileCommand=dontinline,*::*Callee -Xbatch -XX:-TieredCompilation TestBufferLost.java
public class TestBufferLost {

    // TODO we need more tests cases with more variants (virtual calls etc.)

    static value class MyValue {
        long a = 1;
        long b = 2;
        long c = 3;
        long d = 4;
        long e = 5;
    }

    static MyValue VAL = new MyValue();

    public static void test1Callee(MyValue val) {
        // This will buffer again if the scalarized arg does not contain the buffer oop
        VAL = val;
    }

    public static void test1() {
        test1Callee(VAL);
    }

    public static MyValue test2Callee() {
        return VAL;
    }

    public static void test2() {
        // This will buffer again if the scalarized return does not contain the buffer oop
        VAL = test2Callee();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10_000_000; ++i) {
            test1();
            test2();
        }

        MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
        System.out.println("Heap size: " + mem.getHeapMemoryUsage().getUsed() / (1024*1024) + " MB");
    }
}
