1 /* 2 * Copyright (c) 2003, 2016, 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.ExplicitGCInvokesConcurrentAndUnloadsClasses != "true" 37 * @requires vm.opt.DisableExplicitGC != "true" 38 * @library /lib/testlibrary/ 39 * @modules jdk.management 40 * 41 * @build jdk.testlibrary.* ResetPeakMemoryUsage MemoryUtil RunUtil 42 * @run main ResetPeakMemoryUsage 43 */ 44 45 import java.lang.management.*; 46 import java.lang.ref.WeakReference; 47 import java.util.*; 48 49 public class ResetPeakMemoryUsage { 50 private static MemoryMXBean mbean = ManagementFactory.getMemoryMXBean(); 51 // make public so that it can't be optimized away easily 52 public static Object[] obj; 53 54 /** 55 * Run the test multiple times with different GC versions. 56 * First with default command line specified by the framework. 57 * Then with all GC versions specified by the test. 58 */ 59 public static void main(String a[]) throws Throwable { 60 final String main = "ResetPeakMemoryUsage$TestMain"; 61 final String ms = "-Xms256m"; 62 final String mn = "-Xmn8m"; 63 RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseConcMarkSweepGC"); 64 RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseParallelGC"); 65 RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseG1GC", "-XX:G1HeapRegionSize=1m"); 66 RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseSerialGC", 67 "-XX:MarkSweepAlwaysCompactCount=1"); 68 } 69 70 private static class TestMain { 71 public static void main(String[] argv) { 72 List pools = ManagementFactory.getMemoryPoolMXBeans(); 73 ListIterator iter = pools.listIterator(); 74 boolean found = false; 75 while (iter.hasNext()) { 76 MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next(); 77 // only check heap pools that support usage threshold 78 // this is typically only the old generation space 79 // since the other spaces are expected to get filled up 80 if (p.getType() == MemoryType.HEAP && 81 p.isUsageThresholdSupported()) 82 { 83 found = true; 84 testPool(p); 85 } 86 } 87 if (!found) { 88 throw new RuntimeException("No heap pool found"); 89 } 90 } 91 } 92 93 private static void testPool(MemoryPoolMXBean mpool) { 94 System.out.println("Selected memory pool: "); 95 MemoryUtil.printMemoryPool(mpool); 96 97 MemoryUsage usage0 = mpool.getUsage(); 98 MemoryUsage peak0 = mpool.getPeakUsage(); 99 100 // use a size that is larger than the young generation and G1 regions 101 // to force the array into the old gen 102 int largeArraySize = 9 * 1000 * 1000; 103 104 System.out.println("Before big object array (of size "+largeArraySize+") is allocated: "); 105 printMemoryUsage(usage0, peak0); 106 107 obj = new Object[largeArraySize]; 108 WeakReference<Object> weakRef = new WeakReference<>(obj); 109 110 MemoryUsage usage1 = mpool.getUsage(); 111 MemoryUsage peak1 = mpool.getPeakUsage(); 112 System.out.println("After the object is allocated: "); 113 printMemoryUsage(usage1, peak1); 114 115 if (usage1.getUsed() <= usage0.getUsed()) { 116 throw new RuntimeException( 117 formatSize("Before allocation: used", usage0.getUsed()) + 118 " expected to be < " + 119 formatSize("After allocation: used", usage1.getUsed())); 120 } 121 122 if (peak1.getUsed() <= peak0.getUsed()) { 123 throw new RuntimeException( 124 formatSize("Before allocation: peak", peak0.getUsed()) + 125 " expected to be < " + 126 formatSize("After allocation: peak", peak1.getUsed())); 127 } 128 129 130 // The object is now garbage and do a GC 131 // memory usage should drop 132 obj = null; 133 134 //This will cause sure shot GC unlike Runtime.gc() invoked by mbean.gc() 135 while(weakRef.get() != null) { 136 mbean.gc(); 137 } 138 139 MemoryUsage usage2 = mpool.getUsage(); 140 MemoryUsage peak2 = mpool.getPeakUsage(); 141 System.out.println("After GC: "); 142 printMemoryUsage(usage2, peak2); 143 144 if (usage2.getUsed() >= usage1.getUsed()) { 145 throw new RuntimeException( 146 formatSize("Before GC: used", usage1.getUsed()) + " " + 147 " expected to be > " + 148 formatSize("After GC: used", usage2.getUsed())); 149 } 150 151 mpool.resetPeakUsage(); 152 153 MemoryUsage usage3 = mpool.getUsage(); 154 MemoryUsage peak3 = mpool.getPeakUsage(); 155 System.out.println("After resetPeakUsage: "); 156 printMemoryUsage(usage3, peak3); 157 158 if (peak3.getUsed() != usage3.getUsed()) { 159 throw new RuntimeException( 160 formatSize("After resetting peak: peak", peak3.getUsed()) + " " + 161 " expected to be equal to " + 162 formatSize("current used", usage3.getUsed())); 163 } 164 165 if (peak3.getUsed() >= peak2.getUsed()) { 166 throw new RuntimeException( 167 formatSize("After resetting peak: peak", peak3.getUsed()) + " " + 168 " expected to be < " + 169 formatSize("previous peak", peak2.getUsed())); 170 } 171 172 System.out.println(RunUtil.successMessage); 173 } 174 175 private static String INDENT = " "; 176 private static void printMemoryUsage(MemoryUsage current, MemoryUsage peak) { 177 System.out.println("Current Usage: "); 178 MemoryUtil.printMemoryUsage(current); 179 System.out.println("Peak Usage: "); 180 MemoryUtil.printMemoryUsage(peak); 181 182 } 183 private static String formatSize(String name, long value) { 184 StringBuffer buf = new StringBuffer(name + " = " + value); 185 if (value > 0) { 186 buf.append(" (" + (value >> 10) + "K)"); 187 } 188 return buf.toString(); 189 } 190 }