Path: blob/master/test/jdk/java/lang/management/MemoryMXBean/MemoryUtil.java
41155 views
/*1* Copyright (c) 2004, 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* @bug 498228925* @summary Utility class.26* @author Mandy Chung27*/2829import java.lang.management.*;30import java.util.*;3132public class MemoryUtil {3334private static String INDENT = " ";35private static String formatSize(String name, long value) {36StringBuffer buf = new StringBuffer(name + " = " + value);37if (value > 0) {38buf.append(" (" + (value >> 10) + "K)");39}40return buf.toString();41}42public static void printMemoryUsage(MemoryUsage usage) {43System.out.println(INDENT + formatSize("Initial size ", usage.getInit()));44System.out.println(INDENT + formatSize("Used size ", usage.getUsed()));45System.out.println(INDENT + formatSize("Committd size ", usage.getCommitted()));46System.out.println(INDENT + formatSize("Max size ", usage.getMax()));47}4849public static void printMemoryPool(MemoryPoolMXBean pool) {50System.out.println(INDENT + "Memory Pool name: " + pool.getName());51System.out.println(INDENT + "Type: " + pool.getType());52System.out.println(INDENT + "Memory Usage: " +53pool.getUsage());54System.out.println(INDENT + "Threshold: " +55(pool.isUsageThresholdSupported() ? pool.getUsageThreshold() : -1));56System.out.println(INDENT + "ThresholdCount: " +57(pool.isUsageThresholdSupported() ? pool.getUsageThresholdCount() : -1));58System.out.print(INDENT + "Manager = [");59String[] mgrs = pool.getMemoryManagerNames();60for (int i = 0; i < mgrs.length; i++) {61System.out.print(mgrs[i]);62if (i < (mgrs.length - 1)) {63System.out.print(" | ");64}65}66System.out.println("]");67}6869public static void printMemoryPools(List pools) {70ListIterator iter = pools.listIterator();71System.out.println(INDENT + "Number of memory pools = " + pools.size());72while (iter.hasNext()) {73MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();74printMemoryPool(pool);75}76}7778public static void printMemoryManager(MemoryManagerMXBean mgr) {79if (mgr instanceof GarbageCollectorMXBean) {80GarbageCollectorMXBean gc = (GarbageCollectorMXBean) mgr;81System.out.println(INDENT + "Garbage Collector name: " + gc.getName());82System.out.println(INDENT + "GC Count: " + gc.getCollectionCount());83System.out.println(INDENT + "GC Time : " + gc.getCollectionTime());84} else {85System.out.println(INDENT + "Memory Manager name: " + mgr.getName());86}8788System.out.print("Pool = [");89String[] pools = mgr.getMemoryPoolNames();90for (int i = 0; i < pools.length; i++) {91System.out.print(INDENT + pools[i]);92if (i < (pools.length - 1)) {93System.out.print(" | ");94}95}96System.out.println("]");97}9899public static void printMemoryManagers(List managers) {100ListIterator iter = managers.listIterator();101System.out.println(INDENT + "Number of memory managers = " +102managers.size());103while (iter.hasNext()) {104MemoryManagerMXBean mgr = (MemoryManagerMXBean) iter.next();105printMemoryManager(mgr);106}107}108109public static void printMemoryNotificationInfo110(MemoryNotificationInfo minfo, String type) {111System.out.print("Notification for " + minfo.getPoolName());112System.out.print(" [type = " + type);113System.out.println(" count = " + minfo.getCount() + "]");114System.out.println(INDENT + "usage = " + minfo.getUsage());115}116}117118119