Path: blob/master/src/java.management/share/classes/sun/management/MemoryImpl.java
41152 views
/*1* Copyright (c) 2003, 2008, 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.management;2627import java.lang.management.ManagementFactory;28import java.lang.management.MemoryMXBean;29import java.lang.management.MemoryUsage;30import java.lang.management.MemoryNotificationInfo;31import java.lang.management.MemoryManagerMXBean;32import java.lang.management.MemoryPoolMXBean;33import javax.management.ObjectName;34import javax.management.MBeanNotificationInfo;35import javax.management.Notification;36import javax.management.openmbean.CompositeData;3738/**39* Implementation class for the memory subsystem.40* Standard and committed hotspot-specific metrics if any.41*42* ManagementFactory.getMemoryMXBean() returns an instance43* of this class.44*/45class MemoryImpl extends NotificationEmitterSupport46implements MemoryMXBean {4748private final VMManagement jvm;4950private static MemoryPoolMXBean[] pools = null;51private static MemoryManagerMXBean[] mgrs = null;5253/**54* Constructor of MemoryImpl class55*/56MemoryImpl(VMManagement vm) {57this.jvm = vm;58}5960public int getObjectPendingFinalizationCount() {61return jdk.internal.misc.VM.getFinalRefCount();62}6364public void gc() {65Runtime.getRuntime().gc();66}6768// Need to make a VM call to get coherent value69public MemoryUsage getHeapMemoryUsage() {70return getMemoryUsage0(true);71}7273public MemoryUsage getNonHeapMemoryUsage() {74return getMemoryUsage0(false);75}7677public boolean isVerbose() {78return jvm.getVerboseGC();79}8081public void setVerbose(boolean value) {82Util.checkControlAccess();8384setVerboseGC(value);85}8687// The current Hotspot implementation does not support88// dynamically add or remove memory pools & managers.89static synchronized MemoryPoolMXBean[] getMemoryPools() {90if (pools == null) {91pools = getMemoryPools0();92}93return pools;94}95static synchronized MemoryManagerMXBean[] getMemoryManagers() {96if (mgrs == null) {97mgrs = getMemoryManagers0();98}99return mgrs;100}101private static native MemoryPoolMXBean[] getMemoryPools0();102private static native MemoryManagerMXBean[] getMemoryManagers0();103private native MemoryUsage getMemoryUsage0(boolean heap);104private native void setVerboseGC(boolean value);105106private static final String notifName =107"javax.management.Notification";108private static final String[] notifTypes = {109MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,110MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED111};112private static final String[] notifMsgs = {113"Memory usage exceeds usage threshold",114"Memory usage exceeds collection usage threshold"115};116117public MBeanNotificationInfo[] getNotificationInfo() {118return new MBeanNotificationInfo[] {119new MBeanNotificationInfo(notifTypes, notifName, "Memory Notification")120};121}122123private static String getNotifMsg(String notifType) {124for (int i = 0; i < notifTypes.length; i++) {125if (notifType == notifTypes[i]) {126return notifMsgs[i];127}128}129return "Unknown message";130}131132private static long seqNumber = 0;133private static long getNextSeqNumber() {134return ++seqNumber;135}136137static void createNotification(String notifType,138String poolName,139MemoryUsage usage,140long count) {141MemoryImpl mbean = (MemoryImpl) ManagementFactory.getMemoryMXBean();142if (!mbean.hasListeners()) {143// if no listener is registered.144return;145}146long timestamp = System.currentTimeMillis();147String msg = getNotifMsg(notifType);148Notification notif = new Notification(notifType,149mbean.getObjectName(),150getNextSeqNumber(),151timestamp,152msg);153MemoryNotificationInfo info =154new MemoryNotificationInfo(poolName,155usage,156count);157CompositeData cd =158MemoryNotifInfoCompositeData.toCompositeData(info);159notif.setUserData(cd);160mbean.sendNotification(notif);161}162163public ObjectName getObjectName() {164return Util.newObjectName(ManagementFactory.MEMORY_MXBEAN_NAME);165}166167}168169170