Path: blob/master/test/hotspot/jtreg/gc/shenandoah/mxbeans/TestMemoryMXBeans.java
41153 views
/*1* Copyright (c) 2017, 2018, Red Hat, Inc. 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*22*/2324/**25* @test TestMemoryMXBeans26* @summary Test JMX memory beans27* @requires vm.gc.Shenandoah28* @modules java.base/jdk.internal.misc29* java.management30* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -Xmx1g TestMemoryMXBeans -1 102431* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -Xms1g -Xmx1g TestMemoryMXBeans 1024 102432* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -Xms128m -Xmx1g TestMemoryMXBeans 128 102433* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -Xms1g -Xmx1g -XX:ShenandoahUncommitDelay=0 TestMemoryMXBeans 1024 102434* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -Xms128m -Xmx1g -XX:ShenandoahUncommitDelay=0 TestMemoryMXBeans 128 102435*/3637import java.lang.management.*;38import java.util.*;3940public class TestMemoryMXBeans {4142public static void main(String[] args) throws Exception {43if (args.length < 2) {44throw new IllegalStateException("Should provide expected heap sizes");45}4647long initSize = 1L * Integer.parseInt(args[0]) * 1024 * 1024;48long maxSize = 1L * Integer.parseInt(args[1]) * 1024 * 1024;4950// wait for GC to uncommit51Thread.sleep(1000);5253testMemoryBean(initSize, maxSize);54}5556public static void testMemoryBean(long initSize, long maxSize) {57MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();58long heapInit = memoryMXBean.getHeapMemoryUsage().getInit();59long heapCommitted = memoryMXBean.getHeapMemoryUsage().getCommitted();60long heapMax = memoryMXBean.getHeapMemoryUsage().getMax();61long nonHeapInit = memoryMXBean.getNonHeapMemoryUsage().getInit();62long nonHeapCommitted = memoryMXBean.getNonHeapMemoryUsage().getCommitted();63long nonHeapMax = memoryMXBean.getNonHeapMemoryUsage().getMax();6465if (initSize > 0 && heapInit != initSize) {66throw new IllegalStateException("Init heap size is wrong: " + heapInit + " vs " + initSize);67}68if (maxSize > 0 && heapMax != maxSize) {69throw new IllegalStateException("Max heap size is wrong: " + heapMax + " vs " + maxSize);70}71if (initSize > 0 && maxSize > 0 && initSize != maxSize && heapCommitted == heapMax) {72throw new IllegalStateException("Committed heap size is max: " + heapCommitted +73" (init: " + initSize + ", max: " + maxSize + ")");74}75if (initSize > 0 && maxSize > 0 && initSize == maxSize && heapCommitted != heapMax) {76throw new IllegalStateException("Committed heap size is not max: " + heapCommitted +77" (init: " + initSize + ", max: " + maxSize + ")");78}79if (initSize > 0 && heapCommitted < initSize) {80throw new IllegalStateException("Committed heap size is below min: " + heapCommitted +81" (init: " + initSize + ", max: " + maxSize + ")");82}83}84}858687