Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/management/GarbageCollectorImpl.java
38827 views
/*1* Copyright (c) 2003, 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.management;2627import com.sun.management.GarbageCollectorMXBean;28import com.sun.management.GarbageCollectionNotificationInfo;29import java.lang.management.ManagementFactory;30import java.lang.management.MemoryPoolMXBean;31import java.lang.management.MemoryUsage;3233import com.sun.management.GcInfo;34import javax.management.openmbean.CompositeData;35import javax.management.MBeanInfo;36import javax.management.MBeanAttributeInfo;37import javax.management.ObjectName;38import javax.management.MBeanNotificationInfo;39import javax.management.Notification;40import javax.management.NotificationFilter;41import javax.management.NotificationListener;42import javax.management.ListenerNotFoundException;4344import java.util.List;45import java.util.ListIterator;46import java.util.Map;4748/**49* Implementation class for the garbage collector.50* Standard and committed hotspot-specific metrics if any.51*52* ManagementFactory.getGarbageCollectorMXBeans() returns a list53* of instances of this class.54*/55class GarbageCollectorImpl extends MemoryManagerImpl56implements GarbageCollectorMXBean {5758GarbageCollectorImpl(String name) {59super(name);60}6162public native long getCollectionCount();63public native long getCollectionTime();646566// The memory pools are static and won't be changed.67// TODO: If the hotspot implementation begins to have pools68// dynamically created and removed, this needs to be modified.69private String[] poolNames = null;70synchronized String[] getAllPoolNames() {71if (poolNames == null) {72List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();73poolNames = new String[pools.size()];74int i = 0;75for (MemoryPoolMXBean m : pools) {76poolNames[i++] = m.getName();77}78}79return poolNames;80}8182// Sun JDK extension83private GcInfoBuilder gcInfoBuilder;8485private synchronized GcInfoBuilder getGcInfoBuilder() {86if(gcInfoBuilder == null) {87gcInfoBuilder = new GcInfoBuilder(this, getAllPoolNames());88}89return gcInfoBuilder;90}9192public GcInfo getLastGcInfo() {93GcInfo info = getGcInfoBuilder().getLastGcInfo();94return info;95}9697private final static String notifName =98"javax.management.Notification";99100private final static String[] gcNotifTypes = {101GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION102};103104@Override105public MBeanNotificationInfo[] getNotificationInfo() {106return new MBeanNotificationInfo[]{107new MBeanNotificationInfo(gcNotifTypes,108notifName,109"GC Notification")110};111}112113private static long seqNumber = 0;114private static long getNextSeqNumber() {115return ++seqNumber;116}117118void createGCNotification(long timestamp,119String gcName,120String gcAction,121String gcCause,122GcInfo gcInfo) {123124if (!hasListeners()) {125return;126}127128Notification notif = new Notification(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION,129getObjectName(),130getNextSeqNumber(),131timestamp,132gcName);133GarbageCollectionNotificationInfo info =134new GarbageCollectionNotificationInfo(gcName,135gcAction,136gcCause,137gcInfo);138139CompositeData cd =140GarbageCollectionNotifInfoCompositeData.toCompositeData(info);141notif.setUserData(cd);142sendNotification(notif);143}144145public synchronized void addNotificationListener(NotificationListener listener,146NotificationFilter filter,147Object handback)148{149boolean before = hasListeners();150super.addNotificationListener(listener, filter, handback);151boolean after = hasListeners();152if (!before && after) {153setNotificationEnabled(this, true);154}155}156157public synchronized void removeNotificationListener(NotificationListener listener)158throws ListenerNotFoundException {159boolean before = hasListeners();160super.removeNotificationListener(listener);161boolean after = hasListeners();162if (before && !after) {163setNotificationEnabled(this,false);164}165}166167public synchronized void removeNotificationListener(NotificationListener listener,168NotificationFilter filter,169Object handback)170throws ListenerNotFoundException171{172boolean before = hasListeners();173super.removeNotificationListener(listener,filter,handback);174boolean after = hasListeners();175if (before && !after) {176setNotificationEnabled(this,false);177}178}179180public ObjectName getObjectName() {181return Util.newObjectName(ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE, getName());182}183184native void setNotificationEnabled(GarbageCollectorMXBean gc,185boolean enabled);186187}188189190