Path: blob/master/src/java.management/share/classes/java/lang/management/MemoryMXBean.java
41159 views
/*1* Copyright (c) 2003, 2020, 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;2627import javax.management.openmbean.CompositeData;2829/**30* The management interface for the memory system of31* the Java virtual machine.32*33* <p> A Java virtual machine has a single instance of the implementation34* class of this interface. This instance implementing this interface is35* an <a href="ManagementFactory.html#MXBean">MXBean</a>36* that can be obtained by calling37* the {@link ManagementFactory#getMemoryMXBean} method or38* from the {@link ManagementFactory#getPlatformMBeanServer39* platform MBeanServer} method.40*41* <p>The {@code ObjectName} for uniquely identifying the MXBean for42* the memory system within an MBeanServer is:43* <blockquote>44* {@link ManagementFactory#MEMORY_MXBEAN_NAME45* java.lang:type=Memory}46* </blockquote>47*48* It can be obtained by calling the49* {@link PlatformManagedObject#getObjectName} method.50*51* <h2> Memory </h2>52* The memory system of the Java virtual machine manages53* the following kinds of memory:54*55* <h3> 1. Heap </h3>56* The Java virtual machine has a <i>heap</i> that is the runtime57* data area from which memory for all class instances and arrays58* are allocated. It is created at the Java virtual machine start-up.59* Heap memory for objects is reclaimed by an automatic memory management60* system which is known as a <i>garbage collector</i>.61*62* <p>The heap may be of a fixed size or may be expanded and shrunk.63* The memory for the heap does not need to be contiguous.64*65* <h3> 2. Non-Heap Memory</h3>66* The Java virtual machine manages memory other than the heap67* (referred as <i>non-heap memory</i>).68*69* <p> The Java virtual machine has a <i>method area</i> that is shared70* among all threads.71* The method area belongs to non-heap memory. It stores per-class structures72* such as a runtime constant pool, field and method data, and the code for73* methods and constructors. It is created at the Java virtual machine74* start-up.75*76* <p> The method area is logically part of the heap but a Java virtual77* machine implementation may choose not to either garbage collect78* or compact it. Similar to the heap, the method area may be of a79* fixed size or may be expanded and shrunk. The memory for the80* method area does not need to be contiguous.81*82* <p>In addition to the method area, a Java virtual machine83* implementation may require memory for internal processing or84* optimization which also belongs to non-heap memory.85* For example, the JIT compiler requires memory for storing the native86* machine code translated from the Java virtual machine code for87* high performance.88*89* <h2>Memory Pools and Memory Managers</h2>90* {@link MemoryPoolMXBean Memory pools} and91* {@link MemoryManagerMXBean memory managers} are the abstract entities92* that monitor and manage the memory system93* of the Java virtual machine.94*95* <p>A memory pool represents a memory area that the Java virtual machine96* manages. The Java virtual machine has at least one memory pool97* and it may create or remove memory pools during execution.98* A memory pool can belong to either the heap or the non-heap memory.99*100* <p>A memory manager is responsible for managing one or more memory pools.101* The garbage collector is one type of memory manager responsible102* for reclaiming memory occupied by unreachable objects. A Java virtual103* machine may have one or more memory managers. It may104* add or remove memory managers during execution.105* A memory pool can be managed by more than one memory manager.106*107* <h2>Memory Usage Monitoring</h2>108*109* Memory usage is a very important monitoring attribute for the memory system.110* The memory usage, for example, could indicate:111* <ul>112* <li>the memory usage of an application,</li>113* <li>the workload being imposed on the automatic memory management system,</li>114* <li>potential memory leakage.</li>115* </ul>116*117* <p>118* The memory usage can be monitored in three ways:119* <ul>120* <li>Polling</li>121* <li>Usage Threshold Notification</li>122* <li>Collection Usage Threshold Notification</li>123* </ul>124*125* Details are specified in the {@link MemoryPoolMXBean} interface.126*127* <p>The memory usage monitoring mechanism is intended for load-balancing128* or workload distribution use. For example, an application would stop129* receiving any new workload when its memory usage exceeds a130* certain threshold. It is not intended for an application to detect131* and recover from a low memory condition.132*133* <h2>Notifications</h2>134*135* <p>This {@code MemoryMXBean} is a136* {@link javax.management.NotificationEmitter NotificationEmitter}137* that emits two types of memory {@link javax.management.Notification138* notifications} if any one of the memory pools139* supports a <a href="MemoryPoolMXBean.html#UsageThreshold">usage threshold</a>140* or a <a href="MemoryPoolMXBean.html#CollectionThreshold">collection usage141* threshold</a> which can be determined by calling the142* {@link MemoryPoolMXBean#isUsageThresholdSupported} and143* {@link MemoryPoolMXBean#isCollectionUsageThresholdSupported} methods.144* <ul>145* <li>{@link MemoryNotificationInfo#MEMORY_THRESHOLD_EXCEEDED146* usage threshold exceeded notification} - for notifying that147* the memory usage of a memory pool is increased and has reached148* or exceeded its149* <a href="MemoryPoolMXBean.html#UsageThreshold"> usage threshold</a> value.150* </li>151* <li>{@link MemoryNotificationInfo#MEMORY_COLLECTION_THRESHOLD_EXCEEDED152* collection usage threshold exceeded notification} - for notifying that153* the memory usage of a memory pool is greater than or equal to its154* <a href="MemoryPoolMXBean.html#CollectionThreshold">155* collection usage threshold</a> after the Java virtual machine156* has expended effort in recycling unused objects in that157* memory pool.</li>158* </ul>159*160* <p>161* The notification emitted is a {@link javax.management.Notification}162* instance whose {@link javax.management.Notification#setUserData163* user data} is set to a {@link CompositeData CompositeData}164* that represents a {@link MemoryNotificationInfo} object165* containing information about the memory pool when the notification166* was constructed. The {@code CompositeData} contains the attributes167* as described in {@link MemoryNotificationInfo#from168* MemoryNotificationInfo}.169*170* <hr>171* <h2>NotificationEmitter</h2>172* The {@code MemoryMXBean} object returned by173* {@link ManagementFactory#getMemoryMXBean} implements174* the {@link javax.management.NotificationEmitter NotificationEmitter}175* interface that allows a listener to be registered within the176* {@code MemoryMXBean} as a notification listener.177*178* Below is an example code that registers a {@code MyListener} to handle179* notification emitted by the {@code MemoryMXBean}.180*181* <blockquote><pre>182* class MyListener implements javax.management.NotificationListener {183* public void handleNotification(Notification notif, Object handback) {184* // handle notification185* ....186* }187* }188*189* MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();190* NotificationEmitter emitter = (NotificationEmitter) mbean;191* MyListener listener = new MyListener();192* emitter.addNotificationListener(listener, null, null);193* </pre></blockquote>194*195* @see ManagementFactory#getPlatformMXBeans(Class)196* @see <a href="../../../javax/management/package-summary.html">197* JMX Specification.</a>198* @see <a href="package-summary.html#examples">199* Ways to Access MXBeans</a>200*201* @author Mandy Chung202* @since 1.5203*/204public interface MemoryMXBean extends PlatformManagedObject {205/**206* Returns the approximate number of objects for which207* finalization is pending.208*209* @return the approximate number objects for which finalization210* is pending.211*/212public int getObjectPendingFinalizationCount();213214/**215* Returns the current memory usage of the heap that216* is used for object allocation. The heap consists217* of one or more memory pools. The {@code used}218* and {@code committed} size of the returned memory219* usage is the sum of those values of all heap memory pools220* whereas the {@code init} and {@code max} size of the221* returned memory usage represents the setting of the heap222* memory which may not be the sum of those of all heap223* memory pools.224* <p>225* The amount of used memory in the returned memory usage226* is the amount of memory occupied by both live objects227* and garbage objects that have not been collected, if any.228*229* <p>230* <b>MBeanServer access</b>:<br>231* The mapped type of {@code MemoryUsage} is232* {@code CompositeData} with attributes as specified in233* {@link MemoryUsage#from MemoryUsage}.234*235* @return a {@link MemoryUsage} object representing236* the heap memory usage.237*/238public MemoryUsage getHeapMemoryUsage();239240/**241* Returns the current memory usage of non-heap memory that242* is used by the Java virtual machine.243* The non-heap memory consists of one or more memory pools.244* The {@code used} and {@code committed} size of the245* returned memory usage is the sum of those values of246* all non-heap memory pools whereas the {@code init}247* and {@code max} size of the returned memory usage248* represents the setting of the non-heap249* memory which may not be the sum of those of all non-heap250* memory pools.251*252* <p>253* <b>MBeanServer access</b>:<br>254* The mapped type of {@code MemoryUsage} is255* {@code CompositeData} with attributes as specified in256* {@link MemoryUsage#from MemoryUsage}.257*258* @return a {@link MemoryUsage} object representing259* the non-heap memory usage.260*/261public MemoryUsage getNonHeapMemoryUsage();262263/**264* Tests if verbose output for the memory system is enabled.265*266* @return {@code true} if verbose output for the memory267* system is enabled; {@code false} otherwise.268*/269public boolean isVerbose();270271/**272* Enables or disables verbose output for the memory273* system. The verbose output information and the output stream274* to which the verbose information is emitted are implementation275* dependent. Typically, a Java virtual machine implementation276* prints a message whenever it frees memory at garbage collection.277*278* <p>279* Each invocation of this method enables or disables verbose280* output globally.281*282* @param value {@code true} to enable verbose output;283* {@code false} to disable.284*285* @throws java.lang.SecurityException if a security manager286* exists and the caller does not have287* ManagementPermission("control").288*/289public void setVerbose(boolean value);290291/**292* Runs the garbage collector.293* The call <code>gc()</code> is effectively equivalent to the294* call:295* <blockquote><pre>296* System.gc()297* </pre></blockquote>298*299* @see java.lang.System#gc()300*/301public void gc();302303}304305306