1.1 --- a/test/gc/g1/TestStringDeduplicationTools.java Sat Apr 05 23:38:24 2014 -0700 1.2 +++ b/test/gc/g1/TestStringDeduplicationTools.java Thu Apr 03 10:39:27 2014 +0200 1.3 @@ -310,7 +310,9 @@ 1.4 } 1.5 1.6 System.gc(); 1.7 + 1.8 System.out.println("Heap Memory Usage: " + ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed()); 1.9 + System.out.println("Array Header Size: " + unsafe.ARRAY_CHAR_BASE_OFFSET); 1.10 1.11 System.out.println("End: MemoryUsageTest"); 1.12 } 1.13 @@ -482,31 +484,40 @@ 1.14 public static void testMemoryUsage() throws Exception { 1.15 // Test that memory usage is reduced after deduplication 1.16 OutputAnalyzer output; 1.17 - final String usagePattern = "Heap Memory Usage: (\\d+)"; 1.18 + final String heapMemoryUsagePattern = "Heap Memory Usage: (\\d+)"; 1.19 + final String arrayHeaderSizePattern = "Array Header Size: (\\d+)"; 1.20 1.21 // Run without deduplication 1.22 output = MemoryUsageTest.run(false); 1.23 output.shouldHaveExitValue(0); 1.24 - final long memoryUsageWithoutDedup = Long.parseLong(output.firstMatch(usagePattern, 1)); 1.25 + final long heapMemoryUsageWithoutDedup = Long.parseLong(output.firstMatch(heapMemoryUsagePattern, 1)); 1.26 + final long arrayHeaderSizeWithoutDedup = Long.parseLong(output.firstMatch(arrayHeaderSizePattern, 1)); 1.27 1.28 // Run with deduplication 1.29 output = MemoryUsageTest.run(true); 1.30 output.shouldHaveExitValue(0); 1.31 - final long memoryUsageWithDedup = Long.parseLong(output.firstMatch(usagePattern, 1)); 1.32 + final long heapMemoryUsageWithDedup = Long.parseLong(output.firstMatch(heapMemoryUsagePattern, 1)); 1.33 + final long arrayHeaderSizeWithDedup = Long.parseLong(output.firstMatch(arrayHeaderSizePattern, 1)); 1.34 + 1.35 + // Sanity check to make sure one instance isn't using compressed class pointers and the other not 1.36 + if (arrayHeaderSizeWithoutDedup != arrayHeaderSizeWithDedup) { 1.37 + throw new Exception("Unexpected difference between array header sizes"); 1.38 + } 1.39 1.40 // Calculate expected memory usage with deduplication enabled. This calculation does 1.41 // not take alignment and padding into account, so it's a conservative estimate. 1.42 - final long sizeOfChar = 2; // bytes 1.43 - final long bytesSaved = (LargeNumberOfStrings - 1) * (StringLength * sizeOfChar + unsafe.ARRAY_CHAR_BASE_OFFSET); 1.44 - final long memoryUsageWithDedupExpected = memoryUsageWithoutDedup - bytesSaved; 1.45 + final long sizeOfChar = unsafe.ARRAY_CHAR_INDEX_SCALE; 1.46 + final long sizeOfCharArray = StringLength * sizeOfChar + arrayHeaderSizeWithoutDedup; 1.47 + final long bytesSaved = (LargeNumberOfStrings - 1) * sizeOfCharArray; 1.48 + final long heapMemoryUsageWithDedupExpected = heapMemoryUsageWithoutDedup - bytesSaved; 1.49 1.50 System.out.println("Memory usage summary:"); 1.51 - System.out.println(" memoryUsageWithoutDedup: " + memoryUsageWithoutDedup); 1.52 - System.out.println(" memoryUsageWithDedup: " + memoryUsageWithDedup); 1.53 - System.out.println(" memoryUsageWithDedupExpected: " + memoryUsageWithDedupExpected); 1.54 + System.out.println(" heapMemoryUsageWithoutDedup: " + heapMemoryUsageWithoutDedup); 1.55 + System.out.println(" heapMemoryUsageWithDedup: " + heapMemoryUsageWithDedup); 1.56 + System.out.println(" heapMemoryUsageWithDedupExpected: " + heapMemoryUsageWithDedupExpected); 1.57 1.58 - if (memoryUsageWithDedup > memoryUsageWithDedupExpected) { 1.59 - throw new Exception("Unexpected memory usage, memoryUsageWithDedup should less or equal to memoryUsageWithDedupExpected"); 1.60 + if (heapMemoryUsageWithDedup > heapMemoryUsageWithDedupExpected) { 1.61 + throw new Exception("Unexpected memory usage, heapMemoryUsageWithDedup should be less or equal to heapMemoryUsageWithDedupExpected"); 1.62 } 1.63 } 1.64 }