blob: bda978eb2c792e1df39c550b64a1e5ecbaf218b0 [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001/*
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
17import java.util.Arrays;
18import java.util.LinkedList;
19
20/**
21 * Exercise the construction and throwing of OutOfMemoryError.
22 */
23public class Main {
24 public static void main(String args[]) {
25 System.out.println("tests beginning");
26 testHugeArray();
27 testOomeLarge();
28 testOomeSmall();
Mathieu Chartier04e983a2015-11-13 08:36:59 -080029 testOomeToCharArray();
jeffhao5d1ac922011-09-29 17:41:15 -070030 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
jeffhao5d1ac922011-09-29 17:41:15 -070047 Boolean sawEx = false;
Elliott Hughes741b5b72012-01-31 19:18:51 -080048 byte[] a;
jeffhao5d1ac922011-09-29 17:41:15 -070049
50 try {
Elliott Hughes741b5b72012-01-31 19:18:51 -080051 // 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];
jeffhao5d1ac922011-09-29 17:41:15 -070054 } 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.
jeffhao5d1ac922011-09-29 17:41:15 -070070 */
71 private static boolean testOomeSmallInternal() {
jeffhao5d1ac922011-09-29 17:41:15 -070072 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 Hughes741b5b72012-01-31 19:18:51 -080082 for (int i = 0; i < Runtime.getRuntime().maxMemory() / objSize; i++) {
jeffhao5d1ac922011-09-29 17:41:15 -070083 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 Chartier04e983a2015-11-13 08:36:59 -0800110
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 }
jeffhao5d1ac922011-09-29 17:41:15 -0700127}