jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | public class Main { |
| 18 | static class ArrayMemEater { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame^] | 19 | static boolean sawOome; |
| 20 | |
| 21 | static void blowup(char[][] holder) { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 22 | try { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame^] | 23 | for (int i = 0; i < holder.length; ++i) { |
| 24 | holder[i] = new char[128 * 1024]; |
| 25 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 26 | } catch (OutOfMemoryError oome) { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame^] | 27 | ArrayMemEater.sawOome = true; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 28 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 29 | } |
| 30 | } |
| 31 | |
| 32 | static class InstanceMemEater { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame^] | 33 | static boolean sawOome; |
| 34 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 35 | InstanceMemEater next; |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame^] | 36 | double d1, d2, d3, d4, d5, d6, d7, d8; // Bloat this object so we fill the heap faster. |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame^] | 38 | static InstanceMemEater allocate() { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 39 | try { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame^] | 40 | return new InstanceMemEater(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 41 | } catch (OutOfMemoryError e) { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame^] | 42 | InstanceMemEater.sawOome = true; |
| 43 | return null; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 44 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame^] | 47 | static void confuseCompilerOptimization(InstanceMemEater instance) { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame^] | 51 | static boolean triggerArrayOOM() { |
| 52 | ArrayMemEater.blowup(new char[1 * 1024 * 1024][]); |
| 53 | return ArrayMemEater.sawOome; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame^] | 56 | static boolean triggerInstanceOOM() { |
| 57 | InstanceMemEater memEater = InstanceMemEater.allocate(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 58 | InstanceMemEater lastMemEater = memEater; |
| 59 | do { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame^] | 60 | lastMemEater.next = InstanceMemEater.allocate(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 61 | lastMemEater = lastMemEater.next; |
| 62 | } while (lastMemEater != null); |
| 63 | memEater.confuseCompilerOptimization(memEater); |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame^] | 64 | return InstanceMemEater.sawOome; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | public static void main(String[] args) { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame^] | 68 | 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 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 75 | } |
| 76 | } |