1 /* 2 * Copyright (C) 2017 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.lang.reflect.InvocationHandler; 18 import java.lang.reflect.Method; 19 import java.lang.reflect.Proxy; 20 import java.util.ArrayList; 21 22 /** 23 * Ensure that one can dispatch without aborting when the heap is full. 24 */ 25 public class OOMEOnDispatch implements InvocationHandler { 26 27 static ArrayList<Object> storage = new ArrayList<>(100000); 28 main(String[] args)29 public static void main(String[] args) { 30 InvocationHandler handler = new OOMEOnDispatch(); 31 OOMEInterface inf = (OOMEInterface)Proxy.newProxyInstance( 32 OOMEInterface.class.getClassLoader(), new Class[] { OOMEInterface.class }, 33 handler); 34 35 // Stop the JIT to be sure nothing is running that could be resolving classes or causing 36 // verification. 37 Main.stopJit(); 38 Main.waitForCompilation(); 39 40 int l = 1024 * 1024; 41 while (l > 8) { 42 try { 43 storage.add(new byte[l]); 44 } catch (OutOfMemoryError e) { 45 l = l/2; 46 } 47 } 48 49 try { 50 inf.foo(); 51 storage.clear(); 52 System.out.println("Did not receive OOME!"); 53 } catch (OutOfMemoryError oome) { 54 storage.clear(); 55 System.out.println("Received OOME"); 56 } 57 58 Main.startJit(); 59 } 60 invoke(Object proxy, Method method, Object[] args)61 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 62 storage.clear(); 63 System.out.println("Should not have reached OOMEOnDispatch.invoke!"); 64 return null; 65 } 66 } 67 68 interface OOMEInterface { foo()69 public void foo(); 70 } 71