Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/MemoryMXBean/MemoryUtil.java
38821 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.print(INDENT + "Manager = [");57String[] mgrs = pool.getMemoryManagerNames();58for (int i = 0; i < mgrs.length; i++) {59System.out.print(mgrs[i]);60if (i < (mgrs.length - 1)) {61System.out.print(" | ");62}63}64System.out.println("]");65}6667public static void printMemoryPools(List pools) {68ListIterator iter = pools.listIterator();69System.out.println(INDENT + "Number of memory pools = " + pools.size());70while (iter.hasNext()) {71MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();72printMemoryPool(pool);73}74}7576public static void printMemoryManager(MemoryManagerMXBean mgr) {77if (mgr instanceof GarbageCollectorMXBean) {78GarbageCollectorMXBean gc = (GarbageCollectorMXBean) mgr;79System.out.println(INDENT + "Garbage Collector name: " + gc.getName());80System.out.println(INDENT + "GC Count: " + gc.getCollectionCount());81System.out.println(INDENT + "GC Time : " + gc.getCollectionTime());82} else {83System.out.println(INDENT + "Memory Manager name: " + mgr.getName());84}8586System.out.print("Pool = [");87String[] pools = mgr.getMemoryPoolNames();88for (int i = 0; i < pools.length; i++) {89System.out.print(INDENT + pools[i]);90if (i < (pools.length - 1)) {91System.out.print(" | ");92}93}94System.out.println("]");95}9697public static void printMemoryManagers(List managers) {98ListIterator iter = managers.listIterator();99System.out.println(INDENT + "Number of memory managers = " +100managers.size());101while (iter.hasNext()) {102MemoryManagerMXBean mgr = (MemoryManagerMXBean) iter.next();103printMemoryManager(mgr);104}105}106107public static void printMemoryNotificationInfo108(MemoryNotificationInfo minfo, String type) {109System.out.print("Notification for " + minfo.getPoolName());110System.out.print(" [type = " + type);111System.out.println(" count = " + minfo.getCount() + "]");112System.out.println(INDENT + "usage = " + minfo.getUsage());113}114}115116117