import java.util.ArrayList;

public class StressAdd {

    public static void main(String argv[]) {
        int initial = 0;
        if (argv.length > 0) {
            initial = Integer.parseInt(argv[0]);
        }
        try {
            test(initial, Integer.MAX_VALUE);
        } catch (OutOfMemoryError e) {
            // This handler should only be reached if the callee unwound the frame because of
            // problems rematerializing EA objects
            assert e.getMessage().equals("Java heap space: failed reallocation of scalar replaced objects") : e;
            throw e;
        }
    }

    public static void test(int initial, int limit) {
        ArrayList<Short> list = new ArrayList<Short>(initial);
        try {
            for (int j = 0; j < limit; j++) {
                Short t = (short) j;
                list.add(t);
                // System.err.println(j);
            }
        } catch (OutOfMemoryError e) {
            list = null;
            System.out.println("OOM");
        }
    }
}
