blob: d31cbf1fe1aedb98178556610c4d3bc1dbdc1671 [file] [log] [blame]
jeffhaoc1160702011-10-27 15:48:45 -07001/*
2 * Copyright (C) 2011 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
Ian Rogersdd0c4b82013-01-11 09:02:55 -080017import java.lang.reflect.Method;
jeffhaoc1160702011-10-27 15:48:45 -070018import java.util.ArrayList;
jeffhaoc1160702011-10-27 15:48:45 -070019import java.util.List;
jeffhaoc1160702011-10-27 15:48:45 -070020
Ian Rogersdd0c4b82013-01-11 09:02:55 -080021public class Main {
jeffhaoc1160702011-10-27 15:48:45 -070022
23 public static void main(String[] args) throws Exception {
jeffhaoc1160702011-10-27 15:48:45 -070024 int alloc1 = 1;
Mathieu Chartier44fe8b32015-04-14 11:33:53 -070025 // Setup reflection stuff before allocating to prevent OOME caused by allocations from
26 // Class.forName or getDeclaredMethod.
27 // Reflective equivalent of: dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
28 final Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime");
29 final Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime");
30 final Object runtime = get_runtime.invoke(null);
31 final Method clear_growth_limit = vm_runtime.getDeclaredMethod("clearGrowthLimit");
Mathieu Chartiera61894d2015-04-23 16:32:54 -070032 List<byte[]> l = new ArrayList<byte[]>();
jeffhaoc1160702011-10-27 15:48:45 -070033 try {
jeffhaoc1160702011-10-27 15:48:45 -070034 while (true) {
35 // Allocate a MB at a time
36 l.add(new byte[1048576]);
37 alloc1++;
38 }
39 } catch (OutOfMemoryError e) {
Mathieu Chartiera61894d2015-04-23 16:32:54 -070040 l = null;
jeffhaoc1160702011-10-27 15:48:45 -070041 }
Ian Rogersdd0c4b82013-01-11 09:02:55 -080042 // Expand the heap to the maximum size.
Ian Rogersdd0c4b82013-01-11 09:02:55 -080043 clear_growth_limit.invoke(runtime);
jeffhaoc1160702011-10-27 15:48:45 -070044 int alloc2 = 1;
Mathieu Chartiera61894d2015-04-23 16:32:54 -070045 l = new ArrayList<byte[]>();
jeffhaoc1160702011-10-27 15:48:45 -070046 try {
jeffhaoc1160702011-10-27 15:48:45 -070047 while (true) {
48 // Allocate a MB at a time
49 l.add(new byte[1048576]);
50 alloc2++;
51 }
52 } catch (OutOfMemoryError e2) {
Mathieu Chartiera61894d2015-04-23 16:32:54 -070053 l = null;
jeffhaoc1160702011-10-27 15:48:45 -070054 if (alloc1 > alloc2) {
55 System.out.println("ERROR: Allocated less memory after growth" +
56 "limit cleared (" + alloc1 + " MBs > " + alloc2 + " MBs");
57 System.exit(1);
58 }
59 }
Ian Rogersdd0c4b82013-01-11 09:02:55 -080060 System.out.println("Test complete");
jeffhaoc1160702011-10-27 15:48:45 -070061 }
62}