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 package art; 18 19 import java.lang.reflect.Field; 20 import java.util.Arrays; 21 22 public class Test920 { run()23 public static void run() throws Exception { 24 doTest(); 25 } 26 doTest()27 public static void doTest() throws Exception { 28 testObjectSize(new Object()); 29 testObjectSize(new Object()); 30 31 testObjectSize(new int[0]); 32 testObjectSize(new int[1]); 33 testObjectSize(new int[2]); 34 35 testObjectSize(new double[0]); 36 testObjectSize(new double[1]); 37 testObjectSize(new double[2]); 38 39 testObjectSize(new String("abc")); 40 testObjectSize(new String("wxyz")); 41 42 testObjectHash(); 43 } 44 testObjectSize(Object o)45 private static void testObjectSize(Object o) { 46 System.out.println(o.getClass() + " " + getObjectSize(o)); 47 } 48 testObjectHash()49 private static void testObjectHash() { 50 Object[] objects = new Object[] { 51 new Object(), 52 new Object(), 53 54 new MyHash(1), 55 new MyHash(1), 56 new MyHash(2) 57 }; 58 59 int hashes[] = new int[objects.length]; 60 61 for (int i = 0; i < objects.length; i++) { 62 hashes[i] = getObjectHashCode(objects[i]); 63 } 64 65 // Implementation detail: we use the identity hashcode, for simplicity. 66 for (int i = 0; i < objects.length; i++) { 67 int ihash = System.identityHashCode(objects[i]); 68 if (hashes[i] != ihash) { 69 throw new RuntimeException(objects[i] + ": " + hashes[i] + " vs " + ihash); 70 } 71 } 72 73 Runtime.getRuntime().gc(); 74 Runtime.getRuntime().gc(); 75 76 for (int i = 0; i < objects.length; i++) { 77 int newhash = getObjectHashCode(objects[i]); 78 if (hashes[i] != newhash) { 79 throw new RuntimeException(objects[i] + ": " + hashes[i] + " vs " + newhash); 80 } 81 } 82 } 83 getObjectSize(Object o)84 private static native long getObjectSize(Object o); getObjectHashCode(Object o)85 private static native int getObjectHashCode(Object o); 86 87 private static class MyHash { 88 private int hash; 89 MyHash(int h)90 public MyHash(int h) { 91 hash = h; 92 } 93 hashCode()94 public int hashCode() { 95 return hash; 96 } 97 } 98 } 99