1 /* 2 * Copyright (C) 2018 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 dalvik.system.VMRuntime; 18 19 public class Main { 20 21 public static void main(String[] args) throws Exception { 22 VMRuntime runtime = VMRuntime.getRuntime(); 23 24 try { 25 int N = 1024 * 1024; 26 int S = 512; 27 for (int n = 0; n < N; ++n) { 28 // Allocate unreachable objects. 29 $noinline$Alloc(runtime); 30 // Allocate an object with a substantial size to increase memory 31 // pressure and eventually trigger non-explicit garbage collection 32 // (explicit garbage collections triggered by java.lang.Runtime.gc() 33 // are always full GCs). Upon garbage collection, the objects 34 // allocated in $noinline$Alloc used to trigger a crash. 35 Object[] moving_array = new Object[S]; 36 } 37 } catch (OutOfMemoryError e) { 38 // Stop here. 39 } 40 System.out.println("passed"); 41 } 42 43 // When using the Concurrent Copying (CC) collector (default collector), 44 // this method allocates an object in the non-moving space and an object 45 // in the region space, make the former reference the later, and returns 46 // nothing (so that none of these objects are reachable when upon return). 47 static void $noinline$Alloc(VMRuntime runtime) { 48 Object[] non_moving_array = (Object[]) runtime.newNonMovableArray(Object.class, 1); 49 // Small object, unlikely to trigger garbage collection. 50 non_moving_array[0] = new Object(); 51 } 52 53 } 54