1 /* 2 * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6546089 27 * @summary Basic unit test of MemoryPoolMXBean.isUsageThresholdExceeded() and 28 * MemoryPoolMXBean.isCollectionThresholdExceeded(). 29 * @author Mandy Chung 30 * 31 * @run main/othervm ThresholdTest 32 */ 33 34 import java.lang.management.*; 35 import java.util.*; 36 37 public class ThresholdTest { 38 public static void main(String args[]) throws Exception { 39 long[] bigObject = new long[1000000]; 40 41 System.gc(); // force an initial full-gc 42 List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); 43 try { 44 for (MemoryPoolMXBean p : pools) { 45 // verify if isUsageThresholdExceeded() returns correct value 46 checkUsageThreshold(p); 47 // verify if isCollectionUsageThresholdExceeded() returns correct value 48 checkCollectionUsageThreshold(p); 49 } 50 } finally { 51 // restore the default 52 for (MemoryPoolMXBean p : pools) { 53 if (p.isUsageThresholdSupported()) { 54 p.setUsageThreshold(0); 55 } 56 if (p.isCollectionUsageThresholdSupported()) { 57 p.setCollectionUsageThreshold(0); 58 } 59 } 60 } 61 62 System.out.println("Test passed."); 63 } 64 65 private static void checkUsageThreshold(MemoryPoolMXBean p) throws Exception { 66 67 if (!p.isUsageThresholdSupported()) { 68 return; 69 } 70 71 long threshold = p.getUsageThreshold(); 72 if (threshold != 0) { 73 // Expect the default threshold is zero (disabled) 74 throw new RuntimeException("TEST FAILED: " + 75 "Pool " + p.getName() + 76 " has non-zero threshold (" + threshold); 77 } 78 79 // isUsageThresholdExceeded() should return false if threshold == 0 80 if (p.isUsageThresholdExceeded()) { 81 throw new RuntimeException("TEST FAILED: " + 82 "Pool " + p.getName() + 83 " isUsageThresholdExceeded() returned true" + 84 " but threshold = 0"); 85 } 86 87 p.setUsageThreshold(1); 88 // force a full gc to minimize the likelihood of running GC 89 // between getting the usage and checking the threshold 90 System.gc(); 91 92 MemoryUsage u = p.getUsage(); 93 if (u.getUsed() >= 1) { 94 if (!p.isUsageThresholdExceeded()) { 95 throw new RuntimeException("TEST FAILED: " + 96 "Pool " + p.getName() + 97 " isUsageThresholdExceeded() returned false but " + 98 " threshold(" + p.getUsageThreshold() + 99 ") <= used(" + u.getUsed() + ")"); 100 } 101 } else { 102 if (p.isUsageThresholdExceeded()) { 103 throw new RuntimeException("TEST FAILED: " + 104 "Pool " + p.getName() + 105 " isUsageThresholdExceeded() returned true but" + 106 " threshold(" + p.getUsageThreshold() + 107 ") > used(" + u.getUsed() + ")"); 108 } 109 } 110 111 // disable low memory detection and isUsageThresholdExceeded() 112 // should return false 113 p.setUsageThreshold(0); 114 if (p.isUsageThresholdExceeded()) { 115 throw new RuntimeException("TEST FAILED: " + 116 "Pool " + p.getName() + 117 " isUsageThresholdExceeded() returned true but threshold = 0"); 118 } 119 } 120 121 private static void checkCollectionUsageThreshold(MemoryPoolMXBean p) throws Exception { 122 123 if (!p.isCollectionUsageThresholdSupported()) { 124 return; 125 } 126 127 long threshold = p.getCollectionUsageThreshold(); 128 if (threshold != 0) { 129 // Expect the default threshold is zero (disabled) 130 throw new RuntimeException("TEST FAILED: " + 131 "Pool " + p.getName() + 132 " has non-zero threshold (" + threshold); 133 } 134 135 // isCollectionUsageThresholdExceeded() should return false if threshold == 0 136 if (p.isCollectionUsageThresholdExceeded()) { 137 throw new RuntimeException("TEST FAILED: " + 138 "Pool " + p.getName() + 139 " isCollectionUsageThresholdExceeded() returned true" + 140 " but threshold = 0"); 141 } 142 143 p.setCollectionUsageThreshold(1); 144 MemoryUsage u = p.getCollectionUsage(); 145 if (u == null) { 146 if (p.isCollectionUsageThresholdExceeded()) { 147 throw new RuntimeException("TEST FAILED: " + 148 "Pool " + p.getName() + 149 " isCollectionUsageThresholdExceeded() returned true but" + 150 " getCollectionUsage() return null"); 151 } 152 } else if (u.getUsed() >= 1) { 153 if (!p.isCollectionUsageThresholdExceeded()) { 154 throw new RuntimeException("TEST FAILED: " + 155 "Pool " + p.getName() + 156 " isCollectionUsageThresholdExceeded() returned false but " + 157 " threshold(" + p.getCollectionUsageThreshold() + 158 ") < used(" + u.getUsed() + ")"); 159 } 160 } else { 161 if (p.isCollectionUsageThresholdExceeded()) { 162 throw new RuntimeException("TEST FAILED: " + 163 "Pool " + p.getName() + 164 " isCollectionUsageThresholdExceeded() returned true but" + 165 " threshold(" + p.getCollectionUsageThreshold() + 166 ") > used(" + u.getUsed() + ")"); 167 } 168 } 169 170 // disable low memory detection and isCollectionUsageThresholdExceeded() 171 // should return false 172 p.setCollectionUsageThreshold(0); 173 if (p.isCollectionUsageThresholdExceeded()) { 174 throw new RuntimeException("TEST FAILED: " + 175 "Pool " + p.getName() + 176 " isCollectionUsageThresholdExceeded() returned true but threshold = 0"); 177 } 178 } 179 }