java.lang.management
Interface MemoryMXBean


public interface MemoryMXBean

The management interface for the memory system of the Java virtual machine.

A Java virtual machine has a single instance of the implementation class of this interface. This instance implementing this interface is an MXBean that can be obtained by calling the ManagementFactory.getMemoryMXBean() sample code for java.lang.management.ManagementFactory.getMemoryMXBean() definition code for java.lang.management.ManagementFactory.getMemoryMXBean() method or from the platform MBeanServer sample code for java.lang.management.ManagementFactory.getPlatformMBeanServer() definition code for java.lang.management.ManagementFactory.getPlatformMBeanServer() method.

The ObjectName for uniquely identifying the MXBean for the memory system within an MBeanServer is:

java.lang:type=Memory sample code for java.lang.management.ManagementFactory.MEMORY_MXBEAN_NAME definition code for java.lang.management.ManagementFactory.MEMORY_MXBEAN_NAME

Memory

The memory system of the Java virtual machine manages the following kinds of memory:

1. Heap

The Java virtual machine has a heap that is the runtime data area from which memory for all class instances and arrays are allocated. It is created at the Java virtual machine start-up. Heap memory for objects is reclaimed by an automatic memory management system which is known as a garbage collector.

The heap may be of a fixed size or may be expanded and shrunk. The memory for the heap does not need to be contiguous.

2. Non-Heap Memory

The Java virtual machine manages memory other than the heap (referred as non-heap memory).

The Java virtual machine has a method area that is shared among all threads. The method area belongs to non-heap memory. It stores per-class structures such as a runtime constant pool, field and method data, and the code for methods and constructors. It is created at the Java virtual machine start-up.

The method area is logically part of the heap but a Java virtual machine implementation may choose not to either garbage collect or compact it. Similar to the heap, the method area may be of a fixed size or may be expanded and shrunk. The memory for the method area does not need to be contiguous.

In addition to the method area, a Java virtual machine implementation may require memory for internal processing or optimization which also belongs to non-heap memory. For example, the JIT compiler requires memory for storing the native machine code translated from the Java virtual machine code for high performance.

Memory Pools and Memory Managers

Memory pools sample code for java.lang.management.MemoryPoolMXBean definition code for java.lang.management.MemoryPoolMXBean and memory managers sample code for java.lang.management.MemoryManagerMXBean definition code for java.lang.management.MemoryManagerMXBean are the abstract entities that monitor and manage the memory system of the Java virtual machine.

A memory pool represents a memory area that the Java virtual machine manages. The Java virtual machine has at least one memory pool and it may create or remove memory pools during execution. A memory pool can belong to either the heap or the non-heap memory.

A memory manager is responsible for managing one or more memory pools. The garbage collector is one type of memory manager responsible for reclaiming memory occupied by unreachable objects. A Java virtual machine may have one or more memory managers. It may add or remove memory managers during execution. A memory pool can be managed by more than one memory manager.

Memory Usage Monitoring

Memory usage is a very important monitoring attribute for the memory system. The memory usage, for example, could indicate:

The memory usage can be monitored in three ways:

Details are specified in the MemoryPoolMXBean sample code for java.lang.management.MemoryPoolMXBean definition code for java.lang.management.MemoryPoolMXBean interface.

The memory usage monitoring mechanism is intended for load-balancing or workload distribution use. For example, an application would stop receiving any new workload when its memory usage exceeds a certain threshold. It is not intended for an application to detect and recover from a low memory condition.

Notifications

This MemoryMXBean is a NotificationEmitter sample code for javax.management.NotificationEmitter definition code for javax.management.NotificationEmitter that emits two types of memory notifications sample code for javax.management.Notification definition code for javax.management.Notification if any one of the memory pools supports a usage threshold or a collection usage threshold which can be determined by calling the MemoryPoolMXBean.isUsageThresholdSupported() sample code for java.lang.management.MemoryPoolMXBean.isUsageThresholdSupported() definition code for java.lang.management.MemoryPoolMXBean.isUsageThresholdSupported() and MemoryPoolMXBean.isCollectionUsageThresholdSupported() sample code for java.lang.management.MemoryPoolMXBean.isCollectionUsageThresholdSupported() definition code for java.lang.management.MemoryPoolMXBean.isCollectionUsageThresholdSupported() methods.

The notification emitted is a Notification sample code for javax.management.Notification definition code for javax.management.Notification instance whose user data sample code for javax.management.Notification.setUserData(java.lang.Object) definition code for javax.management.Notification.setUserData(java.lang.Object) is set to a CompositeData sample code for javax.management.openmbean.CompositeData definition code for javax.management.openmbean.CompositeData that represents a MemoryNotificationInfo sample code for java.lang.management.MemoryNotificationInfo definition code for java.lang.management.MemoryNotificationInfo object containing information about the memory pool when the notification was constructed. The CompositeData contains the attributes as described in MemoryNotificationInfo sample code for java.lang.management.MemoryNotificationInfo.from(javax.management.openmbean.CompositeData) definition code for java.lang.management.MemoryNotificationInfo.from(javax.management.openmbean.CompositeData) .


NotificationEmitter

The MemoryMXBean object returned by ManagementFactory.getMemoryMXBean() sample code for java.lang.management.ManagementFactory.getMemoryMXBean() definition code for java.lang.management.ManagementFactory.getMemoryMXBean() implements the NotificationEmitter sample code for javax.management.NotificationEmitter definition code for javax.management.NotificationEmitter interface that allows a listener to be registered within the MemoryMXBean as a notification listener. Below is an example code that registers a MyListener to handle notification emitted by the MemoryMXBean.
 class MyListener implements javax.management.NotificationListener {
     public void handleNotification(Notification notif, Object handback) {
         // handle notification
         ....
     }
 }

 MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
 NotificationEmitter emitter = (NotificationEmitter) mbean;
 MyListener listener = new MyListener();
 emitter.addNotificationListener(listener, null, null);
 

Since:
1.5
See Also:
JMX Specification., Ways to Access MXBeans

Method Summary
 void gc sample code for java.lang.management.MemoryMXBean.gc() definition code for java.lang.management.MemoryMXBean.gc() ()
          Runs the garbage collector.
 MemoryUsage sample code for java.lang.management.MemoryUsage definition code for java.lang.management.MemoryUsage getHeapMemoryUsage sample code for java.lang.management.MemoryMXBean.getHeapMemoryUsage() definition code for java.lang.management.MemoryMXBean.getHeapMemoryUsage() ()
          Returns the current memory usage of the heap that is used for object allocation.
 MemoryUsage sample code for java.lang.management.MemoryUsage definition code for java.lang.management.MemoryUsage getNonHeapMemoryUsage sample code for java.lang.management.MemoryMXBean.getNonHeapMemoryUsage() definition code for java.lang.management.MemoryMXBean.getNonHeapMemoryUsage() ()
          Returns the current memory usage of non-heap memory that is used by the Java virtual machine.
 int getObjectPendingFinalizationCount sample code for java.lang.management.MemoryMXBean.getObjectPendingFinalizationCount() definition code for java.lang.management.MemoryMXBean.getObjectPendingFinalizationCount() ()
          Returns the approximate number of objects for which finalization is pending.
 boolean isVerbose sample code for java.lang.management.MemoryMXBean.isVerbose() definition code for java.lang.management.MemoryMXBean.isVerbose() ()
          Tests if verbose output for the memory system is enabled.
 void setVerbose sample code for java.lang.management.MemoryMXBean.setVerbose(boolean) definition code for java.lang.management.MemoryMXBean.setVerbose(boolean) (boolean value)
          Enables or disables verbose output for the memory system.
 

Method Detail

getObjectPendingFinalizationCount sample code for java.lang.management.MemoryMXBean.getObjectPendingFinalizationCount() definition code for java.lang.management.MemoryMXBean.getObjectPendingFinalizationCount()

int getObjectPendingFinalizationCount()
Returns the approximate number of objects for which finalization is pending.

Returns:
the approximate number objects for which finalization is pending.

getHeapMemoryUsage sample code for java.lang.management.MemoryMXBean.getHeapMemoryUsage() definition code for java.lang.management.MemoryMXBean.getHeapMemoryUsage()

MemoryUsage sample code for java.lang.management.MemoryUsage definition code for java.lang.management.MemoryUsage  getHeapMemoryUsage()
Returns the current memory usage of the heap that is used for object allocation. The heap consists of one or more memory pools. The used and committed size of the returned memory usage is the sum of those values of all heap memory pools whereas the init and max size of the returned memory usage represents the setting of the heap memory which may not be the sum of those of all heap memory pools.

The amount of used memory in the returned memory usage is the amount of memory occupied by both live objects and garbage objects that have not been collected, if any.

MBeanServer access:
The mapped type of MemoryUsage is CompositeData with attributes as specified in MemoryUsage sample code for java.lang.management.MemoryUsage.from(javax.management.openmbean.CompositeData) definition code for java.lang.management.MemoryUsage.from(javax.management.openmbean.CompositeData) .

Returns:
a MemoryUsage sample code for java.lang.management.MemoryUsage definition code for java.lang.management.MemoryUsage object representing the heap memory usage.

getNonHeapMemoryUsage sample code for java.lang.management.MemoryMXBean.getNonHeapMemoryUsage() definition code for java.lang.management.MemoryMXBean.getNonHeapMemoryUsage()

MemoryUsage sample code for java.lang.management.MemoryUsage definition code for java.lang.management.MemoryUsage  getNonHeapMemoryUsage()
Returns the current memory usage of non-heap memory that is used by the Java virtual machine. The non-heap memory consists of one or more memory pools. The used and committed size of the returned memory usage is the sum of those values of all non-heap memory pools whereas the init and max size of the returned memory usage represents the setting of the non-heap memory which may not be the sum of those of all non-heap memory pools.

MBeanServer access:
The mapped type of MemoryUsage is CompositeData with attributes as specified in MemoryUsage sample code for java.lang.management.MemoryUsage.from(javax.management.openmbean.CompositeData) definition code for java.lang.management.MemoryUsage.from(javax.management.openmbean.CompositeData) .

Returns:
a MemoryUsage sample code for java.lang.management.MemoryUsage definition code for java.lang.management.MemoryUsage object representing the non-heap memory usage.

isVerbose sample code for java.lang.management.MemoryMXBean.isVerbose() definition code for java.lang.management.MemoryMXBean.isVerbose()

boolean isVerbose()
Tests if verbose output for the memory system is enabled.

Returns:
true if verbose output for the memory system is enabled; false otherwise.

setVerbose sample code for java.lang.management.MemoryMXBean.setVerbose(boolean) definition code for java.lang.management.MemoryMXBean.setVerbose(boolean)

void setVerbose(boolean value)
Enables or disables verbose output for the memory system. The verbose output information and the output stream to which the verbose information is emitted are implementation dependent. Typically, a Java virtual machine implementation prints a message whenever it frees memory at garbage collection.

Each invocation of this method enables or disables verbose output globally.

Parameters:
value - true to enable verbose output; false to disable.
Throws:
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and the caller does not have ManagementPermission("control").

gc sample code for java.lang.management.MemoryMXBean.gc() definition code for java.lang.management.MemoryMXBean.gc()

void gc()
Runs the garbage collector. The call gc() is effectively equivalent to the call:
 System.gc()
 

See Also:
System.gc() sample code for java.lang.System.gc() definition code for java.lang.System.gc()