public interface MemoryMXBean
MemoryMXBean
is an interface used by the management
system to access memory-related properties.
ObjectName
: com.azul.zing:type=Memory
The MemoryMXBean provides an overview of the memory system and the memory managers that control the size and use patterns of memory. Two categories of monitoring metrics and three methods for collecting data make finding answers to your questions easy:
Monitoring method | Example |
---|---|
Raw metrics | Sizes of memory pools |
Health metrics | Percent of heap occupied; notifications |
The interface has three methods for a monitoring application to collect metrics:
Direct connect | Data is provided directly as a graphable numeric value (long or double) |
Detailed analysis | Data is provided in a returned object containing a snapshot of relevant data |
Threshold and notification | Threshold crossing generates a notification with an object containing a snapshot of relevant data |
The most effective method of monitoring the Java Heap memory use is to monitor the
collection frequency and the live set size. The
getPercentJavaHeapOccupiedAfterCollection()
attribute/method
uses a heuristic approach to estimate the live set size that works well for
both concurrent and stop the world collectors. The value returned estimates
the amount of Java Heap space occupied by live objects.
For environments that use elastic memory, the Java Heap can grow beyond the
size initially reserved. At any point during execution, the
isJavaHeapUsingMoreMemoryThanInitiallyReserved()
provides the answer
to the question whether the process is using much more Java Heap memory
than was originally anticipated.
Thresholds make long term monitoring easy. When the threshold is set
using setPercentJavaHeapOccupiedAfterCollectionThreshold(double)
then
the Zing VM will automatically track the number of times the threshold
is crossed. The count is accessed using
getPercentJavaHeapOccupiedAfterCollectionThresholdExceededCount()
.
To know whether the threshold is exceeded at any moment, use
isPercentJavaHeapOccupiedAfterCollectionThresholdExceeded()
.
The getPercentJavaHeapOccupiedAfterCollectionThreshold()
attribute
returns the current value of the threshold.
For repeated notfications
after the threshold has been crossed, use
setIntervalToWaitBeforeRenotifyWhenThresholdContinuouslyExceeded(long)
to set the time interval in seconds that renotification is desired.
UsegetIntervalToWaitBeforeRenotifyWhenThresholdContinuouslyExceeded()
to determine the current value of the interval in seconds.
One of the primary questions people want answered is how much of the Java
Heap is occupied by the application's objects. Unlike other Java Virtual
Machines, the Zing VM uses the Java Heap for internal data structures, the
Permanent Generation, and (optionally) the code cache. To determine the amount of
memory used by the application, the use of just the New Generation and
the Old generation is the most appropriate metric. The Application Object Heap
memory pool is the combined New Generation and Old Generation memory pools. To
determine its use, access the simple health metric to obtain the percent in use:
getApplicationObjectHeapMemoryPercentInUse()
For those Java Virtual Machines that use elastic memory, the percent in use can exceed 100 percent.
The raw metrics that are used to do the calculation of the percent are also available as direct connect metrics:
In use memory | getApplicationObjectHeapUse() |
Usable memory | getApplicationObjectHeapUsableMemory() |
The direct connect methods return the memory use as a type long
.
Detailed analysis returns a
MemoryUsage
object with the initial reserved, size, use, and
memory pool size type, FIXED
or ELASTIC
.
Java Heap | Native Heap/th> | |
---|---|---|
Direct connect | getJavaHeapUse() |
getNonJavaHeapUse() |
Detailed analysis | getJavaHeapMemoryUsage() |
getNonJavaHeapMemoryUsage() |
The methods that have a "MemoryUsage" suffix return a MemoryUsage
object.
The number of objects pending finalization, particularly when
those objects free system resources that can be reused,
can be an indicator of the health of the Java process. Access
the number of objects pending finalization using
getObjectsPendingFinalizationCount()
. While knowing
how many objects are awaiting finalization is an important
metric, the number of objects with finalizers allocated provides
information on the rate of creation and finalization. A
count is kept internally of the number of
objects with finalizers allocated. The value of the cumulative counter
can be accessed and reset using:
getObjectsWithFinalizerCumulativeAllocationCount()
resetObjectsWithFinalizerCumulativeAllocationCount()
getTimeOfLastResetObjectsWithFinalizerCumulativeAllocationCount()
Generate a full garbage collection, a New Generation collection followed by
an Old Generation collection by using:
fullGC()
newGenerationGC()
- currently not implemented
Turn on and off Garbage Collector logging using:
isVerbose()
setVerbose(boolean)
isDetailedVerbose()
setDetailedVerbose(boolean)
The types of notifications are also described in MemoryNotificationInfo
.
Percent Java Heap Occupied after Collection Threshold
A threshold notification set using setPercentJavaHeapOccupiedAfterCollectionThreshold(double)
by specifying the maximum percentage of the heap that should be occupied after
garbage collection. Please see setPercentJavaHeapOccupiedAfterCollectionThreshold(double)
for a complete description of how this value is calculated for a concurrent collector.
By default, the value of this threshold is zero. If the value is zero then
no notifications will be triggered.
Java Heap using More Memory than Initially Reserved
An event notification that is triggered when the Java Heap use is greater than
the amount of memory initially reserved for the Java Heap. Generally, this will
only work when the Java Heap memory pool type is MemoryPoolSizeType.ELASTIC
.
Notifications are easy to set up using the MemoryMXBean. Here is an
example that sets up a listener for notifications for the two types of
notifications supported, an event notification and a threshold exceeded
notification.
Extracting information from the javax.management.Notification
object
that is delivered with the memory notification is also in the example code.
Build the client program using:
$JAVA_HOME/javac -cp $JAVA_HOME/etc/extensions/mxbeans/agents/ZingJMM.jar ClientMemoryNotification.java
import javax.management.JMX; import javax.management.MBeanServerConnection; import javax.management.Notification; import javax.management.NotificationListener; import javax.management.ObjectName; import javax.management.openmbean.CompositeData; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; import com.azul.zing.management.MemoryMXBean; import com.azul.zing.management.MemoryNotificationInfo; import com.azul.zing.management.MemoryPoolSizeType; import com.azul.zing.management.MemoryUsage; public class ClientMemoryNotification { // Class to handle notifications public static class ClientListener implements NotificationListener { @Override public void handleNotification(Notification notification, Object handback) { if (notification.getType().equals(MemoryNotificationInfo.PERCENT_JAVA_HEAP_OCCUPIED_AFTER_COLLECTION_THRESHOLD_EXCEEDED) || notification.getType().equals(MemoryNotificationInfo.JAVA_HEAP_USING_MORE_MEMORY_THAN_INITIALLY_RESERVED)) { // Get the information associated with this notification long sequenceNumber = notification.getSequenceNumber(); long timeStamp = notification.getTimeStamp(); String type = notification.getType(); // Access NotificationInfo attributes MemoryNotificationInfo info = MemoryNotificationInfo.from((CompositeData) notification.getUserData()); String poolName = info.getPoolName(); String notificationReason = info.getNotificationReason(); long count = info.getCount(); // Access the MemoryUsage details MemoryUsage memoryUsage = info.getMemoryUsage(); double elapsedTime = memoryUsage.getElapsedTimeSinceJVMStartSec(); MemoryPoolSizeType mpst = memoryUsage.getMemoryPoolSizeType(); long initialReserved = memoryUsage.getInitialReserved(); long size = memoryUsage.getSize(); long used = memoryUsage.getUsed(); System.out.println("sequenceNumber: " + sequenceNumber + " timeStamp: " + timeStamp + " type: " + type + " poolName: " + poolName + " notificationReason: " + notificationReason + " count: " + count + " elapsedTime: " + elapsedTime + " memoryPoolSizeType: " + mpst + " initialReserved: " + initialReserved + " size: " + size + " used: " + used); // Your code ... } } } public static void main(String[] args) throws Exception { String ipAddr = args[0]; // IP address of the machine where the server is running String port = args[1]; // Port specified when starting the server using: // -Dcom.sun.management.jmxremote \ // -Dcom.sun.management.jmxremote.port=${listeningOnPort} \ // -Dcom.sun.management.jmxremote.authenticate=false \ // -Dcom.sun.management.jmxremote.ssl=false \ // Create an RMI connector client and connect to the RMI connector server // Protocol: // service:jmx:protocol:sap // //[host[:port]][url-path] // new JMXServiceURL("service:jmx:rmi: ///jndi/rmi://:9999/jmxrmi"); JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://"+ ipAddr + ":" + port + "/jmxrmi"); JMXConnector jmxc = JMXConnectorFactory.connect(url, null); // Greate an MBeanServerConnection MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); // Set up to montitor the notifications generated by the Memory MXBean // Construct the ObjectName for the Memory MXBean ObjectName mbeanNameMemory = new ObjectName("com.azul.zing:type=Memory"); // Create a proxy for the MXBean instead of going through the MBean server connection MemoryMXBean memoryMXBeanProxy = JMX.newMXBeanProxy(mbsc, mbeanNameMemory, MemoryMXBean.class, true); // Set a threshold value to generate a notification when the maximum Java // Heap occupied after collection exceeds that threshold if (memoryMXBeanProxy.isPercentJavaHeapOccupiedAfterCollectionThresholdSupported()) { memoryMXBeanProxy.setPercentJavaHeapOccupiedAfterCollectionThreshold(60); } // Create the notification listener ClientListener memoryListener = new ClientListener(); // Add notification listener for the MXBean mbsc.addNotificationListener(mbeanNameMemory, memoryListener, null, null); nap(20000000); } private static void nap(int millis) { try { Thread.sleep(millis); } catch (InterruptedException e) { e.printStackTrace(); } } }
The process’s Java Heap use (Process’s Reserved Zing Memory)
getJavaHeapUse()
getJavaHeapMemoryUsage()
returns MemoryUsage
object
The process’s non-Java Heap use (Process’s Native Heap)
getNonJavaHeapUse()
getNonJavaHeapMemoryUsage()
returns MemoryUsage
object
Monitoring only New Generation + Old Generation heap use
getApplicationObjectHeapUse()
- current use New+Old
getApplicationObjectHeapUsableMemory()
- total memory available for New+Old
getApplicationObjectHeapMemoryPercentInUse()
- percent New+Old in use
Current Java Heap using more than initially reserved (-Xmx)
isJavaHeapUsingMoreMemoryThanInitiallyReserved()
Estimate percent of the Java heap occupied by live objects
getPercentJavaHeapOccupiedAfterCollection()
Thresholds and notifications
Threshold for estimated percent of the Java heap occupied by live objects
setPercentJavaHeapOccupiedAfterCollectionThreshold(double)
getPercentJavaHeapOccupiedAfterCollectionThreshold()
isPercentJavaHeapOccupiedAfterCollectionThresholdExceeded()
getPercentJavaHeapOccupiedAfterCollectionThresholdExceededCount()
isPercentJavaHeapOccupiedAfterCollectionThresholdSupported()
Interval to wait before sending another notification when a threshold is constantly exceeded
setIntervalToWaitBeforeRenotifyWhenThresholdContinuouslyExceeded(long)
getIntervalToWaitBeforeRenotifyWhenThresholdContinuouslyExceeded()
Verbose GC output on/off
setVerbose(boolean)
(-XX:+PrintGC/-verbose:gc)
isVerbose()
setDetailedVerbose(boolean)
(-XX:+PrintGCDetails)
isDetailedVerbose()
Force a Garbage Collection
newGenerationGC()
fullGC()
The Finalizer queue and Finalizable objects allocated
getObjectsPendingFinalizationCount()
getObjectsWithFinalizerCumulativeAllocationCount()
resetObjectsWithFinalizerCumulativeAllocationCount()
getTimeOfLastResetObjectsWithFinalizerCumulativeAllocationCount()
Modifier and Type | Method and Description |
---|---|
void |
fullGC()
Invokes the JVM’s garbage collector; equivalent to
System.gc() . |
double |
getApplicationObjectHeapMemoryPercentInUse()
Process’s percent of the application object memory area of the Java heap in use.
|
long |
getApplicationObjectHeapUsableMemory()
Process’s amount of memory available for use by the application object area of the
Java heap.
|
long |
getApplicationObjectHeapUse()
Process’s current memory use of the application object area of the
Java heap.
|
long |
getIntervalToWaitBeforeRenotifyWhenThresholdContinuouslyExceeded()
The minimum interval to wait before sending a renotification when the threshold is
continuously exceeded.
|
MemoryUsage |
getJavaHeapMemoryUsage()
Process’s current memory usage of the Java heap.
|
long |
getJavaHeapUse()
Process’s current memory use of the Java heap.
|
MemoryUsage |
getNonJavaHeapMemoryUsage()
Process’s current memory usage for all of the process’s memory in
use outside of the Java heap.
|
long |
getNonJavaHeapUse()
The process’s current memory use of the non-Java heap.
|
int |
getObjectsPendingFinalizationCount()
Approximate number of objects that are on the Finalizer queue
waiting to be finalized.
|
long |
getObjectsWithFinalizerCumulativeAllocationCount()
Approximate number of allocated objects that have a
Finalize method. |
double |
getPercentJavaHeapOccupiedAfterCollection()
Estimate of the percent of the entire Java heap pool occupied after
garbage collection.
|
double |
getPercentJavaHeapOccupiedAfterCollectionThreshold()
The percent Java heap occupied after collection threshold;
the default is zero.
|
long |
getPercentJavaHeapOccupiedAfterCollectionThresholdExceededCount()
The number of times the percent Java heap occupied after collection
threshold has been reached or exceeded.
|
double |
getTimeOfLastResetObjectsWithFinalizerCumulativeAllocationCount()
Time since the start of the JVM when the last reset was done of
the counter holding the value of the cumulative count of the number of
allocated objects that have a
Finalize method. |
java.lang.String |
getVersion()
Version of the MemoryMXBean application programming interface.
|
boolean |
isDetailedVerbose()
Indicates whether detailed verbose output (+PrintGCDetails) is
enabled for the memory system.
|
boolean |
isJavaHeapUsingMoreMemoryThanInitiallyReserved()
Answers the question whether the process is using an amount of memory
for the Java Heap that is greater than the amount of memory initially
reserved for the Java Heap.
|
boolean |
isPercentJavaHeapOccupiedAfterCollectionThresholdExceeded()
Is the value of the percent Java heap occupied after collection metric
currently exceeding the threshold.
|
boolean |
isPercentJavaHeapOccupiedAfterCollectionThresholdSupported()
Indicates whether the percent Java heap occupied after collection threshold
is supported.
|
boolean |
isVerbose()
Indicates whether verbose output (+PrintGC) is enabled for
the memory system.
|
void |
newGenerationGC()
Invokes the garbage collector to collect the garbage in the New Generation.
|
double |
resetObjectsWithFinalizerCumulativeAllocationCount()
Reset the counter holding the value of the cumulative count of the number of
allocated objects that have a
Finalize method. |
boolean |
setDetailedVerbose(boolean value)
Enables or disables detailed verbose output (PrintGCDetails) for the
memory system.
|
boolean |
setIntervalToWaitBeforeRenotifyWhenThresholdContinuouslyExceeded(long secondsToWaitBeforeRenotify)
Sets the minimum interval to wait before a renotification when the threshold
is continuously exceeded.
|
boolean |
setPercentJavaHeapOccupiedAfterCollectionThreshold(double threshold)
Set The percent Java heap occupied after collection threshold (in bytes);
the default is zero.
|
boolean |
setVerbose(boolean value)
Enables or disables verbose output (PrintGC) for the memory system.
|
void fullGC()
Invokes the JVM’s garbage collector; equivalent to System.gc()
.
java.lang.UnsupportedOperationException
- if this is not supported.double getApplicationObjectHeapMemoryPercentInUse()
Process’s percent of the application object memory area of the Java heap in use.
The value representing the percentage of memory currently in use for allocating application objects in the New (also called Young) Generation and Old Generation. The value is calculated using the formula:long
value representing the percentage of
the application object area of the Java heap currently in use.long getApplicationObjectHeapUsableMemory()
Process’s amount of memory available for use by the application object area of the Java heap.
The value representing the amount of memory available for use for allocating application objects in the New (also called Young) Generation and Old Generation. The value includes both the memory occupied by objects and the memory available for new object allocation.long
value representing the number of bytes available for use in
the application object area of the Java heap.long getApplicationObjectHeapUse()
Process’s current memory use of the application object area of the Java heap.
The value representing the current memory use of the New (also called Young) Generation together with the current memory use of the Old Generation.long
value representing the number of bytes or memory currently in use in
the application object area of the Java heap.long getIntervalToWaitBeforeRenotifyWhenThresholdContinuouslyExceeded()
The minimum interval to wait before sending a renotification when the threshold is continuously exceeded. The first notification is sent when the conditions are checked and the threshold has been crossed. If, as time continues, the threshold remains continuously exceeded, rather than flooding the system with notifications, no additional notifications are sent. This value represents the minimum number of seconds to wait before renotifying when that threshold is continuously exceeded. If the returned value is zero, no renotifications are generated.
java.lang.UnsupportedOperationException
- if this is not supported.setIntervalToWaitBeforeRenotifyWhenThresholdContinuouslyExceeded()
for an explanation of the time interval to wait before renotification
when the threshold is continuously exceeded.
MemoryUsage getJavaHeapMemoryUsage()
Process’s current memory usage of the Java heap. The Java heap memory may contain many pools including some used and managed exclusively by the JVM as well as the New and Old Generations used for application object allocation.
MemoryUsage
instance representing Java heap memory.long getJavaHeapUse()
Process’s current memory use of the Java heap. The Java heap memory may contain many pools including some used and managed exclusively by the JVM as well as the New and Old Generations used for application object allocation.
long
value representing the number of bytes of Java heap
memory in use.MemoryUsage getNonJavaHeapMemoryUsage()
Process’s current memory usage for all of the process’s memory in use outside of the Java heap.
MemoryUsage
instance representing non-Java heap memory.long getNonJavaHeapUse()
The process’s current memory use of the non-Java heap.
long
value representing the number of bytes of non-Java
heap memory in use.int getObjectsPendingFinalizationCount()
Approximate number of objects that are on the Finalizer queue waiting to be finalized.
long getObjectsWithFinalizerCumulativeAllocationCount()
Approximate number of allocated objects that have a
Finalize
method.
Finalize
method.double getPercentJavaHeapOccupiedAfterCollection()
Estimate of the percent of the entire Java heap pool occupied after
garbage collection.
The value is an estimate of the total size of the Java heap
at the end of a garbage collection of the entire Java heap. The calculation adds
the size of each of the MemoryPoolSizeType.FIXED
pools in the Java heap
together with the size of each of the MemoryPoolSizeType.ELASTIC
pools, the latter
at the end of each pool’s most recent collection. The result is an estimate
of the live set size of the garbage collected pools added to the size of each
of the MemoryPoolSizeType.FIXED
pools in the Java heap.
The percent value returned is based on a ratio calculated using:
1. A numerator that is the minimum of:
The total Java heap size at the end of the last New Generation garbage collection
The total Java heap size at the end of the last Old Generation garbage collection
2. A denominator that is the committed size of the Java heap at the end of the same garbage collection.
double
value representing percentage of Java
heap memory occupied after garbage collection.double getPercentJavaHeapOccupiedAfterCollectionThreshold()
The percent Java heap occupied after collection threshold; the default is zero.
java.lang.UnsupportedOperationException
- if this is not supported.isPercentJavaHeapOccupiedAfterCollectionThresholdSupported()
long getPercentJavaHeapOccupiedAfterCollectionThresholdExceededCount()
The number of times the percent Java heap occupied after collection threshold has been reached or exceeded.
java.lang.UnsupportedOperationException
- if this is not supported.isPercentJavaHeapOccupiedAfterCollectionThresholdSupported()
double getTimeOfLastResetObjectsWithFinalizerCumulativeAllocationCount()
Time since the start of the JVM when the last reset was done of
the counter holding the value of the cumulative count of the number of
allocated objects that have a Finalize
method.
Finalize
method was done. Units
are seconds.java.lang.String getVersion()
Version of the MemoryMXBean application programming interface. The format for the version is majorVersion.minorVersion.microVersion.
boolean isDetailedVerbose()
Indicates whether detailed verbose output (+PrintGCDetails) is enabled for the memory system.
true
when detailed verbose output is enabled,
otherwise false
.boolean isJavaHeapUsingMoreMemoryThanInitiallyReserved()
Answers the question whether the process is using an amount of memory for the Java Heap that is greater than the amount of memory initially reserved for the Java Heap.
true
if Java heap memory use exceeds the amount
initially reserved;
false
if Java heap memory use is below the amount
initially reserved.boolean isPercentJavaHeapOccupiedAfterCollectionThresholdExceeded()
Is the value of the percent Java heap occupied after collection metric currently exceeding the threshold.
true
if the percent Java heap occupied after
collection threshold is currently reached or exceeded;
otherwise, false
java.lang.UnsupportedOperationException
- if this is not supported.isPercentJavaHeapOccupiedAfterCollectionThresholdSupported()
boolean isPercentJavaHeapOccupiedAfterCollectionThresholdSupported()
Indicates whether the percent Java heap occupied after collection threshold is supported.
true
if the percent Java heap occupied after collection
threshold is supported, otherwise false
.boolean isVerbose()
Indicates whether verbose output (+PrintGC) is enabled for the memory system.
true
when verbose output is enabled, otherwise
false
.void newGenerationGC()
Invokes the garbage collector to collect the garbage in the New Generation.
java.lang.UnsupportedOperationException
- if this is not supported.double resetObjectsWithFinalizerCumulativeAllocationCount()
Reset the counter holding the value of the cumulative count of the number of
allocated objects that have a Finalize
method.
Finalize
method occurred. Units
are seconds.java.lang.SecurityException
- if caller does not have
ManagementPermission("control")
.boolean setDetailedVerbose(boolean value)
Enables or disables detailed verbose output (PrintGCDetails) for the memory system.
value
- true
to enable; false
to
disable.true
when successful execution,
otherwise false
.java.lang.SecurityException
- if caller doesn’t have
ManagementPermission("control")
.boolean setIntervalToWaitBeforeRenotifyWhenThresholdContinuouslyExceeded(long secondsToWaitBeforeRenotify)
Sets the minimum interval to wait before a renotification when the threshold
is continuously exceeded. The first notification is sent when the conditions
are checked and the threshold has been crossed. If, as time continues, the threshold
remains continuously exceeded, rather than flooding the system with notifications, no
additional notifications are sent.
However, the
setIntervalToWaitBeforeRenotifyWhenThresholdContinuouslyExceeded
can
be set to a time interval that, measured from the time of the previous notification, would cause
a renotification to be sent when the threshold is continuously exceeded. If the
renotification is sent then the timer starts again at that time to determine
when the next time interval will be exceeded. Use of this feature does not guarantee that
renotifications will occur at the interval specified because the check for the
resource’s exceeding of the threshold may only occur at an interval longer than
the value of the parameter supplied.
The default value is 0 and, when set to that value, means that no renotifications
will be generated.
secondsToWaitBeforeRenotify
- The minimum interval to wait in seconds before a renotification.true
when successful execution,
otherwise false
.java.lang.IllegalArgumentException
- if the new secondsToWaitBeforeRenotify
is either negative or greater than the maximum allowed.java.lang.UnsupportedOperationException
- if this is not supported.java.lang.SecurityException
- if caller does not have
ManagementPermission("control")
.boolean setPercentJavaHeapOccupiedAfterCollectionThreshold(double threshold)
Set The percent Java heap occupied after collection threshold (in bytes); the default is zero. If the value is zero then it will not be possible to subscribe to notifications based on crossing this threshold.
threshold
- is a percent of the Java heap occupied after collection
threshold value. An example of a valid value: 50true
when successful execution,
otherwise false
.java.lang.UnsupportedOperationException
- if this is not supported.java.lang.IllegalArgumentException
- if the new threshold
is either negative or greater than the maximum allowed.java.lang.SecurityException
- if caller does not have
ManagementPermission("control")
.isPercentJavaHeapOccupiedAfterCollectionThresholdSupported()
boolean setVerbose(boolean value)
Enables or disables verbose output (PrintGC) for the memory system.
value
- true
to enable; false
to
disable.true
when successful execution,
otherwise false
.java.lang.SecurityException
- if caller doesn’t have
ManagementPermission("control")
. Submit a bug or feature request
Copyright © 2015-2017, Azul Systems, Inc. All rights reserved.
Azul Systems and Zing are registered trademarks of Azul Systems, Inc.