Path: blob/master/src/java.management/share/classes/java/lang/management/MemoryNotificationInfo.java
41159 views
/*1* Copyright (c) 2003, 2017, 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 java.lang.management;26import javax.management.openmbean.CompositeData;27import sun.management.MemoryNotifInfoCompositeData;2829/**30* The information about a memory notification.31*32* <p>33* A memory notification is emitted by {@link MemoryMXBean}34* when the Java virtual machine detects that the memory usage35* of a memory pool is exceeding a threshold value.36* The notification emitted will contain the memory notification37* information about the detected condition:38* <ul>39* <li>The name of the memory pool.</li>40* <li>The memory usage of the memory pool when the notification41* was constructed.</li>42* <li>The number of times that the memory usage has crossed43* a threshold when the notification was constructed.44* For usage threshold notifications, this count will be the45* {@link MemoryPoolMXBean#getUsageThresholdCount usage threshold46* count}. For collection threshold notifications,47* this count will be the48* {@link MemoryPoolMXBean#getCollectionUsageThresholdCount49* collection usage threshold count}.50* </li>51* </ul>52*53* <p>54* A {@link CompositeData CompositeData} representing55* the {@code MemoryNotificationInfo} object56* is stored in the57* {@link javax.management.Notification#setUserData user data}58* of a {@link javax.management.Notification notification}.59* The {@link #from from} method is provided to convert from60* a {@code CompositeData} to a {@code MemoryNotificationInfo}61* object. For example:62*63* <blockquote><pre>64* Notification notif;65*66* // receive the notification emitted by MemoryMXBean and set to notif67* ...68*69* String notifType = notif.getType();70* if (notifType.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||71* notifType.equals(MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {72* // retrieve the memory notification information73* CompositeData cd = (CompositeData) notif.getUserData();74* MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);75* ....76* }77* </pre></blockquote>78*79* <p>80* The types of notifications emitted by {@code MemoryMXBean} are:81* <ul>82* <li>A {@link #MEMORY_THRESHOLD_EXCEEDED83* usage threshold exceeded notification}.84* <br>This notification will be emitted when85* the memory usage of a memory pool is increased and has reached86* or exceeded its87* <a href="MemoryPoolMXBean.html#UsageThreshold"> usage threshold</a> value.88* Subsequent crossing of the usage threshold value does not cause89* further notification until the memory usage has returned90* to become less than the usage threshold value.91* </li>92* <li>A {@link #MEMORY_COLLECTION_THRESHOLD_EXCEEDED93* collection usage threshold exceeded notification}.94* <br>This notification will be emitted when95* the memory usage of a memory pool is greater than or equal to its96* <a href="MemoryPoolMXBean.html#CollectionThreshold">97* collection usage threshold</a> after the Java virtual machine98* has expended effort in recycling unused objects in that99* memory pool.</li>100* </ul>101*102* @author Mandy Chung103* @since 1.5104*105*/106public class MemoryNotificationInfo {107private final String poolName;108private final MemoryUsage usage;109private final long count;110111/**112* Notification type denoting that113* the memory usage of a memory pool has114* reached or exceeded its115* <a href="MemoryPoolMXBean.html#UsageThreshold"> usage threshold</a> value.116* This notification is emitted by {@link MemoryMXBean}.117* Subsequent crossing of the usage threshold value does not cause118* further notification until the memory usage has returned119* to become less than the usage threshold value.120* The value of this notification type is121* {@code java.management.memory.threshold.exceeded}.122*/123public static final String MEMORY_THRESHOLD_EXCEEDED =124"java.management.memory.threshold.exceeded";125126/**127* Notification type denoting that128* the memory usage of a memory pool is greater than or equal to its129* <a href="MemoryPoolMXBean.html#CollectionThreshold">130* collection usage threshold</a> after the Java virtual machine131* has expended effort in recycling unused objects in that132* memory pool.133* This notification is emitted by {@link MemoryMXBean}.134* The value of this notification type is135* {@code java.management.memory.collection.threshold.exceeded}.136*/137public static final String MEMORY_COLLECTION_THRESHOLD_EXCEEDED =138"java.management.memory.collection.threshold.exceeded";139140/**141* Constructs a {@code MemoryNotificationInfo} object.142*143* @param poolName The name of the memory pool which triggers this notification.144* @param usage Memory usage of the memory pool.145* @param count The threshold crossing count.146*/147public MemoryNotificationInfo(String poolName,148MemoryUsage usage,149long count) {150if (poolName == null) {151throw new NullPointerException("Null poolName");152}153if (usage == null) {154throw new NullPointerException("Null usage");155}156157this.poolName = poolName;158this.usage = usage;159this.count = count;160}161162MemoryNotificationInfo(CompositeData cd) {163MemoryNotifInfoCompositeData.validateCompositeData(cd);164165this.poolName = MemoryNotifInfoCompositeData.getPoolName(cd);166this.usage = MemoryNotifInfoCompositeData.getUsage(cd);167this.count = MemoryNotifInfoCompositeData.getCount(cd);168}169170/**171* Returns the name of the memory pool that triggers this notification.172* The memory pool usage has crossed a threshold.173*174* @return the name of the memory pool that triggers this notification.175*/176public String getPoolName() {177return poolName;178}179180/**181* Returns the memory usage of the memory pool182* when this notification was constructed.183*184* @return the memory usage of the memory pool185* when this notification was constructed.186*/187public MemoryUsage getUsage() {188return usage;189}190191/**192* Returns the number of times that the memory usage has crossed193* a threshold when the notification was constructed.194* For usage threshold notifications, this count will be the195* {@link MemoryPoolMXBean#getUsageThresholdCount threshold196* count}. For collection threshold notifications,197* this count will be the198* {@link MemoryPoolMXBean#getCollectionUsageThresholdCount199* collection usage threshold count}.200*201* @return the number of times that the memory usage has crossed202* a threshold when the notification was constructed.203*/204public long getCount() {205return count;206}207208/**209* Returns a {@code MemoryNotificationInfo} object represented by the210* given {@code CompositeData}.211* The given {@code CompositeData} must contain212* the following attributes:213* <table class="striped" style="margin-left:2em">214* <caption style="display:none">The attributes and the types the given CompositeData contains</caption>215* <thead>216* <tr>217* <th scope="col">Attribute Name</th>218* <th scope="col">Type</th>219* </tr>220* </thead>221* <tbody style="text-align:left">222* <tr>223* <th scope="row">poolName</th>224* <td>{@code java.lang.String}</td>225* </tr>226* <tr>227* <th scope="row">usage</th>228* <td>{@code javax.management.openmbean.CompositeData}</td>229* </tr>230* <tr>231* <th scope="row">count</th>232* <td>{@code java.lang.Long}</td>233* </tr>234* </tbody>235* </table>236*237* @param cd {@code CompositeData} representing a238* {@code MemoryNotificationInfo}239*240* @throws IllegalArgumentException if {@code cd} does not241* represent a {@code MemoryNotificationInfo} object.242*243* @return a {@code MemoryNotificationInfo} object represented244* by {@code cd} if {@code cd} is not {@code null};245* {@code null} otherwise.246*/247public static MemoryNotificationInfo from(CompositeData cd) {248if (cd == null) {249return null;250}251252if (cd instanceof MemoryNotifInfoCompositeData) {253return ((MemoryNotifInfoCompositeData) cd).getMemoryNotifInfo();254} else {255return new MemoryNotificationInfo(cd);256}257}258}259260261