Path: blob/master/test/jdk/java/lang/management/MemoryMXBean/MemoryTest.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* @test25* @bug 453053826* @summary Basic unit test of MemoryMXBean.getMemoryPools() and27* MemoryMXBean.getMemoryManager().28* @requires vm.gc != "Z"29* @author Mandy Chung30*31* @modules jdk.management32* @run main MemoryTest 2 333*/3435/*36* @test37* @bug 453053838* @summary Basic unit test of MemoryMXBean.getMemoryPools() and39* MemoryMXBean.getMemoryManager().40* @requires vm.gc == "Z"41* @author Mandy Chung42*43* @modules jdk.management44* @run main MemoryTest 2 145*/4647/*48* NOTE: This expected result is hardcoded in this test and this test49* will be affected if the heap memory layout is changed in50* the future implementation.51*/5253import java.lang.management.*;54import java.util.*;5556public class MemoryTest {57private static boolean testFailed = false;58private static MemoryMXBean mm = ManagementFactory.getMemoryMXBean();59private static final int HEAP = 0;60private static final int NONHEAP = 1;61private static final int NUM_TYPES = 2;6263// WARNING: if the number of pools changes in the future,64// this test needs to be modified to handle different version of VMs.6566// Hotspot VM 1.5 expected to have67// heap memory pools = 3 (Eden, Survivor spaces, Old gen)68// non-heap memory pools = 2 (Perm gen, Code cache)69// or 4 if Class Sharing is enabled.70// Number of memory managers = 371// They are: Copy/Scavenger + MSC + CodeCache manager72// (or equivalent for other collectors)73// Number of GC memory managers = 27475// Hotspot VM 1.8+ after perm gen removal is expected to have between two76// or five non-heap memory pools:77// - Code cache (between one and three depending on the -XX:SegmentedCodeCache option)78// - Metaspace79// - Compressed Class Space (if compressed class pointers are used)8081private static int[] expectedMinNumPools = new int[2];82private static int[] expectedMaxNumPools = new int[2];83private static int expectedNumGCMgrs;84private static int expectedNumMgrs;85private static String[] types = { "heap", "non-heap" };8687public static void main(String args[]) throws Exception {88expectedNumGCMgrs = Integer.valueOf(args[0]);89expectedNumMgrs = expectedNumGCMgrs + 2;9091int expectedNumPools = Integer.valueOf(args[1]);92expectedMinNumPools[HEAP] = expectedNumPools;93expectedMaxNumPools[HEAP] = expectedNumPools;9495expectedMinNumPools[NONHEAP] = 2;96expectedMaxNumPools[NONHEAP] = 5;9798checkMemoryPools();99checkMemoryManagers();100if (testFailed)101throw new RuntimeException("TEST FAILED.");102103System.out.println("Test passed.");104105}106107private static void checkMemoryPools() throws Exception {108List pools = ManagementFactory.getMemoryPoolMXBeans();109boolean hasPerm = false;110111int[] numPools = new int[NUM_TYPES];112for (ListIterator iter = pools.listIterator(); iter.hasNext();) {113MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();114if (pool.getType() == MemoryType.HEAP) {115numPools[HEAP]++;116}117if (pool.getType() == MemoryType.NON_HEAP) {118numPools[NONHEAP]++;119}120if (pool.getName().toLowerCase().contains("perm")) {121hasPerm = true;122}123}124125if (hasPerm) {126// If the VM has perm gen there will be between 2 and 4 non heap127// pools (4 if class data sharing is used)128expectedMinNumPools[NONHEAP] = 2;129expectedMaxNumPools[NONHEAP] = 4;130}131132// Check the number of Memory pools133for (int i = 0; i < NUM_TYPES; i++) {134if (numPools[i] < expectedMinNumPools[i] ||135numPools[i] > expectedMaxNumPools[i]) {136throw new RuntimeException("TEST FAILED: " +137"Number of " + types[i] + " pools = " + numPools[i] +138" but expected <= " + expectedMaxNumPools[i] +139" and >= " + expectedMinNumPools[i]);140}141}142}143144private static void checkMemoryManagers() throws Exception {145List mgrs = ManagementFactory.getMemoryManagerMXBeans();146147int numGCMgr = 0;148149// Check the number of Memory Managers150for (ListIterator iter = mgrs.listIterator(); iter.hasNext();) {151MemoryManagerMXBean mgr = (MemoryManagerMXBean) iter.next();152String[] poolNames = mgr.getMemoryPoolNames();153if (poolNames == null || poolNames.length == 0) {154throw new RuntimeException("TEST FAILED: " +155"Expected to have one or more pools for " +156mgr.getName() + "manager.");157}158159if (mgr instanceof GarbageCollectorMXBean) {160numGCMgr++;161} else {162for (int i = 0; i < poolNames.length; i++) {163checkPoolType(poolNames[i], MemoryType.NON_HEAP);164}165}166}167168if (mgrs.size() != expectedNumMgrs) {169throw new RuntimeException("TEST FAILED: " +170"Number of memory managers = " + mgrs.size() +171" but expected = " + expectedNumMgrs);172}173if (numGCMgr != expectedNumGCMgrs) {174throw new RuntimeException("TEST FAILED: " +175"Number of GC managers = " + numGCMgr + " but expected = " +176expectedNumGCMgrs);177}178}179private static List pools = ManagementFactory.getMemoryPoolMXBeans();180private static void checkPoolType(String name, MemoryType type)181throws Exception {182for (ListIterator iter = pools.listIterator(); iter.hasNext(); ) {183MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();184if (pool.getName().equals(name)) {185if (pool.getType() != type) {186throw new RuntimeException("TEST FAILED: " +187"Pool " + pool.getName() + " is of type " +188pool.getType() + " but expected to be " + type);189} else {190return;191}192}193}194throw new RuntimeException("TEST FAILED: " +195"Pool " + name + " is of type " + type +196" not found");197}198}199200201