| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Iterator iter = …;
MemoryPoolMXBean item = (MemoryPoolMXBean)iter.next();
Based on 21 examples
public interface MemoryPoolMXBean
The management interface for a memory pool. A memory pool represents the memory resource managed by the Java virtual machine and is managed by one or more {@link MemoryManagerMXBean memory managers}.
A Java virtual machine has one or more instances of the implementation class of this interface. An instance implementing this interface is an MXBean that can be obtained by calling the {@link ManagementFactory#getMemoryPoolMXBeans} method or from the {@link ManagementFactory#getPlatformMBeanServer platform MBeanServer} method.
The ObjectName for uniquely identifying the MXBean for a memory pool within an MBeanServer is:
{@link ManagementFactory#MEMORY_POOL_MXBEAN_DOMAIN_TYPE java.lang:type=MemoryPool},name=pool's name
The Java virtual machine has a heap for object allocation and also maintains non-heap memory for the method area and the Java virtual machine execution. The Java virtual machine can have one or more memory pools. Each memory pool represents a memory area of one of the following types:
In general, this method is a lightweight operation for getting an approximate memory usage. For some memory pools, for example, when objects are not packed contiguously, this method may be an expensive operation that requires some computation to determine the current memory usage. An implementation should document when this is the case.
A Java virtual machine performs usage threshold crossing checking on a memory pool basis at its best appropriate time, typically, at garbage collection time. Each memory pool maintains a {@link #getUsageThresholdCount usage threshold count} that will get incremented every time when the Java virtual machine detects that the memory pool usage is crossing the threshold.
This manageable usage threshold attribute is designed for monitoring the increasing trend of memory usage with low overhead. Usage threshold may not be appropriate for some memory pools. For example, a generational garbage collector, a common garbage collection algorithm used in many Java virtual machine implementations, manages two or more generations segregating objects by age. Most of the objects are allocated in the youngest generation (say a nursery memory pool). The nursery memory pool is designed to be filled up and collecting the nursery memory pool will free most of its memory space since it is expected to contain mostly short-lived objects and mostly are unreachable at garbage collection time. In this case, it is more appropriate for the nursery memory pool not to support a usage threshold. In addition, if the cost of an object allocation in one memory pool is very low (for example, just atomic pointer exchange), the Java virtual machine would probably not support the usage threshold for that memory pool since the overhead in comparing the usage with the threshold is higher than the cost of object allocation.
The memory usage of the system can be monitored using polling or threshold notification mechanisms.
An application can continuously monitor its memory usage by calling either the {@link #getUsage} method for all memory pools or the {@link #isUsageThresholdExceeded} method for those memory pools that support a usage threshold. Below is example code that has a thread delicated for task distribution and processing. At every interval, it will determine if it should receive and process new tasks based on its memory usage. If the memory usage exceeds its usage threshold, it will redistribute all outstanding tasks to other VMs and stop receiving new tasks until the memory usage returns below its usage threshold.
// Assume the usage threshold is supported for this pool. // Set the threshold to myThreshold above which no new tasks // should be taken. pool.setUsageThreshold(myThreshold); .... boolean lowMemory = false; while (true) { if (pool.isUsageThresholdExceeded()) { // potential low memory, so redistribute tasks to other VMs lowMemory = true; redistributeTasks(); // stop receiving new tasks stopReceivingTasks(); } else { if (lowMemory) { // resume receiving tasks lowMemory = false; resumeReceivingTasks(); } // processing outstanding task ... } // sleep for sometime try { Thread.sleep(sometime); } catch (InterruptedException e) { ... } }
Below shows another example that takes some action if a memory pool is under low memory and ignores the memory usage changes during the action processing time.
// Assume the usage threshold is supported for this pool. // Set the threshold to myThreshold which determines if // the application will take some action under low memory condition. pool.setUsageThreshold(myThreshold); int prevCrossingCount = 0; while (true) { // A busy loop to detect when the memory usage // has exceeded the threshold. while (!pool.isUsageThresholdExceeded() || pool.getUsageThresholdCount() == prevCrossingCount) { try { Thread.sleep(sometime) } catch (InterruptException e) { .... } } // Do some processing such as check for memory usage // and issue a warning .... // Gets the current threshold count. The busy loop will then // ignore any crossing of threshold happens during the processing. prevCrossingCount = pool.getUsageThresholdCount(); }
Usage threshold notification will be emitted by {@link MemoryMXBean}. When the Java virtual machine detects that the memory usage of a memory pool has reached or exceeded the usage threshold the virtual machine will trigger the MemoryMXBean to emit an {@link MemoryNotificationInfo#MEMORY_THRESHOLD_EXCEEDED usage threshold exceeded notification}. Another usage threshold exceeded notification will not be generated until the usage has fallen below the threshold and then exceeded it again.
Below is an example code implementing the same logic as the first example above but using the usage threshold notification mechanism to detect low memory conditions instead of polling. In this example code, upon receiving notification, the notification listener notifies another thread to perform the actual action such as to redistribute outstanding tasks, stop receiving tasks, or resume receiving tasks. The handleNotification method should be designed to do a very minimal amount of work and return without delay to avoid causing delay in delivering subsequent notifications. Time-consuming actions should be performed by a separate thread. The notification listener may be invoked by multiple threads concurrently; so the tasks performed by the listener should be properly synchronized.
class MyListener implements javax.management.NotificationListener { public void handleNotification(Notification notification, Object handback) { String notifType = notification.getType(); if (notifType.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) { // potential low memory, notify another thread // to redistribute outstanding tasks to other VMs // and stop receiving new tasks. lowMemory = true; notifyAnotherThread(lowMemory); } } } // Register MyListener with MemoryMXBean MemoryMXBean mbean = ManagementFactory.getMemoryMXBean(); NotificationEmitter emitter = (NotificationEmitter) mbean; MyListener listener = new MyListener(); emitter.addNotificationListener(listener, null, null); // Assume this pool supports a usage threshold. // Set the threshold to myThreshold above which no new tasks // should be taken. pool.setUsageThreshold(myThreshold); // Usage threshold detection is enabled and notification will be // handled by MyListener. Continue for other processing. ....
There is no guarantee about when the MemoryMXBean will emit a threshold notification and when the notification will be delivered. When a notification listener is invoked, the memory usage of the memory pool may have crossed the usage threshold more than once. The {@link MemoryNotificationInfo#getCount} method returns the number of times that the memory usage has crossed the usage threshold at the point in time when the notification was constructed. It can be compared with the current usage threshold count returned by the {@link #getUsageThresholdCount} method to determine if such situation has occurred.
The {@link MemoryPoolMXBean#isCollectionUsageThresholdSupported} method can be used to determine if this functionality is supported.
A Java virtual machine performs collection usage threshold checking on a memory pool basis. This checking is enabled if the collection usage threshold is set to a positive value. If the collection usage threshold is set to zero, this checking is disabled on this memory pool. Default value is zero. The Java virtual machine performs the collection usage threshold checking at garbage collection time.
Some garbage-collected memory pools may choose not to support the collection usage threshold. For example, a memory pool is only managed by a continuous concurrent garbage collector. Objects can be allocated in this memory pool by some thread while the unused objects are reclaimed by the concurrent garbage collector simultaneously. Unless there is a well-defined garbage collection time which is the best appropriate time to check the memory usage, the collection usage threshold should not be supported.
The collection usage threshold is designed for monitoring the memory usage after the Java virtual machine has expended effort in reclaiming memory space. The collection usage could also be monitored by the polling and threshold notification mechanism described above for the usage threshold in a similar fashion.
Method Summary | |
---|---|
MemoryUsage |
Returns the memory usage after the Java virtual machine most recently expended effort in recycling unused objects in this memory pool. |
long |
Returns the collection usage threshold value of this memory pool in bytes. |
long |
Returns the number of times that the Java virtual machine has detected that the memory usage has reached or exceeded the collection usage threshold. |
String[] |
Returns the name of memory managers that manages this memory pool. |
String |
getName() Returns the name representing this memory pool. |
MemoryUsage |
Returns the peak memory usage of this memory pool since the Java virtual machine was started or since the peak was reset. |
MemoryType |
getType() Returns the type of this memory pool. |
MemoryUsage |
getUsage() Returns an estimate of the memory usage of this memory pool. |
long |
Returns the usage threshold value of this memory pool in bytes. |
long |
Returns the number of times that the memory usage has crossed the usage threshold. |
boolean |
Tests if the memory usage of this memory pool after the most recent collection on which the Java virtual machine has expended effort has reached or exceeded its collection usage threshold. |
boolean |
Tests if this memory pool supports a collection usage threshold. |
boolean |
Tests if the memory usage of this memory pool reaches or exceeds its usage threshold value. |
boolean |
Tests if this memory pool supports usage threshold. |
boolean |
isValid() Tests if this memory pool is valid in the Java virtual machine. |
void |
Resets the peak memory usage statistic of this memory pool to the current memory usage. |
void |
setCollectionUsageThreshold(long threhsold) Sets the collection usage threshold of this memory pool to the given threshold value. |
void |
setUsageThreshold(long threshold) Sets the threshold of this memory pool to the given threshold value if this memory pool supports the usage threshold. |
Method Detail |
---|
public MemoryUsage getCollectionUsage()
MBeanServer access:
The mapped type of MemoryUsage is
CompositeData with attributes as specified in
{@link MemoryUsage#from MemoryUsage}.
public long getCollectionUsageThreshold()
public long getCollectionUsageThresholdCount()
public String[] getMemoryManagerNames()
public String getName()
public MemoryUsage getPeakUsage()
MBeanServer access:
The mapped type of MemoryUsage is
CompositeData with attributes as specified in
{@link MemoryUsage#from MemoryUsage}.
public MemoryType getType()
MBeanServer access:
The mapped type of MemoryType is String
and the value is the name of the MemoryType.
public MemoryUsage getUsage()
This method requests the Java virtual machine to make a best-effort estimate of the current memory usage of this memory pool. For some memory pools, this method may be an expensive operation that requires some computation to determine the estimate. An implementation should document when this is the case.
This method is designed for use in monitoring system memory usage and detecting low memory condition.
MBeanServer access:
The mapped type of MemoryUsage is
CompositeData with attributes as specified in
{@link MemoryUsage#from MemoryUsage}.
public long getUsageThreshold()
public long getUsageThresholdCount()
public boolean isCollectionUsageThresholdExceeded()
public boolean isCollectionUsageThresholdSupported()
public boolean isUsageThresholdExceeded()
public boolean isUsageThresholdSupported()
public boolean isValid()
public void resetPeakUsage()
public void setCollectionUsageThreshold(long threhsold)
The collection usage threshold crossing checking is enabled in this memory pool if the threshold is set to a positive value. The collection usage threshold crossing checking is disabled if it is set to zero.
threhsold
- the new collection usage threshold value in bytes.
Must be non-negative.public void setUsageThreshold(long threshold)
threshold
- the new threshold value in bytes. Must be non-negative.
| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |