2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 import java.util.Arrays;
18 import java.util.LinkedList;
21 * Exercise the construction and throwing of OutOfMemoryError.
24 public static void main(String args[]) {
25 System.out.println("tests beginning");
29 System.out.println("tests succeeded");
32 private static void testHugeArray() {
34 final int COUNT = 32768*32768 + 4;
35 int[] tooBig = new int[COUNT];
37 Arrays.fill(tooBig, 0xdd);
38 } catch (OutOfMemoryError oom) {
39 System.out.println("Got expected huge-array OOM");
43 private static void testOomeLarge() {
44 System.out.println("testOomeLarge beginning");
46 /* Just shy of the typical max heap size so that it will actually
47 * try to allocate it instead of short-circuiting.
49 * TODO: stop assuming the VM defaults to 16MB max
51 final int SIXTEEN_MB = (16 * 1024 * 1024 - 32);
53 Boolean sawEx = false;
57 a = new byte[SIXTEEN_MB];
58 } catch (OutOfMemoryError oom) {
59 //Log.i(TAG, "HeapTest/OomeLarge caught " + oom);
64 throw new RuntimeException("Test failed: " +
65 "OutOfMemoryError not thrown");
68 System.out.println("testOomeLarge succeeded");
71 /* Do this in another method so that the GC has a chance of freeing the
72 * list afterwards. Even if we null out list when we're done, the conservative
73 * GC may see a stale pointer to it in a register.
75 * TODO: stop assuming the VM defaults to 16MB max
77 private static boolean testOomeSmallInternal() {
78 final int SIXTEEN_MB = (16 * 1024 * 1024);
79 final int LINK_SIZE = 6 * 4; // estimated size of a LinkedList's node
81 LinkedList<Object> list = new LinkedList<Object>();
83 /* Allocate progressively smaller objects to fill up the entire heap.
85 int objSize = 1 * 1024 * 1024;
86 while (objSize >= LINK_SIZE) {
87 boolean sawEx = false;
89 for (int i = 0; i < SIXTEEN_MB / objSize; i++) {
90 list.add((Object)new byte[objSize]);
92 } catch (OutOfMemoryError oom) {
100 objSize = (objSize * 4) / 5;
106 private static void testOomeSmall() {
107 System.out.println("testOomeSmall beginning");
108 if (!testOomeSmallInternal()) {
109 /* Can't reliably throw this from inside the internal function, because
110 * we may not be able to allocate the RuntimeException.
112 throw new RuntimeException("Test failed: " +
113 "OutOfMemoryError not thrown while filling heap");
115 System.out.println("testOomeSmall succeeded");