jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | import java.util.Arrays; |
| 18 | import java.util.LinkedList; |
| 19 | |
| 20 | /** |
| 21 | * Exercise the construction and throwing of OutOfMemoryError. |
| 22 | */ |
| 23 | public class Main { |
| 24 | public static void main(String args[]) { |
| 25 | System.out.println("tests beginning"); |
| 26 | testHugeArray(); |
| 27 | testOomeLarge(); |
| 28 | testOomeSmall(); |
Mathieu Chartier | 04e983a | 2015-11-13 08:36:59 -0800 | [diff] [blame] | 29 | testOomeToCharArray(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 30 | System.out.println("tests succeeded"); |
| 31 | } |
| 32 | |
| 33 | private static void testHugeArray() { |
| 34 | try { |
| 35 | final int COUNT = 32768*32768 + 4; |
| 36 | int[] tooBig = new int[COUNT]; |
| 37 | |
| 38 | Arrays.fill(tooBig, 0xdd); |
| 39 | } catch (OutOfMemoryError oom) { |
| 40 | System.out.println("Got expected huge-array OOM"); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | private static void testOomeLarge() { |
| 45 | System.out.println("testOomeLarge beginning"); |
| 46 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 47 | Boolean sawEx = false; |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 48 | byte[] a; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 49 | |
| 50 | try { |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 51 | // Just shy of the typical max heap size so that it will actually |
| 52 | // try to allocate it instead of short-circuiting. |
| 53 | a = new byte[(int) Runtime.getRuntime().maxMemory() - 32]; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 54 | } catch (OutOfMemoryError oom) { |
| 55 | //Log.i(TAG, "HeapTest/OomeLarge caught " + oom); |
| 56 | sawEx = true; |
| 57 | } |
| 58 | |
| 59 | if (!sawEx) { |
| 60 | throw new RuntimeException("Test failed: " + |
| 61 | "OutOfMemoryError not thrown"); |
| 62 | } |
| 63 | |
| 64 | System.out.println("testOomeLarge succeeded"); |
| 65 | } |
| 66 | |
| 67 | /* Do this in another method so that the GC has a chance of freeing the |
| 68 | * list afterwards. Even if we null out list when we're done, the conservative |
| 69 | * GC may see a stale pointer to it in a register. |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 70 | */ |
| 71 | private static boolean testOomeSmallInternal() { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 72 | final int LINK_SIZE = 6 * 4; // estimated size of a LinkedList's node |
| 73 | |
| 74 | LinkedList<Object> list = new LinkedList<Object>(); |
| 75 | |
| 76 | /* Allocate progressively smaller objects to fill up the entire heap. |
| 77 | */ |
| 78 | int objSize = 1 * 1024 * 1024; |
| 79 | while (objSize >= LINK_SIZE) { |
| 80 | boolean sawEx = false; |
| 81 | try { |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 82 | for (int i = 0; i < Runtime.getRuntime().maxMemory() / objSize; i++) { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 83 | list.add((Object)new byte[objSize]); |
| 84 | } |
| 85 | } catch (OutOfMemoryError oom) { |
| 86 | sawEx = true; |
| 87 | } |
| 88 | |
| 89 | if (!sawEx) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | objSize = (objSize * 4) / 5; |
| 94 | } |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | private static void testOomeSmall() { |
| 100 | System.out.println("testOomeSmall beginning"); |
| 101 | if (!testOomeSmallInternal()) { |
| 102 | /* Can't reliably throw this from inside the internal function, because |
| 103 | * we may not be able to allocate the RuntimeException. |
| 104 | */ |
| 105 | throw new RuntimeException("Test failed: " + |
| 106 | "OutOfMemoryError not thrown while filling heap"); |
| 107 | } |
| 108 | System.out.println("testOomeSmall succeeded"); |
| 109 | } |
Mathieu Chartier | 04e983a | 2015-11-13 08:36:59 -0800 | [diff] [blame] | 110 | |
| 111 | private static void testOomeToCharArray() { |
| 112 | Object[] o = new Object[2000000]; |
| 113 | String test = "test"; |
| 114 | int i = 0; |
| 115 | try { |
| 116 | for (; i < o.length; ++i) o[i] = new char[1000000]; |
| 117 | } catch (OutOfMemoryError oom) {} |
| 118 | try { |
| 119 | for (; i < o.length; ++i) { |
| 120 | o[i] = test.toCharArray(); |
| 121 | } |
| 122 | } catch (OutOfMemoryError oom) { |
| 123 | o = null; |
| 124 | System.out.println("Got expected toCharArray OOM"); |
| 125 | } |
| 126 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 127 | } |