1 /* 2 * Copyright (C) 2016 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.util.TreeMap; 18 19 public class MovingGCThread implements Runnable { 20 private static TreeMap treeMap = new TreeMap(); 21 22 public void run() { 23 for (int i = 0; i < 20; i++) { 24 testHomogeneousCompaction(); 25 } 26 } 27 28 public static void testHomogeneousCompaction() { 29 final boolean supportHSC = supportHomogeneousSpaceCompact(); 30 if (!supportHSC) { 31 return; 32 } 33 Object o = new Object(); 34 long addressBefore = objectAddress(o); 35 allocateStuff(); 36 final boolean success = performHomogeneousSpaceCompact(); 37 allocateStuff(); 38 if (!success) { 39 System.out.println("error: Expected " + supportHSC + " but got " + success); 40 } 41 allocateStuff(); 42 long addressAfter = objectAddress(o); 43 // This relies on the compaction copying from one space to another space and there being 44 // no overlap. 45 if (addressBefore == addressAfter) { 46 System.out.println("error: Expected different adddress " + addressBefore + " vs " + 47 addressAfter); 48 } 49 } 50 51 private static void allocateStuff() { 52 for (int i = 0; i < 1000; ++i) { 53 Object o = new Object(); 54 treeMap.put(o.hashCode(), o); 55 } 56 } 57 58 // Methods to get access to ART internals. 59 private static native boolean supportHomogeneousSpaceCompact(); 60 private static native boolean performHomogeneousSpaceCompact(); 61 private static native long objectAddress(Object object); 62 } 63