1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2016 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 public class Main { exhaustJavaHeap(Object[] data, int index, int size)18*795d594fSAndroid Build Coastguard Worker private static int exhaustJavaHeap(Object[] data, int index, int size) { 19*795d594fSAndroid Build Coastguard Worker Runtime.getRuntime().gc(); 20*795d594fSAndroid Build Coastguard Worker while (size > 0) { 21*795d594fSAndroid Build Coastguard Worker try { 22*795d594fSAndroid Build Coastguard Worker data[index] = new byte[size]; 23*795d594fSAndroid Build Coastguard Worker index++; 24*795d594fSAndroid Build Coastguard Worker } catch (OutOfMemoryError e) { 25*795d594fSAndroid Build Coastguard Worker size /= 2; 26*795d594fSAndroid Build Coastguard Worker } 27*795d594fSAndroid Build Coastguard Worker } 28*795d594fSAndroid Build Coastguard Worker return index; 29*795d594fSAndroid Build Coastguard Worker } 30*795d594fSAndroid Build Coastguard Worker main(String[] args)31*795d594fSAndroid Build Coastguard Worker public static void main(String[] args) { 32*795d594fSAndroid Build Coastguard Worker Class klass = Other.class; 33*795d594fSAndroid Build Coastguard Worker Object[] data = new Object[100000]; 34*795d594fSAndroid Build Coastguard Worker try { 35*795d594fSAndroid Build Coastguard Worker System.out.println("Filling heap"); 36*795d594fSAndroid Build Coastguard Worker 37*795d594fSAndroid Build Coastguard Worker // Make sure that there is no reclaimable memory in the heap. Otherwise we may throw 38*795d594fSAndroid Build Coastguard Worker // OOME to prevent GC thrashing, even if later allocations may succeed. 39*795d594fSAndroid Build Coastguard Worker Runtime.getRuntime().gc(); 40*795d594fSAndroid Build Coastguard Worker System.runFinalization(); 41*795d594fSAndroid Build Coastguard Worker // NOTE: There is a GC invocation in the exhaustJavaHeap(). So we don't need one here. 42*795d594fSAndroid Build Coastguard Worker 43*795d594fSAndroid Build Coastguard Worker int index = 0; 44*795d594fSAndroid Build Coastguard Worker int initial_size = 256 * 1024 * 1024; 45*795d594fSAndroid Build Coastguard Worker // Repeat to ensure there is no space left on the heap. 46*795d594fSAndroid Build Coastguard Worker index = exhaustJavaHeap(data, index, initial_size); 47*795d594fSAndroid Build Coastguard Worker index = exhaustJavaHeap(data, index, /*size*/ 4); 48*795d594fSAndroid Build Coastguard Worker index = exhaustJavaHeap(data, index, /*size*/ 4); 49*795d594fSAndroid Build Coastguard Worker 50*795d594fSAndroid Build Coastguard Worker // Initialize now that the heap is full. 51*795d594fSAndroid Build Coastguard Worker Other.print(); 52*795d594fSAndroid Build Coastguard Worker } catch (OutOfMemoryError e) { 53*795d594fSAndroid Build Coastguard Worker } catch (Exception e) { 54*795d594fSAndroid Build Coastguard Worker System.out.println(e); 55*795d594fSAndroid Build Coastguard Worker } 56*795d594fSAndroid Build Coastguard Worker } 57*795d594fSAndroid Build Coastguard Worker } 58