Path: blob/master/test/jdk/java/lang/management/MemoryMXBean/ResetPeakMemoryUsage.java
41155 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* The -XX:MarkSweepAlwaysCompactCount=1 argument below makes sure serial gc25* compacts the heap at every full gc so that the usage is correctly updated.26*/2728/*29* @test30* @bug 489250731* @summary Basic Test for MemoryPool.resetPeakUsage()32* @author Mandy Chung33*34* @requires vm.opt.ExplicitGCInvokesConcurrent != "true"35* @requires vm.opt.DisableExplicitGC != "true"36* @library /test/lib37* @modules jdk.management38*39* @build ResetPeakMemoryUsage MemoryUtil RunUtil40* @build sun.hotspot.WhiteBox41* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox42* @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. ResetPeakMemoryUsage43*/4445import java.lang.management.*;46import java.lang.ref.WeakReference;47import java.util.*;4849import sun.hotspot.code.Compiler;5051public class ResetPeakMemoryUsage {52private static MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();53// make public so that it can't be optimized away easily54public static Object[] obj;5556/**57* Run the test multiple times with different GC versions.58* First with default command line specified by the framework.59* Then with all GC versions specified by the test.60*/61public static void main(String a[]) throws Throwable {62final String main = "ResetPeakMemoryUsage$TestMain";63final String ms = "-Xms256m";64final String mn = "-Xmn8m";6566RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseParallelGC");67RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseG1GC", "-XX:G1HeapRegionSize=1m");68RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseSerialGC",69"-XX:MarkSweepAlwaysCompactCount=1");70}7172private static class TestMain {73public static void main(String[] argv) {74List pools = ManagementFactory.getMemoryPoolMXBeans();75ListIterator iter = pools.listIterator();76boolean found = false;77while (iter.hasNext()) {78MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();79// only check heap pools that support usage threshold80// this is typically only the old generation space81// since the other spaces are expected to get filled up82if (p.getType() == MemoryType.HEAP &&83p.isUsageThresholdSupported())84{85found = true;86testPool(p);87}88}89if (!found) {90throw new RuntimeException("No heap pool found");91}92}93}9495private static void testPool(MemoryPoolMXBean mpool) {96System.out.println("Selected memory pool: ");97MemoryUtil.printMemoryPool(mpool);9899MemoryUsage usage0 = mpool.getUsage();100MemoryUsage peak0 = mpool.getPeakUsage();101102// use a size that is larger than the young generation and G1 regions103// to force the array into the old gen104int largeArraySize = 9 * 1000 * 1000;105106System.out.println("Before big object array (of size "+largeArraySize+") is allocated: ");107printMemoryUsage(usage0, peak0);108109obj = new Object[largeArraySize];110WeakReference<Object> weakRef = new WeakReference<>(obj);111112MemoryUsage usage1 = mpool.getUsage();113MemoryUsage peak1 = mpool.getPeakUsage();114System.out.println("After the object is allocated: ");115printMemoryUsage(usage1, peak1);116117if (usage1.getUsed() <= usage0.getUsed()) {118throw new RuntimeException(119formatSize("Before allocation: used", usage0.getUsed()) +120" expected to be < " +121formatSize("After allocation: used", usage1.getUsed()));122}123124if (peak1.getUsed() <= peak0.getUsed()) {125throw new RuntimeException(126formatSize("Before allocation: peak", peak0.getUsed()) +127" expected to be < " +128formatSize("After allocation: peak", peak1.getUsed()));129}130131132// The object is now garbage and do a GC133// memory usage should drop134obj = null;135136//This will cause sure shot GC unlike Runtime.gc() invoked by mbean.gc()137while(weakRef.get() != null) {138mbean.gc();139}140141MemoryUsage usage2 = mpool.getUsage();142MemoryUsage peak2 = mpool.getPeakUsage();143System.out.println("After GC: ");144printMemoryUsage(usage2, peak2);145146if (usage2.getUsed() >= usage1.getUsed()) {147throw new RuntimeException(148formatSize("Before GC: used", usage1.getUsed()) + " " +149" expected to be > " +150formatSize("After GC: used", usage2.getUsed()));151}152153mpool.resetPeakUsage();154155MemoryUsage usage3 = mpool.getUsage();156MemoryUsage peak3 = mpool.getPeakUsage();157System.out.println("After resetPeakUsage: ");158printMemoryUsage(usage3, peak3);159160if (peak3.getUsed() != usage3.getUsed()) {161throw new RuntimeException(162formatSize("After resetting peak: peak", peak3.getUsed()) + " " +163" expected to be equal to " +164formatSize("current used", usage3.getUsed()));165}166167if (peak3.getUsed() >= peak2.getUsed()) {168throw new RuntimeException(169formatSize("After resetting peak: peak", peak3.getUsed()) + " " +170" expected to be < " +171formatSize("previous peak", peak2.getUsed()));172}173174System.out.println(RunUtil.successMessage);175}176177private static String INDENT = " ";178private static void printMemoryUsage(MemoryUsage current, MemoryUsage peak) {179System.out.println("Current Usage: ");180MemoryUtil.printMemoryUsage(current);181System.out.println("Peak Usage: ");182MemoryUtil.printMemoryUsage(peak);183184}185private static String formatSize(String name, long value) {186StringBuffer buf = new StringBuffer(name + " = " + value);187if (value > 0) {188buf.append(" (" + (value >> 10) + "K)");189}190return buf.toString();191}192}193194195