Path: blob/master/test/hotspot/jtreg/gc/epsilon/TestMemoryMXBeans.java
40942 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* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -Xmx1g gc.epsilon.TestMemoryMXBeans -1 102433* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -Xms1g -Xmx1g gc.epsilon.TestMemoryMXBeans 1024 102434* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -Xms128m -Xmx1g gc.epsilon.TestMemoryMXBeans 128 102435*/3637import java.lang.management.*;3839public class TestMemoryMXBeans {4041static volatile Object sink;4243public static void main(String[] args) throws Exception {44if (args.length < 2) {45throw new IllegalStateException("Should provide expected heap sizes");46}4748long initSize = 1L * Integer.parseInt(args[0]) * 1024 * 1024;49long maxSize = 1L * Integer.parseInt(args[1]) * 1024 * 1024;5051testMemoryBean(initSize, maxSize);52testAllocs();53}5455public static void testMemoryBean(long initSize, long maxSize) {56MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();57long heapInit = memoryMXBean.getHeapMemoryUsage().getInit();58long heapMax = memoryMXBean.getHeapMemoryUsage().getMax();59memoryMXBean.getNonHeapMemoryUsage().getInit(); // value not used60memoryMXBean.getNonHeapMemoryUsage().getMax(); // value not used6162if (initSize > 0 && heapInit != initSize) {63throw new IllegalStateException("Init heap size is wrong: " + heapInit + " vs " + initSize);64}65if (maxSize > 0 && heapMax != maxSize) {66throw new IllegalStateException("Max heap size is wrong: " + heapMax + " vs " + maxSize);67}68}6970public static void testAllocs() throws Exception {71MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();7273// Try multiple times, to capture either APIs we call allocate lazily, or the background threads allocating74int maxTries = 10;75int tries = 0;7677while (true) {78// Compute how much we waste during the calls themselves:79long heapUsed1 = memoryMXBean.getHeapMemoryUsage().getUsed();80long heapUsed2 = memoryMXBean.getHeapMemoryUsage().getUsed();81long adj = heapUsed2 - heapUsed1;8283heapUsed1 = memoryMXBean.getHeapMemoryUsage().getUsed();84sink = new int[1024*1024];85heapUsed2 = memoryMXBean.getHeapMemoryUsage().getUsed();8687long diff = heapUsed2 - heapUsed1 - adj;88long min = 8 + 4*1024*1024;89long max = 16 + 4*1024*1024;90if ((min <= diff && diff <= max)) {91// Success92return;93}9495if (tries++ > maxTries) {96throw new IllegalStateException("Allocation did not change used space right: " + diff + " should be in [" + min + ", " + max + "]");97}9899// Wait and try again100Thread.sleep(1000);101}102}103104}105106107