blob: 63c5215210e4daaef449e297c413b50eeb4857be [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 {
Dmitry Petrochenko6d7729d2014-10-01 13:31:58 +070018 static char [][] holder;
19
jeffhao5d1ac922011-09-29 17:41:15 -070020 static class ArrayMemEater {
Elliott Hughesff9af222013-04-11 18:13:31 -070021 static boolean sawOome;
22
23 static void blowup(char[][] holder) {
jeffhao5d1ac922011-09-29 17:41:15 -070024 try {
Elliott Hughesff9af222013-04-11 18:13:31 -070025 for (int i = 0; i < holder.length; ++i) {
Dmitry Petrochenko6d7729d2014-10-01 13:31:58 +070026 holder[i] = new char[1022 * 1024];
Elliott Hughesff9af222013-04-11 18:13:31 -070027 }
jeffhao5d1ac922011-09-29 17:41:15 -070028 } catch (OutOfMemoryError oome) {
Elliott Hughesff9af222013-04-11 18:13:31 -070029 ArrayMemEater.sawOome = true;
jeffhao5d1ac922011-09-29 17:41:15 -070030 }
jeffhao5d1ac922011-09-29 17:41:15 -070031 }
32 }
33
34 static class InstanceMemEater {
Elliott Hughesff9af222013-04-11 18:13:31 -070035 static boolean sawOome;
Mark Mendell8ed2e702014-08-18 22:19:06 -040036 static InstanceMemEater hook;
Elliott Hughesff9af222013-04-11 18:13:31 -070037
jeffhao5d1ac922011-09-29 17:41:15 -070038 InstanceMemEater next;
Elliott Hughesff9af222013-04-11 18:13:31 -070039 double d1, d2, d3, d4, d5, d6, d7, d8; // Bloat this object so we fill the heap faster.
jeffhao5d1ac922011-09-29 17:41:15 -070040
Elliott Hughesff9af222013-04-11 18:13:31 -070041 static InstanceMemEater allocate() {
jeffhao5d1ac922011-09-29 17:41:15 -070042 try {
Elliott Hughesff9af222013-04-11 18:13:31 -070043 return new InstanceMemEater();
jeffhao5d1ac922011-09-29 17:41:15 -070044 } catch (OutOfMemoryError e) {
Elliott Hughesff9af222013-04-11 18:13:31 -070045 InstanceMemEater.sawOome = true;
46 return null;
jeffhao5d1ac922011-09-29 17:41:15 -070047 }
jeffhao5d1ac922011-09-29 17:41:15 -070048 }
49
Elliott Hughesff9af222013-04-11 18:13:31 -070050 static void confuseCompilerOptimization(InstanceMemEater instance) {
Mark Mendell8ed2e702014-08-18 22:19:06 -040051 hook = instance;
jeffhao5d1ac922011-09-29 17:41:15 -070052 }
53 }
54
Dmitry Petrochenko6d7729d2014-10-01 13:31:58 +070055 static class InstanceFinalizerMemEater {
56 static boolean sawOome;
57 static InstanceFinalizerMemEater hook;
58
59 InstanceFinalizerMemEater next;
60
61 static InstanceFinalizerMemEater allocate() {
62 try {
63 return new InstanceFinalizerMemEater();
64 } catch (OutOfMemoryError e) {
65 InstanceFinalizerMemEater.sawOome = true;
66 return null;
67 }
68 }
69
70 static void confuseCompilerOptimization(InstanceFinalizerMemEater instance) {
71 hook = instance;
72 }
73
74 protected void finalize() {}
75 }
76
77 static boolean triggerArrayOOM(char[][] holder) {
78 ArrayMemEater.blowup(holder);
Elliott Hughesff9af222013-04-11 18:13:31 -070079 return ArrayMemEater.sawOome;
jeffhao5d1ac922011-09-29 17:41:15 -070080 }
81
Elliott Hughesff9af222013-04-11 18:13:31 -070082 static boolean triggerInstanceOOM() {
83 InstanceMemEater memEater = InstanceMemEater.allocate();
jeffhao5d1ac922011-09-29 17:41:15 -070084 InstanceMemEater lastMemEater = memEater;
85 do {
Elliott Hughesff9af222013-04-11 18:13:31 -070086 lastMemEater.next = InstanceMemEater.allocate();
jeffhao5d1ac922011-09-29 17:41:15 -070087 lastMemEater = lastMemEater.next;
88 } while (lastMemEater != null);
89 memEater.confuseCompilerOptimization(memEater);
Mark Mendell8ed2e702014-08-18 22:19:06 -040090 InstanceMemEater.hook = null;
Elliott Hughesff9af222013-04-11 18:13:31 -070091 return InstanceMemEater.sawOome;
jeffhao5d1ac922011-09-29 17:41:15 -070092 }
93
Dmitry Petrochenko6d7729d2014-10-01 13:31:58 +070094 static boolean triggerInstanceFinalizerOOM() {
95 InstanceFinalizerMemEater memEater = InstanceFinalizerMemEater.allocate();
96 InstanceFinalizerMemEater lastMemEater = memEater;
97 do {
98 lastMemEater.next = InstanceFinalizerMemEater.allocate();
99 lastMemEater = lastMemEater.next;
100 } while (lastMemEater != null);
101 memEater.confuseCompilerOptimization(memEater);
102 InstanceFinalizerMemEater.hook = null;
103 return InstanceFinalizerMemEater.sawOome;
104 }
105
jeffhao5d1ac922011-09-29 17:41:15 -0700106 public static void main(String[] args) {
Dmitry Petrochenko6d7729d2014-10-01 13:31:58 +0700107 // Keep holder alive to make instance OOM happen faster
108 holder = new char[128 * 1024][];
109 if (triggerArrayOOM(holder)) {
Elliott Hughesff9af222013-04-11 18:13:31 -0700110 System.out.println("NEW_ARRAY correctly threw OOME");
111 }
112
Dmitry Petrochenko6d7729d2014-10-01 13:31:58 +0700113 if (!triggerInstanceFinalizerOOM()) {
114 System.out.println("NEW_INSTANCE (finalize) did not threw OOME");
115 }
116
Elliott Hughesff9af222013-04-11 18:13:31 -0700117 if (triggerInstanceOOM()) {
118 System.out.println("NEW_INSTANCE correctly threw OOME");
119 }
jeffhao5d1ac922011-09-29 17:41:15 -0700120 }
121}