2362N/A * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * The management interface for a memory pool. A memory pool 0N/A * represents the memory resource managed by the Java virtual machine 0N/A * and is managed by one or more {@link MemoryManagerMXBean memory managers}. 0N/A * <p> A Java virtual machine has one or more instances of the 0N/A * implementation class of this interface. An instance 0N/A * implementing this interface is 0N/A * that can be obtained by calling 0N/A * the {@link ManagementFactory#getMemoryPoolMXBeans} method or 0N/A * from the {@link ManagementFactory#getPlatformMBeanServer 0N/A * platform <tt>MBeanServer</tt>} method. 0N/A * <p>The <tt>ObjectName</tt> for uniquely identifying the MXBean for 0N/A * a memory pool within an <tt>MBeanServer</tt> is: 0N/A * {@link ManagementFactory#MEMORY_POOL_MXBEAN_DOMAIN_TYPE 0N/A * <tt>java.lang:type=MemoryPool</tt>}<tt>,name=</tt><i>pool's name</i> 178N/A * It can be obtained by calling the 178N/A * {@link PlatformManagedObject#getObjectName} method. 0N/A * <h4>Memory Type</h4> 0N/A * <p>The Java virtual machine has a heap for object allocation and also 0N/A * maintains non-heap memory for the method area and the Java virtual 0N/A * machine execution. The Java virtual machine can have one or more 0N/A * memory pools. Each memory pool represents a memory area 0N/A * of one of the following types: 0N/A * <li>{@link MemoryType#HEAP heap}</li> 0N/A * <li>{@link MemoryType#NON_HEAP non-heap}</li> 0N/A * <h4>Memory Usage Monitoring</h4> 0N/A * A memory pool has the following attributes: 0N/A * <li><a href="#Usage">Memory usage</a></li> 0N/A * <li><a href="#PeakUsage">Peak memory usage</a></li> 0N/A * <li><a href="#UsageThreshold">Usage Threshold</a></li> 0N/A * <li><a href="#CollectionThreshold">Collection Usage Threshold</a> 0N/A * (only supported by some <em>garbage-collected</em> memory pools)</li> 0N/A * <h4><a name="Usage">1. Memory Usage</a></h4> 0N/A * The {@link #getUsage} method provides an estimate 0N/A * of the current usage of a memory pool. 0N/A * For a garbage-collected memory pool, the amount of used memory 0N/A * includes the memory occupied by all objects in the pool 0N/A * including both <em>reachable</em> and <em>unreachable</em> objects. 0N/A * <p>In general, this method is a lightweight operation for getting 0N/A * an approximate memory usage. For some memory pools, for example, 0N/A * when objects are not packed contiguously, this method may be 0N/A * an expensive operation that requires some computation to determine 0N/A * the current memory usage. An implementation should document when 0N/A * <h4><a name="PeakUsage">2. Peak Memory Usage</a></h4> 0N/A * The Java virtual machine maintains the peak memory usage of a memory 0N/A * pool since the virtual machine was started or the peak was reset. 0N/A * The peak memory usage is returned by the {@link #getPeakUsage} method 0N/A * and reset by calling the {@link #resetPeakUsage} method. 0N/A * <h4><a name="UsageThreshold">3. Usage Threshold</a></h4> 0N/A * Each memory pool has a manageable attribute 0N/A * called the <i>usage threshold</i> which has a default value supplied 0N/A * by the Java virtual machine. The default value is platform-dependent. 0N/A * The usage threshold can be set via the 0N/A * {@link #setUsageThreshold setUsageThreshold} method. 0N/A * If the threshold is set to a positive value, the usage threshold crossing 0N/A * checking is enabled in this memory pool. 0N/A * If the usage threshold is set to zero, usage 0N/A * threshold crossing checking on this memory pool is disabled. 0N/A * The {@link MemoryPoolMXBean#isUsageThresholdSupported} method can 0N/A * be used to determine if this functionality is supported. 0N/A * A Java virtual machine performs usage threshold crossing checking on a 0N/A * memory pool basis at its best appropriate time, typically, 0N/A * at garbage collection time. 0N/A * Each memory pool maintains a {@link #getUsageThresholdCount 0N/A * usage threshold count} that will get incremented 0N/A * every time when the Java virtual machine 0N/A * detects that the memory pool usage is crossing the threshold. 0N/A * This manageable usage threshold attribute is designed for monitoring the 0N/A * increasing trend of memory usage with low overhead. 0N/A * Usage threshold may not be appropriate for some memory pools. 0N/A * For example, a generational garbage collector, a common garbage collection 0N/A * algorithm used in many Java virtual machine implementations, 0N/A * manages two or more generations segregating objects by age. 0N/A * Most of the objects are allocated in 0N/A * the <em>youngest generation</em> (say a nursery memory pool). 0N/A * The nursery memory pool is designed to be filled up and 0N/A * collecting the nursery memory pool will free most of its memory space 0N/A * since it is expected to contain mostly short-lived objects 0N/A * and mostly are unreachable at garbage collection time. 0N/A * In this case, it is more appropriate for the nursery memory pool 0N/A * not to support a usage threshold. In addition, 0N/A * if the cost of an object allocation 0N/A * in one memory pool is very low (for example, just atomic pointer exchange), 0N/A * the Java virtual machine would probably not support the usage threshold 0N/A * for that memory pool since the overhead in comparing the usage with 0N/A * the threshold is higher than the cost of object allocation. 0N/A * The memory usage of the system can be monitored using 0N/A * <a href="#Polling">polling</a> or 0N/A * <a href="#ThresholdNotification">threshold notification</a> mechanisms. 0N/A * <li><a name="Polling"><b>Polling</b></a> 0N/A * An application can continuously monitor its memory usage 0N/A * by calling either the {@link #getUsage} method for all 0N/A * memory pools or the {@link #isUsageThresholdExceeded} method 0N/A * for those memory pools that support a usage threshold. 0N/A * Below is example code that has a thread delicated for 0N/A * task distribution and processing. At every interval, 0N/A * it will determine if it should receive and process new tasks based 0N/A * on its memory usage. If the memory usage exceeds its usage threshold, 0N/A * it will redistribute all outstanding tasks to other VMs and 0N/A * stop receiving new tasks until the memory usage returns 0N/A * below its usage threshold. 0N/A * // Assume the usage threshold is supported for this pool. 0N/A * // Set the threshold to myThreshold above which no new tasks 0N/A * // should be taken. 0N/A * pool.setUsageThreshold(myThreshold); 0N/A * boolean lowMemory = false; 0N/A * if (pool.isUsageThresholdExceeded()) { 0N/A * // potential low memory, so redistribute tasks to other VMs 0N/A * redistributeTasks(); 0N/A * // stop receiving new tasks 0N/A * stopReceivingTasks(); 0N/A * // resume receiving tasks 0N/A * lowMemory = false; 0N/A * resumeReceivingTasks(); 0N/A * // processing outstanding task 0N/A * // sleep for sometime 0N/A * Thread.sleep(sometime); 0N/A * } catch (InterruptedException e) { 0N/A * The above example does not differentiate the case where 0N/A * the memory usage has temporarily dropped below the usage threshold 0N/A * from the case where the memory usage remains above the threshould 0N/A * between two iterations. The usage threshold count returned by 0N/A * the {@link #getUsageThresholdCount} method 0N/A * can be used to determine 0N/A * if the memory usage has returned below the threshold 0N/A * between two polls. 0N/A * Below shows another example that takes some action if a 0N/A * memory pool is under low memory and ignores the memory usage 0N/A * changes during the action processing time. 0N/A * // Assume the usage threshold is supported for this pool. 0N/A * // Set the threshold to myThreshold which determines if 0N/A * // the application will take some action under low memory condition. 0N/A * pool.setUsageThreshold(myThreshold); 0N/A * int prevCrossingCount = 0; 0N/A * // A busy loop to detect when the memory usage 0N/A * // has exceeded the threshold. 0N/A * while (!pool.isUsageThresholdExceeded() || 0N/A * pool.getUsageThresholdCount() == prevCrossingCount) { 0N/A * Thread.sleep(sometime) 0N/A * } catch (InterruptException e) { 0N/A * // Do some processing such as check for memory usage 0N/A * // and issue a warning 0N/A * // Gets the current threshold count. The busy loop will then 0N/A * // ignore any crossing of threshold happens during the processing. 0N/A * prevCrossingCount = pool.getUsageThresholdCount(); 0N/A * <li><a name="ThresholdNotification"><b>Usage Threshold Notifications</b></a> 0N/A * Usage threshold notification will be emitted by {@link MemoryMXBean}. 0N/A * When the Java virtual machine detects that the memory usage of 0N/A * a memory pool has reached or exceeded the usage threshold 0N/A * the virtual machine will trigger the <tt>MemoryMXBean</tt> to emit an 0N/A * {@link MemoryNotificationInfo#MEMORY_THRESHOLD_EXCEEDED 0N/A * usage threshold exceeded notification}. 0N/A * Another usage threshold exceeded notification will not be 0N/A * generated until the usage has fallen below the threshold and 0N/A * then exceeded it again. 0N/A * Below is an example code implementing the same logic as the 0N/A * first example above but using the usage threshold notification 0N/A * mechanism to detect low memory conditions instead of polling. 0N/A * In this example code, upon receiving notification, the notification 0N/A * listener notifies another thread to perform the actual action 0N/A * such as to redistribute outstanding tasks, stop receiving tasks, 0N/A * or resume receiving tasks. 0N/A * The <tt>handleNotification</tt> method should be designed to 0N/A * do a very minimal amount of work and return without delay to avoid 0N/A * causing delay in delivering subsequent notifications. Time-consuming 0N/A * actions should be performed by a separate thread. 0N/A * The notification listener may be invoked by multiple threads 0N/A * concurrently; so the tasks performed by the listener 0N/A * should be properly synchronized. 0N/A * class MyListener implements javax.management.NotificationListener { 0N/A * public void handleNotification(Notification notification, Object handback) { 0N/A * String notifType = notification.getType(); 0N/A * if (notifType.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) { 0N/A * // potential low memory, notify another thread 0N/A * // to redistribute outstanding tasks to other VMs 0N/A * // and stop receiving new tasks. 0N/A * notifyAnotherThread(lowMemory); 0N/A * // Register MyListener with MemoryMXBean 0N/A * MemoryMXBean mbean = ManagementFactory.getMemoryMXBean(); 0N/A * NotificationEmitter emitter = (NotificationEmitter) mbean; 0N/A * MyListener listener = new MyListener(); 0N/A * emitter.addNotificationListener(listener, null, null); 0N/A * // Assume this pool supports a usage threshold. 0N/A * // Set the threshold to myThreshold above which no new tasks 0N/A * // should be taken. 0N/A * pool.setUsageThreshold(myThreshold); 0N/A * // Usage threshold detection is enabled and notification will be 0N/A * // handled by MyListener. Continue for other processing. 0N/A * There is no guarantee about when the <tt>MemoryMXBean</tt> will emit 0N/A * a threshold notification and when the notification will be delivered. 0N/A * When a notification listener is invoked, the memory usage of 0N/A * the memory pool may have crossed the usage threshold more 0N/A * The {@link MemoryNotificationInfo#getCount} method returns the number 0N/A * of times that the memory usage has crossed the usage threshold 0N/A * at the point in time when the notification was constructed. 0N/A * It can be compared with the current usage threshold count returned 0N/A * by the {@link #getUsageThresholdCount} method to determine if 0N/A * such situation has occurred. 0N/A * <h4><a name="CollectionThreshold">4. Collection Usage Threshold</a></h4> 0N/A * Collection usage threshold is a manageable attribute only applicable 0N/A * to some garbage-collected memory pools. 0N/A * After a Java virtual machine has expended effort in reclaiming memory 0N/A * space by recycling unused objects in a memory pool at garbage collection 0N/A * time, some number of bytes in the memory pools that are garbaged 0N/A * collected will still be in use. The collection usage threshold 0N/A * allows a value to be set for this number of bytes such 0N/A * that if the threshold is exceeded, 0N/A * a {@link MemoryNotificationInfo#MEMORY_THRESHOLD_EXCEEDED 0N/A * collection usage threshold exceeded notification} 0N/A * will be emitted by the {@link MemoryMXBean}. 0N/A * In addition, the {@link #getCollectionUsageThresholdCount 0N/A * collection usage threshold count} will then be incremented. 0N/A * The {@link MemoryPoolMXBean#isCollectionUsageThresholdSupported} method can 0N/A * be used to determine if this functionality is supported. 0N/A * A Java virtual machine performs collection usage threshold checking 0N/A * on a memory pool basis. This checking is enabled if the collection 0N/A * usage threshold is set to a positive value. 0N/A * If the collection usage threshold is set to zero, this checking 0N/A * is disabled on this memory pool. Default value is zero. 0N/A * The Java virtual machine performs the collection usage threshold 0N/A * checking at garbage collection time. 0N/A * Some garbage-collected memory pools may 0N/A * choose not to support the collection usage threshold. For example, 0N/A * a memory pool is only managed by a continuous concurrent garbage 0N/A * collector. Objects can be allocated in this memory pool by some thread 0N/A * while the unused objects are reclaimed by the concurrent garbage 0N/A * collector simultaneously. Unless there is a well-defined 0N/A * garbage collection time which is the best appropriate time 0N/A * to check the memory usage, the collection usage threshold should not 0N/A * The collection usage threshold is designed for monitoring the memory usage 0N/A * after the Java virtual machine has expended effort in reclaiming 0N/A * memory space. The collection usage could also be monitored 0N/A * by the polling and threshold notification mechanism 0N/A * described above for the <a href="#UsageThreshold">usage threshold</a> 0N/A * in a similar fashion. 178N/A * @see ManagementFactory#getPlatformMXBeans(Class) 0N/A * JMX Specification.</a> 0N/A * Ways to Access MXBeans</a> 0N/A * @author Mandy Chung 0N/A * Returns the name representing this memory pool. 0N/A * @return the name of this memory pool. 0N/A * Returns the type of this memory pool. 0N/A * <b>MBeanServer access</b>:<br> 0N/A * The mapped type of <tt>MemoryType</tt> is <tt>String</tt> 0N/A * and the value is the name of the <tt>MemoryType</tt>. 0N/A * @return the type of this memory pool. 0N/A * Returns an estimate of the memory usage of this memory pool. 0N/A * This method returns <tt>null</tt> 0N/A * if this memory pool is not valid (i.e. no longer exists). 0N/A * This method requests the Java virtual machine to make 0N/A * a best-effort estimate of the current memory usage of this 0N/A * memory pool. For some memory pools, this method may be an 0N/A * expensive operation that requires some computation to determine 0N/A * the estimate. An implementation should document when 0N/A * <p>This method is designed for use in monitoring system 0N/A * memory usage and detecting low memory condition. 0N/A * <b>MBeanServer access</b>:<br> 0N/A * The mapped type of <tt>MemoryUsage</tt> is 0N/A * <tt>CompositeData</tt> with attributes as specified in 0N/A * {@link MemoryUsage#from MemoryUsage}. 0N/A * @return a {@link MemoryUsage} object; or <tt>null</tt> if 0N/A * this pool not valid. 0N/A * Returns the peak memory usage of this memory pool since the 0N/A * Java virtual machine was started or since the peak was reset. 0N/A * This method returns <tt>null</tt> 0N/A * if this memory pool is not valid (i.e. no longer exists). 0N/A * <b>MBeanServer access</b>:<br> 0N/A * The mapped type of <tt>MemoryUsage</tt> is 0N/A * <tt>CompositeData</tt> with attributes as specified in 0N/A * {@link MemoryUsage#from MemoryUsage}. 0N/A * @return a {@link MemoryUsage} object representing the peak 0N/A * memory usage; or <tt>null</tt> if this pool is not valid. 0N/A * Resets the peak memory usage statistic of this memory pool 0N/A * to the current memory usage. 0N/A * @throws java.lang.SecurityException if a security manager 0N/A * exists and the caller does not have 0N/A * ManagementPermission("control"). 0N/A * Tests if this memory pool is valid in the Java virtual 0N/A * machine. A memory pool becomes invalid once the Java virtual 0N/A * machine removes it from the memory system. 0N/A * @return <tt>true</tt> if the memory pool is valid in the running 0N/A * Java virtual machine; 0N/A * <tt>false</tt> otherwise. 0N/A * Returns the name of memory managers that manages this memory pool. 0N/A * Each memory pool will be managed by at least one memory manager. 0N/A * @return an array of <tt>String</tt> objects, each is the name of 0N/A * a memory manager managing this memory pool. 0N/A * Returns the usage threshold value of this memory pool in bytes. 0N/A * Each memory pool has a platform-dependent default threshold value. 0N/A * The current usage threshold can be changed via the 0N/A * {@link #setUsageThreshold setUsageThreshold} method. 0N/A * @return the usage threshold value of this memory pool in bytes. 0N/A * @throws UnsupportedOperationException if this memory pool 0N/A * does not support a usage threshold. 0N/A * @see #isUsageThresholdSupported 0N/A * Sets the threshold of this memory pool to the given <tt>threshold</tt> 0N/A * value if this memory pool supports the usage threshold. 0N/A * The usage threshold crossing checking is enabled in this memory pool 0N/A * if the threshold is set to a positive value. 0N/A * The usage threshold crossing checking is disabled 0N/A * if it is set to zero. 0N/A * @param threshold the new threshold value in bytes. Must be non-negative. 0N/A * @throws IllegalArgumentException if <tt>threshold</tt> is negative 0N/A * or greater than the maximum amount of memory for 0N/A * this memory pool if defined. 0N/A * @throws UnsupportedOperationException if this memory pool 0N/A * does not support a usage threshold. 0N/A * @throws java.lang.SecurityException if a security manager 0N/A * exists and the caller does not have 0N/A * ManagementPermission("control"). 0N/A * @see #isUsageThresholdSupported 0N/A * @see <a href="#UsageThreshold">Usage threshold</a> 0N/A * Tests if the memory usage of this memory pool 0N/A * reaches or exceeds its usage threshold value. 0N/A * @return <tt>true</tt> if the memory usage of 0N/A * this memory pool reaches or exceeds the threshold value; 0N/A * <tt>false</tt> otherwise. 0N/A * @throws UnsupportedOperationException if this memory pool 0N/A * does not support a usage threshold. 0N/A * Returns the number of times that the memory usage has crossed 0N/A * the usage threshold. 0N/A * @return the number of times that the memory usage 0N/A * has crossed its usage threshold value. 0N/A * @throws UnsupportedOperationException if this memory pool 0N/A * does not support a usage threshold. 0N/A * Tests if this memory pool supports usage threshold. 0N/A * @return <tt>true</tt> if this memory pool supports usage threshold; 0N/A * <tt>false</tt> otherwise. 0N/A * Returns the collection usage threshold value of this memory pool 0N/A * in bytes. The default value is zero. The collection usage 0N/A * threshold can be changed via the 0N/A * {@link #setCollectionUsageThreshold setCollectionUsageThreshold} method. 0N/A * @return the collection usage threshold of this memory pool in bytes. 0N/A * @throws UnsupportedOperationException if this memory pool 0N/A * does not support a collection usage threshold. 0N/A * @see #isCollectionUsageThresholdSupported 0N/A * Sets the collection usage threshold of this memory pool to 0N/A * the given <tt>threshold</tt> value. 0N/A * When this threshold is set to positive, the Java virtual machine 0N/A * will check the memory usage at its best appropriate time after it has 0N/A * expended effort in recycling unused objects in this memory pool. 0N/A * The collection usage threshold crossing checking is enabled 0N/A * in this memory pool if the threshold is set to a positive value. 0N/A * The collection usage threshold crossing checking is disabled 0N/A * if it is set to zero. 0N/A * @param threshold the new collection usage threshold value in bytes. 0N/A * Must be non-negative. 0N/A * @throws IllegalArgumentException if <tt>threshold</tt> is negative 0N/A * or greater than the maximum amount of memory for 0N/A * this memory pool if defined. 0N/A * @throws UnsupportedOperationException if this memory pool 0N/A * does not support a collection usage threshold. 0N/A * @throws java.lang.SecurityException if a security manager 0N/A * exists and the caller does not have 0N/A * ManagementPermission("control"). 0N/A * @see #isCollectionUsageThresholdSupported 0N/A * @see <a href="#CollectionThreshold">Collection usage threshold</a> 0N/A * Tests if the memory usage of this memory pool after 0N/A * the most recent collection on which the Java virtual 0N/A * machine has expended effort has reached or 0N/A * exceeded its collection usage threshold. 0N/A * This method does not request the Java virtual 0N/A * machine to perform any garbage collection other than its normal 0N/A * automatic memory management. 0N/A * @return <tt>true</tt> if the memory usage of this memory pool 0N/A * reaches or exceeds the collection usage threshold value 0N/A * in the most recent collection; 0N/A * <tt>false</tt> otherwise. 0N/A * @throws UnsupportedOperationException if this memory pool 0N/A * does not support a usage threshold. 0N/A * Returns the number of times that the Java virtual machine 0N/A * has detected that the memory usage has reached or 0N/A * exceeded the collection usage threshold. 0N/A * @return the number of times that the memory 0N/A * usage has reached or exceeded the collection usage threshold. 0N/A * @throws UnsupportedOperationException if this memory pool 0N/A * does not support a collection usage threshold. 0N/A * @see #isCollectionUsageThresholdSupported 0N/A * Returns the memory usage after the Java virtual machine 0N/A * most recently expended effort in recycling unused objects 0N/A * in this memory pool. 0N/A * This method does not request the Java virtual 0N/A * machine to perform any garbage collection other than its normal 0N/A * automatic memory management. 0N/A * This method returns <tt>null</tt> if the Java virtual 0N/A * machine does not support this method. 0N/A * <b>MBeanServer access</b>:<br> 0N/A * The mapped type of <tt>MemoryUsage</tt> is 0N/A * <tt>CompositeData</tt> with attributes as specified in 0N/A * {@link MemoryUsage#from MemoryUsage}. 0N/A * @return a {@link MemoryUsage} representing the memory usage of 0N/A * this memory pool after the Java virtual machine most recently 0N/A * expended effort in recycling unused objects; 0N/A * <tt>null</tt> if this method is not supported. 0N/A * Tests if this memory pool supports a collection usage threshold. 0N/A * @return <tt>true</tt> if this memory pool supports the 0N/A * collection usage threshold; <tt>false</tt> otherwise.