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 { |
Dmitry Petrochenko | 6d7729d | 2014-10-01 13:31:58 +0700 | [diff] [blame^] | 18 | static char [][] holder; |
| 19 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 20 | static class ArrayMemEater { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 21 | static boolean sawOome; |
| 22 | |
| 23 | static void blowup(char[][] holder) { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 24 | try { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 25 | for (int i = 0; i < holder.length; ++i) { |
Dmitry Petrochenko | 6d7729d | 2014-10-01 13:31:58 +0700 | [diff] [blame^] | 26 | holder[i] = new char[1022 * 1024]; |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 27 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 28 | } catch (OutOfMemoryError oome) { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 29 | ArrayMemEater.sawOome = true; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 30 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 31 | } |
| 32 | } |
| 33 | |
| 34 | static class InstanceMemEater { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 35 | static boolean sawOome; |
Mark Mendell | 8ed2e70 | 2014-08-18 22:19:06 -0400 | [diff] [blame] | 36 | static InstanceMemEater hook; |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 37 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 38 | InstanceMemEater next; |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 39 | 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] | 40 | |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 41 | static InstanceMemEater allocate() { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 42 | try { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 43 | return new InstanceMemEater(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 44 | } catch (OutOfMemoryError e) { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 45 | InstanceMemEater.sawOome = true; |
| 46 | return null; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 47 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 50 | static void confuseCompilerOptimization(InstanceMemEater instance) { |
Mark Mendell | 8ed2e70 | 2014-08-18 22:19:06 -0400 | [diff] [blame] | 51 | hook = instance; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Dmitry Petrochenko | 6d7729d | 2014-10-01 13:31:58 +0700 | [diff] [blame^] | 55 | 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 Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 79 | return ArrayMemEater.sawOome; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 82 | static boolean triggerInstanceOOM() { |
| 83 | InstanceMemEater memEater = InstanceMemEater.allocate(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 84 | InstanceMemEater lastMemEater = memEater; |
| 85 | do { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 86 | lastMemEater.next = InstanceMemEater.allocate(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 87 | lastMemEater = lastMemEater.next; |
| 88 | } while (lastMemEater != null); |
| 89 | memEater.confuseCompilerOptimization(memEater); |
Mark Mendell | 8ed2e70 | 2014-08-18 22:19:06 -0400 | [diff] [blame] | 90 | InstanceMemEater.hook = null; |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 91 | return InstanceMemEater.sawOome; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Dmitry Petrochenko | 6d7729d | 2014-10-01 13:31:58 +0700 | [diff] [blame^] | 94 | 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 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 106 | public static void main(String[] args) { |
Dmitry Petrochenko | 6d7729d | 2014-10-01 13:31:58 +0700 | [diff] [blame^] | 107 | // Keep holder alive to make instance OOM happen faster |
| 108 | holder = new char[128 * 1024][]; |
| 109 | if (triggerArrayOOM(holder)) { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 110 | System.out.println("NEW_ARRAY correctly threw OOME"); |
| 111 | } |
| 112 | |
Dmitry Petrochenko | 6d7729d | 2014-10-01 13:31:58 +0700 | [diff] [blame^] | 113 | if (!triggerInstanceFinalizerOOM()) { |
| 114 | System.out.println("NEW_INSTANCE (finalize) did not threw OOME"); |
| 115 | } |
| 116 | |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 117 | if (triggerInstanceOOM()) { |
| 118 | System.out.println("NEW_INSTANCE correctly threw OOME"); |
| 119 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 120 | } |
| 121 | } |