15d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao/*
25d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Copyright (C) 2009 The Android Open Source Project
35d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao *
45d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Licensed under the Apache License, Version 2.0 (the "License");
55d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * you may not use this file except in compliance with the License.
65d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * You may obtain a copy of the License at
75d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao *
85d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao *      http://www.apache.org/licenses/LICENSE-2.0
95d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao *
105d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Unless required by applicable law or agreed to in writing, software
115d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * distributed under the License is distributed on an "AS IS" BASIS,
125d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * See the License for the specific language governing permissions and
145d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * limitations under the License.
155d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */
165d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
175d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaopublic class Main {
185d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    static class ArrayMemEater {
19ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes        static boolean sawOome;
20ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes
21ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes        static void blowup(char[][] holder) {
225d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            try {
23ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes                for (int i = 0; i < holder.length; ++i) {
2438c488bcd41ba632a646d7a1d790ec71a2fcf6faMathieu Chartier                    holder[i] = new char[1024 * 1024];
25ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes                }
265d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            } catch (OutOfMemoryError oome) {
27ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes                ArrayMemEater.sawOome = true;
285d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
295d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        }
305d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
315d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
325d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    static class InstanceMemEater {
33ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes        static boolean sawOome;
34ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes
355d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        InstanceMemEater next;
36ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes        double d1, d2, d3, d4, d5, d6, d7, d8; // Bloat this object so we fill the heap faster.
375d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
38ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes        static InstanceMemEater allocate() {
395d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            try {
40ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes                return new InstanceMemEater();
415d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            } catch (OutOfMemoryError e) {
42ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes                InstanceMemEater.sawOome = true;
43ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes                return null;
445d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
455d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        }
465d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
47ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes        static void confuseCompilerOptimization(InstanceMemEater instance) {
485d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        }
495d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
505d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
51ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes    static boolean triggerArrayOOM() {
5238c488bcd41ba632a646d7a1d790ec71a2fcf6faMathieu Chartier        ArrayMemEater.blowup(new char[128 * 1024][]);
53ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes        return ArrayMemEater.sawOome;
545d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
555d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
56ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes    static boolean triggerInstanceOOM() {
57ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes        InstanceMemEater memEater = InstanceMemEater.allocate();
585d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        InstanceMemEater lastMemEater = memEater;
595d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        do {
60ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes            lastMemEater.next = InstanceMemEater.allocate();
615d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            lastMemEater = lastMemEater.next;
625d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        } while (lastMemEater != null);
635d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        memEater.confuseCompilerOptimization(memEater);
64ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes        return InstanceMemEater.sawOome;
655d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
665d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
675d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public static void main(String[] args) {
68ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes        if (triggerArrayOOM()) {
69ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes            System.out.println("NEW_ARRAY correctly threw OOME");
70ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes        }
71ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes
72ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes        if (triggerInstanceOOM()) {
73ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes            System.out.println("NEW_INSTANCE correctly threw OOME");
74ff9af2220a9d0ef8e7c7f34448c6cfae144e7509Elliott Hughes        }
755d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
765d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao}
77