31 * @bug 4892507 32 * @summary Basic Test for MemoryPool.resetPeakUsage() 33 * @author Mandy Chung 34 * 35 * @requires vm.opt.ExplicitGCInvokesConcurrent != "true" 36 * @requires vm.opt.DisableExplicitGC != "true" 37 * @library /lib/testlibrary/ /test/lib 38 * @modules jdk.management 39 * 40 * @build jdk.testlibrary.* ResetPeakMemoryUsage MemoryUtil RunUtil 41 * @build sun.hotspot.WhiteBox 42 * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission 43 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. ResetPeakMemoryUsage 44 */ 45 46 import java.lang.management.*; 47 import java.lang.ref.WeakReference; 48 import java.util.*; 49 50 import sun.hotspot.code.Compiler; 51 52 public class ResetPeakMemoryUsage { 53 private static MemoryMXBean mbean = ManagementFactory.getMemoryMXBean(); 54 // make public so that it can't be optimized away easily 55 public static Object[] obj; 56 57 /** 58 * Run the test multiple times with different GC versions. 59 * First with default command line specified by the framework. 60 * Then with all GC versions specified by the test. 61 */ 62 public static void main(String a[]) throws Throwable { 63 final String main = "ResetPeakMemoryUsage$TestMain"; 64 final String ms = "-Xms256m"; 65 final String mn = "-Xmn8m"; 66 if (!Compiler.isGraalEnabled()) { // Graal does not support CMS 67 RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseConcMarkSweepGC"); 68 } 69 RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseParallelGC"); 70 RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseG1GC", "-XX:G1HeapRegionSize=1m"); 71 RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseSerialGC", 72 "-XX:MarkSweepAlwaysCompactCount=1"); 73 } 74 75 private static class TestMain { 76 public static void main(String[] argv) { 77 List pools = ManagementFactory.getMemoryPoolMXBeans(); 78 ListIterator iter = pools.listIterator(); 79 boolean found = false; 80 while (iter.hasNext()) { 81 MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next(); 82 // only check heap pools that support usage threshold 83 // this is typically only the old generation space 84 // since the other spaces are expected to get filled up 85 if (p.getType() == MemoryType.HEAP && 86 p.isUsageThresholdSupported()) 87 { 88 found = true; 89 testPool(p); 90 } 91 } 92 if (!found) { 93 throw new RuntimeException("No heap pool found"); 94 } 95 } 96 } 97 98 private static void testPool(MemoryPoolMXBean mpool) { 99 System.out.println("Selected memory pool: "); 100 MemoryUtil.printMemoryPool(mpool); 101 102 MemoryUsage usage0 = mpool.getUsage(); 103 MemoryUsage peak0 = mpool.getPeakUsage(); 104 105 // use a size that is larger than the young generation and G1 regions 106 // to force the array into the old gen 107 int largeArraySize = 9 * 1000 * 1000; 108 109 System.out.println("Before big object array (of size "+largeArraySize+") is allocated: "); 110 printMemoryUsage(usage0, peak0); 111 114 115 MemoryUsage usage1 = mpool.getUsage(); 116 MemoryUsage peak1 = mpool.getPeakUsage(); 117 System.out.println("After the object is allocated: "); 118 printMemoryUsage(usage1, peak1); 119 120 if (usage1.getUsed() <= usage0.getUsed()) { 121 throw new RuntimeException( 122 formatSize("Before allocation: used", usage0.getUsed()) + 123 " expected to be < " + 124 formatSize("After allocation: used", usage1.getUsed())); 125 } 126 127 if (peak1.getUsed() <= peak0.getUsed()) { 128 throw new RuntimeException( 129 formatSize("Before allocation: peak", peak0.getUsed()) + 130 " expected to be < " + 131 formatSize("After allocation: peak", peak1.getUsed())); 132 } 133 134 135 // The object is now garbage and do a GC 136 // memory usage should drop 137 obj = null; 138 139 //This will cause sure shot GC unlike Runtime.gc() invoked by mbean.gc() 140 while(weakRef.get() != null) { 141 mbean.gc(); 142 } 143 144 MemoryUsage usage2 = mpool.getUsage(); 145 MemoryUsage peak2 = mpool.getPeakUsage(); 146 System.out.println("After GC: "); 147 printMemoryUsage(usage2, peak2); 148 149 if (usage2.getUsed() >= usage1.getUsed()) { 150 throw new RuntimeException( 151 formatSize("Before GC: used", usage1.getUsed()) + " " + 152 " expected to be > " + 153 formatSize("After GC: used", usage2.getUsed())); 154 } 155 156 mpool.resetPeakUsage(); 157 158 MemoryUsage usage3 = mpool.getUsage(); 159 MemoryUsage peak3 = mpool.getPeakUsage(); 160 System.out.println("After resetPeakUsage: "); | 31 * @bug 4892507 32 * @summary Basic Test for MemoryPool.resetPeakUsage() 33 * @author Mandy Chung 34 * 35 * @requires vm.opt.ExplicitGCInvokesConcurrent != "true" 36 * @requires vm.opt.DisableExplicitGC != "true" 37 * @library /lib/testlibrary/ /test/lib 38 * @modules jdk.management 39 * 40 * @build jdk.testlibrary.* ResetPeakMemoryUsage MemoryUtil RunUtil 41 * @build sun.hotspot.WhiteBox 42 * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission 43 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. ResetPeakMemoryUsage 44 */ 45 46 import java.lang.management.*; 47 import java.lang.ref.WeakReference; 48 import java.util.*; 49 50 import sun.hotspot.code.Compiler; 51 import sun.hotspot.WhiteBox; 52 53 public class ResetPeakMemoryUsage { 54 private static MemoryMXBean mbean = ManagementFactory.getMemoryMXBean(); 55 // make public so that it can't be optimized away easily 56 public static Object[] obj; 57 58 /** 59 * Run the test multiple times with different GC versions. 60 * First with default command line specified by the framework. 61 * Then with all GC versions specified by the test. 62 */ 63 public static void main(String a[]) throws Throwable { 64 final String main = "ResetPeakMemoryUsage$TestMain"; 65 final String ms = "-Xms256m"; 66 final String mn = "-Xmn8m"; 67 if (!Compiler.isGraalEnabled()) { // Graal does not support CMS 68 RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseConcMarkSweepGC"); 69 } 70 RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseParallelGC"); 71 RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseG1GC", "-XX:-G1UseLegacyMonitoring", 72 "-XX:G1HeapRegionSize=1m"); 73 RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseG1GC", "-XX:+G1UseLegacyMonitoring", 74 "-XX:G1HeapRegionSize=1m"); 75 RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseSerialGC", 76 "-XX:MarkSweepAlwaysCompactCount=1"); 77 } 78 79 private static class TestMain { 80 public static void main(String[] argv) { 81 List pools = ManagementFactory.getMemoryPoolMXBeans(); 82 ListIterator iter = pools.listIterator(); 83 boolean found = false; 84 while (iter.hasNext()) { 85 MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next(); 86 // only check heap pools that support usage threshold 87 // this is typically only the old generation space 88 // since the other spaces are expected to get filled up 89 if (p.getType() == MemoryType.HEAP && 90 p.isUsageThresholdSupported()) { 91 if (!p.getName().equals("G1 Old Space")) { 92 // With G1, humongous objects are tracked in the old space 93 // only in legacy monitoring mode. In normal mode, G1 tracks 94 // humongous objects in the humongous space, which latter also 95 // supports a usage threshold, and will thus by tested instead 96 // of the old space. 97 found = true; 98 testPool(p); 99 } 100 } 101 } 102 if (!found) { 103 throw new RuntimeException("No heap pool found"); 104 } 105 } 106 } 107 108 private static void testPool(MemoryPoolMXBean mpool) { 109 System.out.println("Selected memory pool: "); 110 MemoryUtil.printMemoryPool(mpool); 111 112 MemoryUsage usage0 = mpool.getUsage(); 113 MemoryUsage peak0 = mpool.getPeakUsage(); 114 115 // use a size that is larger than the young generation and G1 regions 116 // to force the array into the old gen 117 int largeArraySize = 9 * 1000 * 1000; 118 119 System.out.println("Before big object array (of size "+largeArraySize+") is allocated: "); 120 printMemoryUsage(usage0, peak0); 121 124 125 MemoryUsage usage1 = mpool.getUsage(); 126 MemoryUsage peak1 = mpool.getPeakUsage(); 127 System.out.println("After the object is allocated: "); 128 printMemoryUsage(usage1, peak1); 129 130 if (usage1.getUsed() <= usage0.getUsed()) { 131 throw new RuntimeException( 132 formatSize("Before allocation: used", usage0.getUsed()) + 133 " expected to be < " + 134 formatSize("After allocation: used", usage1.getUsed())); 135 } 136 137 if (peak1.getUsed() <= peak0.getUsed()) { 138 throw new RuntimeException( 139 formatSize("Before allocation: peak", peak0.getUsed()) + 140 " expected to be < " + 141 formatSize("After allocation: peak", peak1.getUsed())); 142 } 143 144 // The object is now garbage and do a GC. Memory usage should drop. 145 obj = null; 146 147 // This will cause a sure shot GC unlike Runtime.gc() invoked by mbean.gc() 148 while (weakRef.get() != null) { 149 mbean.gc(); 150 } 151 152 MemoryUsage usage2 = mpool.getUsage(); 153 MemoryUsage peak2 = mpool.getPeakUsage(); 154 System.out.println("After GC: "); 155 printMemoryUsage(usage2, peak2); 156 157 if (usage2.getUsed() >= usage1.getUsed()) { 158 throw new RuntimeException( 159 formatSize("Before GC: used", usage1.getUsed()) + " " + 160 " expected to be > " + 161 formatSize("After GC: used", usage2.getUsed())); 162 } 163 164 mpool.resetPeakUsage(); 165 166 MemoryUsage usage3 = mpool.getUsage(); 167 MemoryUsage peak3 = mpool.getPeakUsage(); 168 System.out.println("After resetPeakUsage: "); |