1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2011 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Method; 18*795d594fSAndroid Build Coastguard Worker import java.util.ArrayList; 19*795d594fSAndroid Build Coastguard Worker import java.util.List; 20*795d594fSAndroid Build Coastguard Worker 21*795d594fSAndroid Build Coastguard Worker public class Main { 22*795d594fSAndroid Build Coastguard Worker main(String[] args)23*795d594fSAndroid Build Coastguard Worker public static void main(String[] args) throws Exception { 24*795d594fSAndroid Build Coastguard Worker // Setup reflection stuff before allocating to prevent OOME caused by allocations from 25*795d594fSAndroid Build Coastguard Worker // Class.forName or getDeclaredMethod. 26*795d594fSAndroid Build Coastguard Worker // Reflective equivalent of: dalvik.system.VMRuntime.getRuntime().clearGrowthLimit(); 27*795d594fSAndroid Build Coastguard Worker final Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime"); 28*795d594fSAndroid Build Coastguard Worker final Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime"); 29*795d594fSAndroid Build Coastguard Worker final Object runtime = get_runtime.invoke(null); 30*795d594fSAndroid Build Coastguard Worker final Method clear_growth_limit = vm_runtime.getDeclaredMethod("clearGrowthLimit"); 31*795d594fSAndroid Build Coastguard Worker 32*795d594fSAndroid Build Coastguard Worker int alloc1 = allocateTillOOME(); 33*795d594fSAndroid Build Coastguard Worker 34*795d594fSAndroid Build Coastguard Worker // Proactively clean up. 35*795d594fSAndroid Build Coastguard Worker Runtime.getRuntime().gc(); 36*795d594fSAndroid Build Coastguard Worker 37*795d594fSAndroid Build Coastguard Worker // Expand the heap to the maximum size. 38*795d594fSAndroid Build Coastguard Worker clear_growth_limit.invoke(runtime); 39*795d594fSAndroid Build Coastguard Worker 40*795d594fSAndroid Build Coastguard Worker int alloc2 = allocateTillOOME(); 41*795d594fSAndroid Build Coastguard Worker 42*795d594fSAndroid Build Coastguard Worker if (alloc1 > alloc2) { 43*795d594fSAndroid Build Coastguard Worker System.out.println("ERROR: Allocated less memory after growth" + 44*795d594fSAndroid Build Coastguard Worker "limit cleared (" + alloc1 + " MBs > " + alloc2 + " MBs"); 45*795d594fSAndroid Build Coastguard Worker } else { 46*795d594fSAndroid Build Coastguard Worker System.out.println("Test complete"); 47*795d594fSAndroid Build Coastguard Worker } 48*795d594fSAndroid Build Coastguard Worker } 49*795d594fSAndroid Build Coastguard Worker allocateTillOOME()50*795d594fSAndroid Build Coastguard Worker private static int allocateTillOOME() { 51*795d594fSAndroid Build Coastguard Worker int allocations = 0; 52*795d594fSAndroid Build Coastguard Worker List<byte[]> l = new ArrayList<byte[]>(); 53*795d594fSAndroid Build Coastguard Worker try { 54*795d594fSAndroid Build Coastguard Worker while (true) { 55*795d594fSAndroid Build Coastguard Worker // Allocate a MB at a time 56*795d594fSAndroid Build Coastguard Worker l.add(new byte[1048576]); 57*795d594fSAndroid Build Coastguard Worker allocations++; 58*795d594fSAndroid Build Coastguard Worker } 59*795d594fSAndroid Build Coastguard Worker } catch (OutOfMemoryError e) { 60*795d594fSAndroid Build Coastguard Worker // Help clean up. 61*795d594fSAndroid Build Coastguard Worker l.clear(); 62*795d594fSAndroid Build Coastguard Worker } 63*795d594fSAndroid Build Coastguard Worker return allocations; 64*795d594fSAndroid Build Coastguard Worker } 65*795d594fSAndroid Build Coastguard Worker } 66