Path: blob/master/test/jdk/java/lang/management/MemoryPoolMXBean/ThresholdTest.java
41152 views
/*1* Copyright (c) 2007, 2015, 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 654608926* @summary Basic unit test of MemoryPoolMXBean.isUsageThresholdExceeded() and27* MemoryPoolMXBean.isCollectionThresholdExceeded().28* @author Mandy Chung29*30* @run main/othervm ThresholdTest31*/3233import java.lang.management.*;34import java.util.*;3536public class ThresholdTest {37public static void main(String args[]) throws Exception {38long[] bigObject = new long[1000000];3940System.gc(); // force an initial full-gc41List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();42try {43for (MemoryPoolMXBean p : pools) {44// verify if isUsageThresholdExceeded() returns correct value45checkUsageThreshold(p);46// verify if isCollectionUsageThresholdExceeded() returns correct value47checkCollectionUsageThreshold(p);48}49} finally {50// restore the default51for (MemoryPoolMXBean p : pools) {52if (p.isUsageThresholdSupported()) {53p.setUsageThreshold(0);54}55if (p.isCollectionUsageThresholdSupported()) {56p.setCollectionUsageThreshold(0);57}58}59}6061System.out.println("Test passed.");62}6364private static void checkUsageThreshold(MemoryPoolMXBean p) throws Exception {6566if (!p.isUsageThresholdSupported()) {67return;68}6970long threshold = p.getUsageThreshold();71if (threshold != 0) {72// Expect the default threshold is zero (disabled)73throw new RuntimeException("TEST FAILED: " +74"Pool " + p.getName() +75" has non-zero threshold (" + threshold);76}7778// isUsageThresholdExceeded() should return false if threshold == 079if (p.isUsageThresholdExceeded()) {80throw new RuntimeException("TEST FAILED: " +81"Pool " + p.getName() +82" isUsageThresholdExceeded() returned true" +83" but threshold = 0");84}8586p.setUsageThreshold(1);87// force a full gc to minimize the likelihood of running GC88// between getting the usage and checking the threshold89System.gc();9091MemoryUsage u = p.getUsage();92if (u.getUsed() >= 1) {93if (!p.isUsageThresholdExceeded()) {94throw new RuntimeException("TEST FAILED: " +95"Pool " + p.getName() +96" isUsageThresholdExceeded() returned false but " +97" threshold(" + p.getUsageThreshold() +98") <= used(" + u.getUsed() + ")");99}100} else {101if (p.isUsageThresholdExceeded()) {102throw new RuntimeException("TEST FAILED: " +103"Pool " + p.getName() +104" isUsageThresholdExceeded() returned true but" +105" threshold(" + p.getUsageThreshold() +106") > used(" + u.getUsed() + ")");107}108}109110// disable low memory detection and isUsageThresholdExceeded()111// should return false112p.setUsageThreshold(0);113if (p.isUsageThresholdExceeded()) {114throw new RuntimeException("TEST FAILED: " +115"Pool " + p.getName() +116" isUsageThresholdExceeded() returned true but threshold = 0");117}118}119120private static void checkCollectionUsageThreshold(MemoryPoolMXBean p) throws Exception {121122if (!p.isCollectionUsageThresholdSupported()) {123return;124}125126long threshold = p.getCollectionUsageThreshold();127if (threshold != 0) {128// Expect the default threshold is zero (disabled)129throw new RuntimeException("TEST FAILED: " +130"Pool " + p.getName() +131" has non-zero threshold (" + threshold);132}133134// isCollectionUsageThresholdExceeded() should return false if threshold == 0135if (p.isCollectionUsageThresholdExceeded()) {136throw new RuntimeException("TEST FAILED: " +137"Pool " + p.getName() +138" isCollectionUsageThresholdExceeded() returned true" +139" but threshold = 0");140}141142p.setCollectionUsageThreshold(1);143MemoryUsage u = p.getCollectionUsage();144if (u == null) {145if (p.isCollectionUsageThresholdExceeded()) {146throw new RuntimeException("TEST FAILED: " +147"Pool " + p.getName() +148" isCollectionUsageThresholdExceeded() returned true but" +149" getCollectionUsage() return null");150}151} else if (u.getUsed() >= 1) {152if (!p.isCollectionUsageThresholdExceeded()) {153throw new RuntimeException("TEST FAILED: " +154"Pool " + p.getName() +155" isCollectionUsageThresholdExceeded() returned false but " +156" threshold(" + p.getCollectionUsageThreshold() +157") < used(" + u.getUsed() + ")");158}159} else {160if (p.isCollectionUsageThresholdExceeded()) {161throw new RuntimeException("TEST FAILED: " +162"Pool " + p.getName() +163" isCollectionUsageThresholdExceeded() returned true but" +164" threshold(" + p.getCollectionUsageThreshold() +165") > used(" + u.getUsed() + ")");166}167}168169// disable low memory detection and isCollectionUsageThresholdExceeded()170// should return false171p.setCollectionUsageThreshold(0);172if (p.isCollectionUsageThresholdExceeded()) {173throw new RuntimeException("TEST FAILED: " +174"Pool " + p.getName() +175" isCollectionUsageThresholdExceeded() returned true but threshold = 0");176}177}178}179180181