Path: blob/master/test/hotspot/jtreg/serviceability/tmtools/jstat/utils/Pools.java
41159 views
/*1* Copyright (c) 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*/22package utils;2324import java.lang.management.ManagementFactory;25import java.lang.management.MemoryPoolMXBean;2627/**28* Utility to obtain memory pools statistics29*30*/31public class Pools {3233private static final String EDEN_SPACE_POOL = "Eden Space";34private static final String OLD_GEN_POOL = "Old Gen";35private static final String METASPACE_POOL = "Metaspace";36private static final String SURVIVOR_SPACE = "Survivor Space";3738public static long getNGMaxSize() {39// NewGen is consists of Eden and two Survivor spaces40return getPoolMaxSize(EDEN_SPACE_POOL) + 2 * getPoolMaxSize(SURVIVOR_SPACE);41}4243public static long getHeapCommittedSize() {44return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getCommitted() / 1024;45}4647public static long getEdenCommittedSize() {48return getPoolCommittedSize(EDEN_SPACE_POOL);49}5051public static long getOldGenCommittedSize() {52return getPoolCommittedSize(OLD_GEN_POOL);53}5455public static long getMetaspaceCommittedSize() {56return getPoolCommittedSize(METASPACE_POOL);57}5859private static long getPoolMaxSize(String poolName) {60long result;61MemoryPoolMXBean pool = findPool(poolName);62if (pool != null) {63if (pool.getUsage().getMax() == -1) {64result = -1;65} else {66result = pool.getUsage().getCommitted() / 1024;67}68} else {69throw new RuntimeException("Pool '" + poolName + "' wasn't found");70}71log("Max size of the pool '" + poolName + "' is " + result);72return result;73}7475private static long getPoolCommittedSize(String poolName) {76long result;77MemoryPoolMXBean pool = findPool(poolName);78if (pool != null) {79if (pool.getUsage().getCommitted() == -1) {80result = -1;81} else {82result = pool.getUsage().getCommitted() / 1024;83}84} else {85throw new RuntimeException("Pool '" + poolName + "' wasn't found");86}87log("Committed size of the pool '" + poolName + "' is " + result);88return result;89}9091private static MemoryPoolMXBean findPool(String poolName) {92for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {93if (pool.getName().contains(poolName)) {94return pool;95}96}97return null;98}99100private static void log(String s) {101System.out.println(s);102}103104}105106107