Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/MemoryMXBean/ResetPeakMemoryUsage.java
38821 views
/*1* Copyright (c) 2003, 2013, 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* @library /lib/testlibrary/35* @build ResetPeakMemoryUsage MemoryUtil RunUtil36* @run main ResetPeakMemoryUsage37*/3839import java.lang.management.*;40import java.util.*;4142public class ResetPeakMemoryUsage {43private static MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();44// make public so that it can't be optimized away easily45public static Object[] obj;4647/**48* Run the test multiple times with different GC versions.49* First with default command line specified by the framework.50* Then with all GC versions specified by the test.51*/52public static void main(String a[]) throws Throwable {53final String main = "ResetPeakMemoryUsage$TestMain";54final String ms = "-Xms256m";55final String mn = "-Xmn8m";56RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseConcMarkSweepGC");57RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseParallelGC");58RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseG1GC", "-XX:G1HeapRegionSize=1m");59RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseSerialGC",60"-XX:MarkSweepAlwaysCompactCount=1");61}6263private static class TestMain {64public static void main(String[] argv) {65List pools = ManagementFactory.getMemoryPoolMXBeans();66ListIterator iter = pools.listIterator();67boolean found = false;68while (iter.hasNext()) {69MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();70// only check heap pools that support usage threshold71// this is typically only the old generation space72// since the other spaces are expected to get filled up73if (p.getType() == MemoryType.HEAP &&74p.isUsageThresholdSupported())75{76found = true;77testPool(p);78}79}80if (!found) {81throw new RuntimeException("No heap pool found");82}83}84}8586private static void testPool(MemoryPoolMXBean mpool) {87System.out.println("Selected memory pool: ");88MemoryUtil.printMemoryPool(mpool);8990MemoryUsage usage0 = mpool.getUsage();91MemoryUsage peak0 = mpool.getPeakUsage();9293// use a size that is larger than the young generation and G1 regions94// to force the array into the old gen95int largeArraySize = 9 * 1000 * 1000;9697System.out.println("Before big object array (of size "+largeArraySize+") is allocated: ");98printMemoryUsage(usage0, peak0);99100obj = new Object[largeArraySize];101102MemoryUsage usage1 = mpool.getUsage();103MemoryUsage peak1 = mpool.getPeakUsage();104System.out.println("After the object is allocated: ");105printMemoryUsage(usage1, peak1);106107if (usage1.getUsed() <= usage0.getUsed()) {108throw new RuntimeException(109formatSize("Before allocation: used", usage0.getUsed()) +110" expected to be < " +111formatSize("After allocation: used", usage1.getUsed()));112}113114if (peak1.getUsed() <= peak0.getUsed()) {115throw new RuntimeException(116formatSize("Before allocation: peak", peak0.getUsed()) +117" expected to be < " +118formatSize("After allocation: peak", peak1.getUsed()));119}120121122// The object is now garbage and do a GC123// memory usage should drop124obj = null;125mbean.gc();126127MemoryUsage usage2 = mpool.getUsage();128MemoryUsage peak2 = mpool.getPeakUsage();129System.out.println("After GC: ");130printMemoryUsage(usage2, peak2);131132if (usage2.getUsed() >= usage1.getUsed()) {133throw new RuntimeException(134formatSize("Before GC: used", usage1.getUsed()) + " " +135" expected to be > " +136formatSize("After GC: used", usage2.getUsed()));137}138139mpool.resetPeakUsage();140141MemoryUsage usage3 = mpool.getUsage();142MemoryUsage peak3 = mpool.getPeakUsage();143System.out.println("After resetPeakUsage: ");144printMemoryUsage(usage3, peak3);145146if (peak3.getUsed() != usage3.getUsed()) {147throw new RuntimeException(148formatSize("After resetting peak: peak", peak3.getUsed()) + " " +149" expected to be equal to " +150formatSize("current used", usage3.getUsed()));151}152153if (peak3.getUsed() >= peak2.getUsed()) {154throw new RuntimeException(155formatSize("After resetting peak: peak", peak3.getUsed()) + " " +156" expected to be < " +157formatSize("previous peak", peak2.getUsed()));158}159160System.out.println(RunUtil.successMessage);161}162163private static String INDENT = " ";164private static void printMemoryUsage(MemoryUsage current, MemoryUsage peak) {165System.out.println("Current Usage: ");166MemoryUtil.printMemoryUsage(current);167System.out.println("Peak Usage: ");168MemoryUtil.printMemoryUsage(peak);169170}171private static String formatSize(String name, long value) {172StringBuffer buf = new StringBuffer(name + " = " + value);173if (value > 0) {174buf.append(" (" + (value >> 10) + "K)");175}176return buf.toString();177}178}179180181