blob: c93f8bbc542b64a22253f12fb02f0c3c244df885 [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17public class Main {
18 static class ArrayMemEater {
Elliott Hughesff9af222013-04-11 18:13:31 -070019 static boolean sawOome;
20
21 static void blowup(char[][] holder) {
jeffhao5d1ac922011-09-29 17:41:15 -070022 try {
Elliott Hughesff9af222013-04-11 18:13:31 -070023 for (int i = 0; i < holder.length; ++i) {
Mathieu Chartierb363f662014-07-16 13:28:58 -070024 holder[i] = new char[1024 * 1024];
Elliott Hughesff9af222013-04-11 18:13:31 -070025 }
jeffhao5d1ac922011-09-29 17:41:15 -070026 } catch (OutOfMemoryError oome) {
Elliott Hughesff9af222013-04-11 18:13:31 -070027 ArrayMemEater.sawOome = true;
jeffhao5d1ac922011-09-29 17:41:15 -070028 }
jeffhao5d1ac922011-09-29 17:41:15 -070029 }
30 }
31
32 static class InstanceMemEater {
Elliott Hughesff9af222013-04-11 18:13:31 -070033 static boolean sawOome;
Mark Mendell8ed2e702014-08-18 22:19:06 -040034 static InstanceMemEater hook;
Elliott Hughesff9af222013-04-11 18:13:31 -070035
jeffhao5d1ac922011-09-29 17:41:15 -070036 InstanceMemEater next;
Elliott Hughesff9af222013-04-11 18:13:31 -070037 double d1, d2, d3, d4, d5, d6, d7, d8; // Bloat this object so we fill the heap faster.
jeffhao5d1ac922011-09-29 17:41:15 -070038
Elliott Hughesff9af222013-04-11 18:13:31 -070039 static InstanceMemEater allocate() {
jeffhao5d1ac922011-09-29 17:41:15 -070040 try {
Elliott Hughesff9af222013-04-11 18:13:31 -070041 return new InstanceMemEater();
jeffhao5d1ac922011-09-29 17:41:15 -070042 } catch (OutOfMemoryError e) {
Elliott Hughesff9af222013-04-11 18:13:31 -070043 InstanceMemEater.sawOome = true;
44 return null;
jeffhao5d1ac922011-09-29 17:41:15 -070045 }
jeffhao5d1ac922011-09-29 17:41:15 -070046 }
47
Elliott Hughesff9af222013-04-11 18:13:31 -070048 static void confuseCompilerOptimization(InstanceMemEater instance) {
Mark Mendell8ed2e702014-08-18 22:19:06 -040049 hook = instance;
jeffhao5d1ac922011-09-29 17:41:15 -070050 }
51 }
52
Elliott Hughesff9af222013-04-11 18:13:31 -070053 static boolean triggerArrayOOM() {
Mathieu Chartierb363f662014-07-16 13:28:58 -070054 ArrayMemEater.blowup(new char[128 * 1024][]);
Elliott Hughesff9af222013-04-11 18:13:31 -070055 return ArrayMemEater.sawOome;
jeffhao5d1ac922011-09-29 17:41:15 -070056 }
57
Elliott Hughesff9af222013-04-11 18:13:31 -070058 static boolean triggerInstanceOOM() {
59 InstanceMemEater memEater = InstanceMemEater.allocate();
jeffhao5d1ac922011-09-29 17:41:15 -070060 InstanceMemEater lastMemEater = memEater;
61 do {
Elliott Hughesff9af222013-04-11 18:13:31 -070062 lastMemEater.next = InstanceMemEater.allocate();
jeffhao5d1ac922011-09-29 17:41:15 -070063 lastMemEater = lastMemEater.next;
64 } while (lastMemEater != null);
65 memEater.confuseCompilerOptimization(memEater);
Mark Mendell8ed2e702014-08-18 22:19:06 -040066 InstanceMemEater.hook = null;
Elliott Hughesff9af222013-04-11 18:13:31 -070067 return InstanceMemEater.sawOome;
jeffhao5d1ac922011-09-29 17:41:15 -070068 }
69
70 public static void main(String[] args) {
Elliott Hughesff9af222013-04-11 18:13:31 -070071 if (triggerArrayOOM()) {
72 System.out.println("NEW_ARRAY correctly threw OOME");
73 }
74
75 if (triggerInstanceOOM()) {
76 System.out.println("NEW_INSTANCE correctly threw OOME");
77 }
jeffhao5d1ac922011-09-29 17:41:15 -070078 }
79}