Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/MemoryMXBean/MemoryTest.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* @test25* @bug 453053826* @summary Basic unit test of MemoryMXBean.getMemoryPools() and27* MemoryMXBean.getMemoryManager().28* @author Mandy Chung29*30* @run main MemoryTest 231*/3233/*34* NOTE: This expected result is hardcoded in this test and this test35* will be affected if the heap memory layout is changed in36* the future implementation.37*/3839import java.lang.management.*;40import java.util.*;4142public class MemoryTest {43private static boolean testFailed = false;44private static MemoryMXBean mm = ManagementFactory.getMemoryMXBean();45private static final int HEAP = 0;46private static final int NONHEAP = 1;47private static final int NUM_TYPES = 2;4849// WARNING: if the number of pools changes in the future,50// this test needs to be modified to handle different version of VMs.5152// Hotspot VM 1.5 expected to have53// heap memory pools = 3 (Eden, Survivor spaces, Old gen)54// non-heap memory pools = 2 (Perm gen, Code cache)55// or 4 if Class Sharing is enabled.56// Number of memory managers = 357// They are: Copy/Scavenger + MSC + CodeCache manager58// (or equivalent for other collectors)59// Number of GC memory managers = 26061// Hotspot VM 1.8+ after perm gen removal is expected to have two or62// three non-heap memory pools:63// - Code cache64// - Metaspace65// - Compressed Class Space (if compressed class pointers are used)66private static int[] expectedMinNumPools = {3, 2};67private static int[] expectedMaxNumPools = {3, 3};68private static int expectedNumGCMgrs = 2;69private static int expectedNumMgrs = expectedNumGCMgrs + 2;70private static String[] types = { "heap", "non-heap" };7172public static void main(String args[]) throws Exception {73Integer value = new Integer(args[0]);74expectedNumGCMgrs = value.intValue();75expectedNumMgrs = expectedNumGCMgrs + 2;7677checkMemoryPools();78checkMemoryManagers();79if (testFailed)80throw new RuntimeException("TEST FAILED.");8182System.out.println("Test passed.");8384}8586private static void checkMemoryPools() throws Exception {87List pools = ManagementFactory.getMemoryPoolMXBeans();88boolean hasPerm = false;8990int[] numPools = new int[NUM_TYPES];91for (ListIterator iter = pools.listIterator(); iter.hasNext();) {92MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();93if (pool.getType() == MemoryType.HEAP) {94numPools[HEAP]++;95}96if (pool.getType() == MemoryType.NON_HEAP) {97numPools[NONHEAP]++;98}99if (pool.getName().toLowerCase().contains("perm")) {100hasPerm = true;101}102}103104if (hasPerm) {105// If the VM has perm gen there will be between 2 and 4 non heap106// pools (4 if class data sharing is used)107expectedMinNumPools[NONHEAP] = 2;108expectedMaxNumPools[NONHEAP] = 4;109}110111// Check the number of Memory pools112for (int i = 0; i < NUM_TYPES; i++) {113if (numPools[i] < expectedMinNumPools[i] ||114numPools[i] > expectedMaxNumPools[i]) {115throw new RuntimeException("TEST FAILED: " +116"Number of " + types[i] + " pools = " + numPools[i] +117" but expected <= " + expectedMaxNumPools[i] +118" and >= " + expectedMinNumPools[i]);119}120}121}122123private static void checkMemoryManagers() throws Exception {124List mgrs = ManagementFactory.getMemoryManagerMXBeans();125126int numGCMgr = 0;127128// Check the number of Memory Managers129for (ListIterator iter = mgrs.listIterator(); iter.hasNext();) {130MemoryManagerMXBean mgr = (MemoryManagerMXBean) iter.next();131String[] poolNames = mgr.getMemoryPoolNames();132if (poolNames == null || poolNames.length == 0) {133throw new RuntimeException("TEST FAILED: " +134"Expected to have one or more pools for " +135mgr.getName() + "manager.");136}137138if (mgr instanceof GarbageCollectorMXBean) {139numGCMgr++;140} else {141for (int i = 0; i < poolNames.length; i++) {142checkPoolType(poolNames[i], MemoryType.NON_HEAP);143}144}145}146147if (mgrs.size() != expectedNumMgrs) {148throw new RuntimeException("TEST FAILED: " +149"Number of memory managers = " + mgrs.size() +150" but expected = " + expectedNumMgrs);151}152if (numGCMgr != expectedNumGCMgrs) {153throw new RuntimeException("TEST FAILED: " +154"Number of GC managers = " + numGCMgr + " but expected = " +155expectedNumGCMgrs);156}157}158private static List pools = ManagementFactory.getMemoryPoolMXBeans();159private static void checkPoolType(String name, MemoryType type)160throws Exception {161for (ListIterator iter = pools.listIterator(); iter.hasNext(); ) {162MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();163if (pool.getName().equals(name)) {164if (pool.getType() != type) {165throw new RuntimeException("TEST FAILED: " +166"Pool " + pool.getName() + " is of type " +167pool.getType() + " but expected to be " + type);168} else {169return;170}171}172}173throw new RuntimeException("TEST FAILED: " +174"Pool " + name + " is of type " + type +175" not found");176}177}178179180