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 {
main(String args[])24     public static void main(String args[]) {
25         System.out.println("tests beginning");
26         testHugeArray();
27         testOomeLarge();
28         testOomeSmall();
29         testOomeToCharArray();
30         System.out.println("tests succeeded");
31     }
32 
testHugeArray()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 
testOomeLarge()44     private static void testOomeLarge() {
45         System.out.println("testOomeLarge beginning");
46 
47         Boolean sawEx = false;
48         byte[] a;
49 
50         try {
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];
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.
70      */
testOomeSmallInternal()71     private static boolean testOomeSmallInternal() {
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 {
82                 for (int i = 0; i < Runtime.getRuntime().maxMemory() / objSize; i++) {
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 
testOomeSmall()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     }
110 
testOomeToCharArray()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     }
127 }
128