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) { |
Nicolas Geoffray | 74d6a82 | 2014-10-03 10:54:19 +0000 | [diff] [blame^] | 24 | holder[i] = new char[1024 * 1024]; |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 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; |
Mark Mendell | 8ed2e70 | 2014-08-18 22:19:06 -0400 | [diff] [blame] | 34 | static InstanceMemEater hook; |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 35 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 36 | InstanceMemEater next; |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 37 | 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] | 38 | |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 39 | static InstanceMemEater allocate() { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 40 | try { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 41 | return new InstanceMemEater(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 42 | } catch (OutOfMemoryError e) { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 43 | InstanceMemEater.sawOome = true; |
| 44 | return null; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 45 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 48 | static void confuseCompilerOptimization(InstanceMemEater instance) { |
Mark Mendell | 8ed2e70 | 2014-08-18 22:19:06 -0400 | [diff] [blame] | 49 | hook = instance; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Nicolas Geoffray | 74d6a82 | 2014-10-03 10:54:19 +0000 | [diff] [blame^] | 53 | static boolean triggerArrayOOM() { |
| 54 | ArrayMemEater.blowup(new char[128 * 1024][]); |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 55 | return ArrayMemEater.sawOome; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 58 | static boolean triggerInstanceOOM() { |
| 59 | InstanceMemEater memEater = InstanceMemEater.allocate(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 60 | InstanceMemEater lastMemEater = memEater; |
| 61 | do { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 62 | lastMemEater.next = InstanceMemEater.allocate(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 63 | lastMemEater = lastMemEater.next; |
| 64 | } while (lastMemEater != null); |
| 65 | memEater.confuseCompilerOptimization(memEater); |
Mark Mendell | 8ed2e70 | 2014-08-18 22:19:06 -0400 | [diff] [blame] | 66 | InstanceMemEater.hook = null; |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 67 | return InstanceMemEater.sawOome; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | public static void main(String[] args) { |
Nicolas Geoffray | 74d6a82 | 2014-10-03 10:54:19 +0000 | [diff] [blame^] | 71 | if (triggerArrayOOM()) { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 72 | System.out.println("NEW_ARRAY correctly threw OOME"); |
| 73 | } |
| 74 | |
| 75 | if (triggerInstanceOOM()) { |
| 76 | System.out.println("NEW_INSTANCE correctly threw OOME"); |
| 77 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 78 | } |
| 79 | } |