blob: 3ffe2f38aa4ecb4f6841f174298cf06feff95a6e [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) {
24 holder[i] = new char[128 * 1024];
25 }
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;
34
jeffhao5d1ac922011-09-29 17:41:15 -070035 InstanceMemEater next;
Elliott Hughesff9af222013-04-11 18:13:31 -070036 double d1, d2, d3, d4, d5, d6, d7, d8; // Bloat this object so we fill the heap faster.
jeffhao5d1ac922011-09-29 17:41:15 -070037
Elliott Hughesff9af222013-04-11 18:13:31 -070038 static InstanceMemEater allocate() {
jeffhao5d1ac922011-09-29 17:41:15 -070039 try {
Elliott Hughesff9af222013-04-11 18:13:31 -070040 return new InstanceMemEater();
jeffhao5d1ac922011-09-29 17:41:15 -070041 } catch (OutOfMemoryError e) {
Elliott Hughesff9af222013-04-11 18:13:31 -070042 InstanceMemEater.sawOome = true;
43 return null;
jeffhao5d1ac922011-09-29 17:41:15 -070044 }
jeffhao5d1ac922011-09-29 17:41:15 -070045 }
46
Elliott Hughesff9af222013-04-11 18:13:31 -070047 static void confuseCompilerOptimization(InstanceMemEater instance) {
jeffhao5d1ac922011-09-29 17:41:15 -070048 }
49 }
50
Elliott Hughesff9af222013-04-11 18:13:31 -070051 static boolean triggerArrayOOM() {
52 ArrayMemEater.blowup(new char[1 * 1024 * 1024][]);
53 return ArrayMemEater.sawOome;
jeffhao5d1ac922011-09-29 17:41:15 -070054 }
55
Elliott Hughesff9af222013-04-11 18:13:31 -070056 static boolean triggerInstanceOOM() {
57 InstanceMemEater memEater = InstanceMemEater.allocate();
jeffhao5d1ac922011-09-29 17:41:15 -070058 InstanceMemEater lastMemEater = memEater;
59 do {
Elliott Hughesff9af222013-04-11 18:13:31 -070060 lastMemEater.next = InstanceMemEater.allocate();
jeffhao5d1ac922011-09-29 17:41:15 -070061 lastMemEater = lastMemEater.next;
62 } while (lastMemEater != null);
63 memEater.confuseCompilerOptimization(memEater);
Elliott Hughesff9af222013-04-11 18:13:31 -070064 return InstanceMemEater.sawOome;
jeffhao5d1ac922011-09-29 17:41:15 -070065 }
66
67 public static void main(String[] args) {
Elliott Hughesff9af222013-04-11 18:13:31 -070068 if (triggerArrayOOM()) {
69 System.out.println("NEW_ARRAY correctly threw OOME");
70 }
71
72 if (triggerInstanceOOM()) {
73 System.out.println("NEW_INSTANCE correctly threw OOME");
74 }
jeffhao5d1ac922011-09-29 17:41:15 -070075 }
76}