1 /* 2 * Copyright (c) 2003, 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 * The -XX:MarkSweepAlwaysCompactCount=1 argument below makes sure serial gc 26 * compacts the heap at every full gc so that the usage is correctly updated. 27 */ 28 29 /* 30 * @test 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 /test/lib 38 * @modules jdk.management 39 * 40 * @build 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 112 obj = new Object[largeArraySize]; 113 WeakReference<Object> weakRef = new WeakReference<>(obj); 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: "); 161 printMemoryUsage(usage3, peak3); 162 163 if (peak3.getUsed() != usage3.getUsed()) { 164 throw new RuntimeException( 165 formatSize("After resetting peak: peak", peak3.getUsed()) + " " + 166 " expected to be equal to " + 167 formatSize("current used", usage3.getUsed())); 168 } 169 170 if (peak3.getUsed() >= peak2.getUsed()) { 171 throw new RuntimeException( 172 formatSize("After resetting peak: peak", peak3.getUsed()) + " " + 173 " expected to be < " + 174 formatSize("previous peak", peak2.getUsed())); 175 } 176 177 System.out.println(RunUtil.successMessage); 178 } 179 180 private static String INDENT = " "; 181 private static void printMemoryUsage(MemoryUsage current, MemoryUsage peak) { 182 System.out.println("Current Usage: "); 183 MemoryUtil.printMemoryUsage(current); 184 System.out.println("Peak Usage: "); 185 MemoryUtil.printMemoryUsage(peak); 186 187 } 188 private static String formatSize(String name, long value) { 189 StringBuffer buf = new StringBuffer(name + " = " + value); 190 if (value > 0) { 191 buf.append(" (" + (value >> 10) + "K)"); 192 } 193 return buf.toString(); 194 } 195 }