Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/management/GarbageCollectionNotificationInfo.java
38831 views
/*1* Copyright (c) 2011, 2013, 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 com.sun.management;2627import javax.management.Notification;28import javax.management.openmbean.CompositeData;29import javax.management.openmbean.CompositeDataView;30import javax.management.openmbean.CompositeType;31import java.util.Collection;32import java.util.Collections;33import sun.management.GarbageCollectionNotifInfoCompositeData;3435/**36* The information about a garbage collection37*38* <p>39* A garbage collection notification is emitted by {@link GarbageCollectorMXBean}40* when the Java virtual machine completes a garbage collection action41* The notification emitted will contain the garbage collection notification42* information about the status of the memory:43* <u1>44* <li>The name of the garbage collector used perform the collection.</li>45* <li>The action performed by the garbage collector.</li>46* <li>The cause of the garbage collection action.</li>47* <li>A {@link GcInfo} object containing some statistics about the GC cycle48(start time, end time) and the memory usage before and after49the GC cycle.</li>50* </u1>51*52* <p>53* A {@link CompositeData CompositeData} representing54* the {@code GarbageCollectionNotificationInfo} object55* is stored in the56* {@linkplain javax.management.Notification#setUserData userdata}57* of a {@linkplain javax.management.Notification notification}.58* The {@link #from from} method is provided to convert from59* a {@code CompositeData} to a {@code GarbageCollectionNotificationInfo}60* object. For example:61*62* <blockquote><pre>63* Notification notif;64*65* // receive the notification emitted by a GarbageCollectorMXBean and set to notif66* ...67*68* String notifType = notif.getType();69* if (notifType.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {70* // retrieve the garbage collection notification information71* CompositeData cd = (CompositeData) notif.getUserData();72* GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from(cd);73* ....74* }75* </pre></blockquote>76*77* <p>78* The type of the notification emitted by a {@code GarbageCollectorMXBean} is:79* <ul>80* <li>A {@linkplain #GARBAGE_COLLECTION_NOTIFICATION garbage collection notification}.81* <br>Used by every notification emitted by the garbage collector, the details about82* the notification are provided in the {@linkplain #getGcAction action} String83* <p></li>84* </ul>85**/8687@jdk.Exported88public class GarbageCollectionNotificationInfo implements CompositeDataView {8990private final String gcName;91private final String gcAction;92private final String gcCause;93private final GcInfo gcInfo;94private final CompositeData cdata;9596/**97* Notification type denoting that98* the Java virtual machine has completed a garbage collection cycle.99* This notification is emitted by a {@link GarbageCollectorMXBean}.100* The value of this notification type is101* {@code com.sun.management.gc.notification}.102*/103public static final String GARBAGE_COLLECTION_NOTIFICATION =104"com.sun.management.gc.notification";105106/**107* Constructs a {@code GarbageCollectionNotificationInfo} object.108*109* @param gcName The name of the garbage collector used to perform the collection110* @param gcAction The name of the action performed by the garbage collector111* @param gcCause The cause the garbage collection action112* @param gcInfo a GcInfo object providing statistics about the GC cycle113*/114public GarbageCollectionNotificationInfo(String gcName,115String gcAction,116String gcCause,117GcInfo gcInfo) {118if (gcName == null) {119throw new NullPointerException("Null gcName");120}121if (gcAction == null) {122throw new NullPointerException("Null gcAction");123}124if (gcCause == null) {125throw new NullPointerException("Null gcCause");126}127this.gcName = gcName;128this.gcAction = gcAction;129this.gcCause = gcCause;130this.gcInfo = gcInfo;131this.cdata = new GarbageCollectionNotifInfoCompositeData(this);132}133134GarbageCollectionNotificationInfo(CompositeData cd) {135GarbageCollectionNotifInfoCompositeData.validateCompositeData(cd);136137this.gcName = GarbageCollectionNotifInfoCompositeData.getGcName(cd);138this.gcAction = GarbageCollectionNotifInfoCompositeData.getGcAction(cd);139this.gcCause = GarbageCollectionNotifInfoCompositeData.getGcCause(cd);140this.gcInfo = GarbageCollectionNotifInfoCompositeData.getGcInfo(cd);141this.cdata = cd;142}143144/**145* Returns the name of the garbage collector used to perform the collection146*147* @return the name of the garbage collector used to perform the collection148*/149public String getGcName() {150return gcName;151}152153/**154* Returns the action of the performed by the garbage collector155*156* @return the the action of the performed by the garbage collector157*/158public String getGcAction() {159return gcAction;160}161162/**163* Returns the cause the garbage collection164*165* @return the the cause the garbage collection166*/167public String getGcCause() {168return gcCause;169}170171/**172* Returns the GC information related to the last garbage collection173*174* @return the GC information related to the175* last garbage collection176*/177public GcInfo getGcInfo() {178return gcInfo;179}180181/**182* Returns a {@code GarbageCollectionNotificationInfo} object represented by the183* given {@code CompositeData}.184* The given {@code CompositeData} must contain185* the following attributes:186* <blockquote>187* <table border>188* <tr>189* <th align=left>Attribute Name</th>190* <th align=left>Type</th>191* </tr>192* <tr>193* <td>gcName</td>194* <td>{@code java.lang.String}</td>195* </tr>196* <tr>197* <td>gcAction</td>198* <td>{@code java.lang.String}</td>199* </tr>200* <tr>201* <td>gcCause</td>202* <td>{@code java.lang.String}</td>203* </tr>204* <tr>205* <td>gcInfo</td>206* <td>{@code javax.management.openmbean.CompositeData}</td>207* </tr>208* </table>209* </blockquote>210*211* @param cd {@code CompositeData} representing a212* {@code GarbageCollectionNotificationInfo}213*214* @throws IllegalArgumentException if {@code cd} does not215* represent a {@code GarbaageCollectionNotificationInfo} object.216*217* @return a {@code GarbageCollectionNotificationInfo} object represented218* by {@code cd} if {@code cd} is not {@code null};219* {@code null} otherwise.220*/221public static GarbageCollectionNotificationInfo from(CompositeData cd) {222if (cd == null) {223return null;224}225226if (cd instanceof GarbageCollectionNotifInfoCompositeData) {227return ((GarbageCollectionNotifInfoCompositeData) cd).getGarbageCollectionNotifInfo();228} else {229return new GarbageCollectionNotificationInfo(cd);230}231}232233public CompositeData toCompositeData(CompositeType ct) {234return cdata;235}236237}238239240