xref: /aosp_15_r20/art/test/044-proxy/src/OOMEOnDispatch.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2017 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.InvocationHandler;
18*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Method;
19*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Proxy;
20*795d594fSAndroid Build Coastguard Worker import java.util.ArrayList;
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker /**
23*795d594fSAndroid Build Coastguard Worker  * Ensure that one can dispatch without aborting when the heap is full.
24*795d594fSAndroid Build Coastguard Worker  */
25*795d594fSAndroid Build Coastguard Worker public class OOMEOnDispatch implements InvocationHandler {
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker     static ArrayList<Object> storage = new ArrayList<>(100000);
28*795d594fSAndroid Build Coastguard Worker 
exhaustJavaHeap(int size)29*795d594fSAndroid Build Coastguard Worker     private static void exhaustJavaHeap(int size) {
30*795d594fSAndroid Build Coastguard Worker       Runtime.getRuntime().gc();
31*795d594fSAndroid Build Coastguard Worker       while (size > 0) {
32*795d594fSAndroid Build Coastguard Worker         try {
33*795d594fSAndroid Build Coastguard Worker           storage.add(new byte[size]);
34*795d594fSAndroid Build Coastguard Worker         } catch (OutOfMemoryError e) {
35*795d594fSAndroid Build Coastguard Worker           size = size/2;
36*795d594fSAndroid Build Coastguard Worker         }
37*795d594fSAndroid Build Coastguard Worker       }
38*795d594fSAndroid Build Coastguard Worker     }
39*795d594fSAndroid Build Coastguard Worker 
main(String[] args)40*795d594fSAndroid Build Coastguard Worker     public static void main(String[] args) {
41*795d594fSAndroid Build Coastguard Worker         InvocationHandler handler = new OOMEOnDispatch();
42*795d594fSAndroid Build Coastguard Worker         OOMEInterface inf = (OOMEInterface)Proxy.newProxyInstance(
43*795d594fSAndroid Build Coastguard Worker                 OOMEInterface.class.getClassLoader(), new Class[] { OOMEInterface.class },
44*795d594fSAndroid Build Coastguard Worker                 handler);
45*795d594fSAndroid Build Coastguard Worker 
46*795d594fSAndroid Build Coastguard Worker         // Stop the JIT to be sure nothing is running that could be resolving classes or causing
47*795d594fSAndroid Build Coastguard Worker         // verification.
48*795d594fSAndroid Build Coastguard Worker         Main.stopJit();
49*795d594fSAndroid Build Coastguard Worker         Main.waitForCompilation();
50*795d594fSAndroid Build Coastguard Worker 
51*795d594fSAndroid Build Coastguard Worker         // Make sure that there is no reclaimable memory in the heap. Otherwise we may throw
52*795d594fSAndroid Build Coastguard Worker         // OOME to prevent GC thrashing, even if later allocations may succeed.
53*795d594fSAndroid Build Coastguard Worker         Runtime.getRuntime().gc();
54*795d594fSAndroid Build Coastguard Worker         System.runFinalization();
55*795d594fSAndroid Build Coastguard Worker         // NOTE: There is a GC invocation in the exhaustJavaHeap(). So we don't need one here.
56*795d594fSAndroid Build Coastguard Worker 
57*795d594fSAndroid Build Coastguard Worker         int initial_size = 1024 * 1024;
58*795d594fSAndroid Build Coastguard Worker         // Repeat to ensure there is no space left on the heap.
59*795d594fSAndroid Build Coastguard Worker         exhaustJavaHeap(initial_size);
60*795d594fSAndroid Build Coastguard Worker         exhaustJavaHeap(/*size*/ 4);
61*795d594fSAndroid Build Coastguard Worker         exhaustJavaHeap(/*size*/ 4);
62*795d594fSAndroid Build Coastguard Worker 
63*795d594fSAndroid Build Coastguard Worker         try {
64*795d594fSAndroid Build Coastguard Worker             inf.foo();
65*795d594fSAndroid Build Coastguard Worker             storage.clear();
66*795d594fSAndroid Build Coastguard Worker             System.out.println("Did not receive OOME!");
67*795d594fSAndroid Build Coastguard Worker         } catch (OutOfMemoryError oome) {
68*795d594fSAndroid Build Coastguard Worker             storage.clear();
69*795d594fSAndroid Build Coastguard Worker             System.out.println("Received OOME");
70*795d594fSAndroid Build Coastguard Worker         }
71*795d594fSAndroid Build Coastguard Worker 
72*795d594fSAndroid Build Coastguard Worker         Main.startJit();
73*795d594fSAndroid Build Coastguard Worker     }
74*795d594fSAndroid Build Coastguard Worker 
invoke(Object proxy, Method method, Object[] args)75*795d594fSAndroid Build Coastguard Worker     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
76*795d594fSAndroid Build Coastguard Worker         storage.clear();
77*795d594fSAndroid Build Coastguard Worker         System.out.println("Should not have reached OOMEOnDispatch.invoke!");
78*795d594fSAndroid Build Coastguard Worker         return null;
79*795d594fSAndroid Build Coastguard Worker     }
80*795d594fSAndroid Build Coastguard Worker }
81*795d594fSAndroid Build Coastguard Worker 
82*795d594fSAndroid Build Coastguard Worker interface OOMEInterface {
foo()83*795d594fSAndroid Build Coastguard Worker     public void foo();
84*795d594fSAndroid Build Coastguard Worker }
85