1 /* 2 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 package org.graalvm.util.test; 26 27 import static org.junit.Assert.assertEquals; 28 29 import jdk.internal.vm.compiler.collections.EconomicMap; 30 import jdk.internal.vm.compiler.collections.Equivalence; 31 import org.graalvm.compiler.serviceprovider.JavaVersionUtil; 32 import org.graalvm.util.ObjectSizeEstimate; 33 import org.junit.Assume; 34 import org.junit.Test; 35 36 public class CollectionSizeTest { 37 38 /** 39 * Tests the memory size of an empty map and a map with only one or two entries. 40 */ 41 @Test 42 public void testSize() { 43 Assume.assumeTrue("Not working in JDK9 due to module visibility.", JavaVersionUtil.JAVA_SPEC <= 8); 44 EconomicMap<Object, Object> map = EconomicMap.create(Equivalence.IDENTITY); 45 assertEquals(49, ObjectSizeEstimate.forObject(map).getTotalBytes()); 46 47 Integer value = 1; 48 map.put(value, value); 49 assertEquals(153, ObjectSizeEstimate.forObject(map).getTotalBytes()); 50 51 Integer secondValue = 2; 52 map.put(secondValue, secondValue); 53 assertEquals(153 + 20, ObjectSizeEstimate.forObject(map).getTotalBytes()); 54 } 55 56 /** 57 * Tests whether the map actually compresses the entries array when a large number of entries 58 * are deleted. 59 */ 60 @Test 61 public void testCompress() { 62 Assume.assumeTrue("Not working in JDK9 due to module visibility.", JavaVersionUtil.JAVA_SPEC <= 8); 63 EconomicMap<Object, Object> map = EconomicMap.create(); 64 65 // Measuring size of map with one entry. 66 Object firstValue = 0; 67 map.put(firstValue, firstValue); 68 ObjectSizeEstimate afterFirstValue = ObjectSizeEstimate.forObject(map); 69 70 // Add 999 more entries. 71 for (int i = 1; i < 1000; ++i) { 72 Object value = i; 73 map.put(value, value); 74 } 75 ObjectSizeEstimate beforeRemove = ObjectSizeEstimate.forObject(map); 76 77 // Remove 999 first entries. 78 for (int i = 0; i < 999; ++i) { 79 map.removeKey(i); 80 } 81 ObjectSizeEstimate afterRemove = ObjectSizeEstimate.forObject(map); 82 83 // Check that size is same size as with one entry. 84 assertEquals(afterFirstValue, afterRemove); 85 86 // Add 999 new entries. 87 for (int i = 0; i < 999; ++i) { 88 Object value = i; 89 map.put(value, value); 90 } 91 ObjectSizeEstimate afterAdd = ObjectSizeEstimate.forObject(map); 92 93 // Check that entries array is same size again. 94 assertEquals(beforeRemove.getPointerCount(), afterAdd.getPointerCount()); 95 } 96 97 }