Path: blob/master/test/hotspot/jtreg/gc/epsilon/TestMemoryMXBeans.java
66644 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*/2324package gc.epsilon;2526/**27* @test TestMemoryMXBeans28* @requires vm.gc.Epsilon29* @summary Test JMX memory beans30* @modules java.base/jdk.internal.misc31* java.management32*33* @run main/othervm -Xmx256m34* -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC35* gc.epsilon.TestMemoryMXBeans36* -1 25637*38* @run main/othervm -Xmx256m -Xmx256m39* -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC40* gc.epsilon.TestMemoryMXBeans41* 256 25642*43* @run main/othervm -Xms64m -Xmx256m44* -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC45* gc.epsilon.TestMemoryMXBeans46* 64 25647*/4849import java.lang.management.*;5051public class TestMemoryMXBeans {5253static volatile Object sink;5455public static void main(String[] args) throws Exception {56if (args.length < 2) {57throw new IllegalStateException("Should provide expected heap sizes");58}5960long initSize = 1L * Integer.parseInt(args[0]) * 1024 * 1024;61long maxSize = 1L * Integer.parseInt(args[1]) * 1024 * 1024;6263testMemoryBean(initSize, maxSize);64testAllocs();65}6667public static void testMemoryBean(long initSize, long maxSize) {68MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();69long heapInit = memoryMXBean.getHeapMemoryUsage().getInit();70long heapMax = memoryMXBean.getHeapMemoryUsage().getMax();71memoryMXBean.getNonHeapMemoryUsage().getInit(); // value not used72memoryMXBean.getNonHeapMemoryUsage().getMax(); // value not used7374if (initSize > 0 && heapInit != initSize) {75throw new IllegalStateException("Init heap size is wrong: " + heapInit + " vs " + initSize);76}77if (maxSize > 0 && heapMax != maxSize) {78throw new IllegalStateException("Max heap size is wrong: " + heapMax + " vs " + maxSize);79}80}8182public static void testAllocs() throws Exception {83MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();8485// Try multiple times, to capture either APIs we call allocate lazily, or the background threads allocating86int maxTries = 10;87int tries = 0;8889while (true) {90// Compute how much we waste during the calls themselves:91long heapUsed1 = memoryMXBean.getHeapMemoryUsage().getUsed();92long heapUsed2 = memoryMXBean.getHeapMemoryUsage().getUsed();93long adj = heapUsed2 - heapUsed1;9495heapUsed1 = memoryMXBean.getHeapMemoryUsage().getUsed();96sink = new int[1024*1024];97heapUsed2 = memoryMXBean.getHeapMemoryUsage().getUsed();9899long diff = heapUsed2 - heapUsed1 - adj;100long min = 8 + 4*1024*1024;101long max = 16 + 4*1024*1024;102if ((min <= diff && diff <= max)) {103// Success104return;105}106107if (tries++ > maxTries) {108throw new IllegalStateException("Allocation did not change used space right: " + diff + " should be in [" + min + ", " + max + "]");109}110111// Wait and try again112Thread.sleep(1000);113}114}115116}117118119