Path: blob/master/src/jdk.jconsole/share/classes/sun/tools/jconsole/MemoryPoolProxy.java
40948 views
/*1* Copyright (c) 2004, 2012, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jconsole;2627import javax.management.ObjectName;28import java.lang.management.MemoryPoolMXBean;29import java.lang.management.MemoryUsage;30import com.sun.management.GarbageCollectorMXBean;31import com.sun.management.GcInfo;32import java.util.HashMap;33import java.util.Set;34import java.util.Map;3536import static java.lang.management.ManagementFactory.*;3738public class MemoryPoolProxy {39private String poolName;40private ProxyClient client;41private MemoryPoolMXBean pool;42private Map<ObjectName,Long> gcMBeans;43private GcInfo lastGcInfo;4445public MemoryPoolProxy(ProxyClient client, ObjectName poolName) throws java.io.IOException {46this.client = client;47this.pool = client.getMXBean(poolName, MemoryPoolMXBean.class);48this.poolName = this.pool.getName();49this.gcMBeans = new HashMap<ObjectName,Long>();50this.lastGcInfo = null;5152String[] mgrNames = pool.getMemoryManagerNames();53for (String name : mgrNames) {54try {55ObjectName mbeanName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE +56",name=" + name);57if (client.isRegistered(mbeanName)) {58gcMBeans.put(mbeanName, 0L);59}60} catch (Exception e) {61assert false;62}6364}65}6667public boolean isCollectedMemoryPool() {68return (gcMBeans.size() != 0);69}7071public MemoryPoolStat getStat() throws java.io.IOException {72long usageThreshold = (pool.isUsageThresholdSupported()73? pool.getUsageThreshold()74: -1);75long collectThreshold = (pool.isCollectionUsageThresholdSupported()76? pool.getCollectionUsageThreshold()77: -1);78long lastGcStartTime = 0;79long lastGcEndTime = 0;80MemoryUsage beforeGcUsage = null;81MemoryUsage afterGcUsage = null;82long gcId = 0;83if (lastGcInfo != null) {84gcId = lastGcInfo.getId();85lastGcStartTime = lastGcInfo.getStartTime();86lastGcEndTime = lastGcInfo.getEndTime();87beforeGcUsage = lastGcInfo.getMemoryUsageBeforeGc().get(poolName);88afterGcUsage = lastGcInfo.getMemoryUsageAfterGc().get(poolName);89}9091Set<Map.Entry<ObjectName,Long>> set = gcMBeans.entrySet();92for (Map.Entry<ObjectName,Long> e : set) {93GarbageCollectorMXBean gc =94client.getMXBean(e.getKey(),95com.sun.management.GarbageCollectorMXBean.class);96Long gcCount = e.getValue();97Long newCount = gc.getCollectionCount();98if (newCount > gcCount) {99gcMBeans.put(e.getKey(), newCount);100lastGcInfo = gc.getLastGcInfo();101if (lastGcInfo.getEndTime() > lastGcEndTime) {102gcId = lastGcInfo.getId();103lastGcStartTime = lastGcInfo.getStartTime();104lastGcEndTime = lastGcInfo.getEndTime();105beforeGcUsage = lastGcInfo.getMemoryUsageBeforeGc().get(poolName);106afterGcUsage = lastGcInfo.getMemoryUsageAfterGc().get(poolName);107assert(beforeGcUsage != null);108assert(afterGcUsage != null);109}110}111}112113MemoryUsage usage = pool.getUsage();114return new MemoryPoolStat(poolName,115usageThreshold,116usage,117gcId,118lastGcStartTime,119lastGcEndTime,120collectThreshold,121beforeGcUsage,122afterGcUsage);123}124}125126127