1. /*
  2. * @(#)MemoryMXBean.java 1.14 04/04/20
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang.management;
  8. import javax.management.openmbean.CompositeData;
  9. /**
  10. * The management interface for the memory system of
  11. * the Java virtual machine.
  12. *
  13. * <p> A Java virtual machine has a single instance of the implementation
  14. * class of this interface. This instance implementing this interface is
  15. * an <a href="ManagementFactory.html#MXBean">MXBean</a>
  16. * that can be obtained by calling
  17. * the {@link ManagementFactory#getMemoryMXBean} method or
  18. * from the {@link ManagementFactory#getPlatformMBeanServer
  19. * platform <tt>MBeanServer</tt>} method.
  20. *
  21. * <p>The <tt>ObjectName</tt> for uniquely identifying the MXBean for
  22. * the memory system within an MBeanServer is:
  23. * <blockquote>
  24. * {@link ManagementFactory#MEMORY_MXBEAN_NAME
  25. * <tt>java.lang:type=Memory</tt>}
  26. * </blockquote>
  27. *
  28. * <h4> Memory </h4>
  29. * The memory system of the Java virtual machine manages
  30. * the following kinds of memory:
  31. *
  32. * <h4> 1. Heap </h4>
  33. * The Java virtual machine has a <i>heap</i> that is the runtime
  34. * data area from which memory for all class instances and arrays
  35. * are allocated. It is created at the Java virtual machine start-up.
  36. * Heap memory for objects is reclaimed by an automatic memory management
  37. * system which is known as a <i>garbage collector</i>.
  38. *
  39. * <p>The heap may be of a fixed size or may be expanded and shrunk.
  40. * The memory for the heap does not need to be contiguous.
  41. *
  42. * <h4> 2. Non-Heap Memory</h4>
  43. * The Java virtual machine manages memory other than the heap
  44. * (referred as <i>non-heap memory</i>).
  45. *
  46. * <p> The Java virtual machine has a <i>method area</i> that is shared
  47. * among all threads.
  48. * The method area belongs to non-heap memory. It stores per-class structures
  49. * such as a runtime constant pool, field and method data, and the code for
  50. * methods and constructors. It is created at the Java virtual machine
  51. * start-up.
  52. *
  53. * <p> The method area is logically part of the heap but a Java virtual
  54. * machine implementation may choose not to either garbage collect
  55. * or compact it. Similar to the heap, the method area may be of a
  56. * fixed size or may be expanded and shrunk. The memory for the
  57. * method area does not need to be contiguous.
  58. *
  59. * <p>In addition to the method area, a Java virtual machine
  60. * implementation may require memory for internal processing or
  61. * optimization which also belongs to non-heap memory.
  62. * For example, the JIT compiler requires memory for storing the native
  63. * machine code translated from the Java virtual machine code for
  64. * high performance.
  65. *
  66. * <h4>Memory Pools and Memory Managers</h4>
  67. * {@link MemoryPoolMXBean Memory pools} and
  68. * {@link MemoryManagerMXBean memory managers} are the abstract entities
  69. * that monitor and manage the memory system
  70. * of the Java virtual machine.
  71. *
  72. * <p>A memory pool represents a memory area that the Java virtual machine
  73. * manages. The Java virtual machine has at least one memory pool
  74. * and it may create or remove memory pools during execution.
  75. * A memory pool can belong to either the heap or the non-heap memory.
  76. *
  77. * <p>A memory manager is responsible for managing one or more memory pools.
  78. * The garbage collector is one type of memory manager responsible
  79. * for reclaiming memory occupied by unreachable objects. A Java virtual
  80. * machine may have one or more memory managers. It may
  81. * add or remove memory managers during execution.
  82. * A memory pool can be managed by more than one memory manager.
  83. *
  84. * <h4>Memory Usage Monitoring</h4>
  85. *
  86. * Memory usage is a very important monitoring attribute for the memory system.
  87. * The memory usage, for example, could indicate:
  88. * <ul>
  89. * <li>the memory usage of an application,</li>
  90. * <li>the workload being imposed on the automatic memory management system,</li>
  91. * <li>potential memory leakage.</li>
  92. * </ul>
  93. *
  94. * <p>
  95. * The memory usage can be monitored in three ways:
  96. * <ul>
  97. * <li>Polling</li>
  98. * <li>Usage Threshold Notification</li>
  99. * <li>Collection Usage Threshold Notification</li>
  100. * </ul>
  101. *
  102. * Details are specified in the {@link MemoryPoolMXBean} interface.
  103. *
  104. * <p>The memory usage monitoring mechanism is intended for load-balancing
  105. * or workload distribution use. For example, an application would stop
  106. * receiving any new workload when its memory usage exceeds a
  107. * certain threshold. It is not intended for an application to detect
  108. * and recover from a low memory condition.
  109. *
  110. * <h4>Notifications</h4>
  111. *
  112. * <p>This <tt>MemoryMXBean</tt> is a
  113. * {@link javax.management.NotificationEmitter NotificationEmitter}
  114. * that emits two types of memory {@link javax.management.Notification
  115. * notifications} if any one of the memory pools
  116. * supports a <a href="MemoryPoolMXBean#UsageThreshold">usage threshold</a>
  117. * or a <a href="MemoryPoolMXBean#CollectionThreshold">collection usage
  118. * threshold</a> which can be determined by calling the
  119. * {@link MemoryPoolMXBean#isUsageThresholdSupported} and
  120. * {@link MemoryPoolMXBean#isCollectionUsageThresholdSupported} methods.
  121. * <ul>
  122. * <li>{@link MemoryNotificationInfo#MEMORY_THRESHOLD_EXCEEDED
  123. * usage threshold exceeded notification} - for notifying that
  124. * the memory usage of a memory pool is increased and has reached
  125. * or exceeded its
  126. * <a href="MemoryPoolMXBean#UsageThreshold"> usage threshold</a> value.
  127. * </li>
  128. * <li>{@link MemoryNotificationInfo#MEMORY_COLLECTION_THRESHOLD_EXCEEDED
  129. * collection usage threshold exceeded notification} - for notifying that
  130. * the memory usage of a memory pool is greater than or equal to its
  131. * <a href="MemoryPoolMXBean#CollectionThreshold">
  132. * collection usage threshold</a> after the Java virtual machine
  133. * has expended effort in recycling unused objects in that
  134. * memory pool.</li>
  135. * </ul>
  136. *
  137. * <p>
  138. * The notification emitted is a {@link javax.management.Notification}
  139. * instance whose {@link javax.management.Notification#setUserData
  140. * user data} is set to a {@link CompositeData CompositeData}
  141. * that represents a {@link MemoryNotificationInfo} object
  142. * containing information about the memory pool when the notification
  143. * was constructed. The <tt>CompositeData</tt> contains the attributes
  144. * as described in {@link MemoryNotificationInfo#from
  145. * MemoryNotificationInfo}.
  146. *
  147. * <hr>
  148. * <h4>NotificationEmitter</h4>
  149. * The <tt>MemoryMXBean</tt> object returned by
  150. * {@link ManagementFactory#getMemoryMXBean} implements
  151. * the {@link javax.management.NotificationEmitter NotificationEmitter}
  152. * interface that allows a listener to be registered within the
  153. * <tt>MemoryMXBean</tt> as a notification listener.
  154. *
  155. * Below is an example code that registers a <tt>MyListener</tt> to handle
  156. * notification emitted by the <tt>MemoryMXBean</tt>.
  157. *
  158. * <blockquote><pre>
  159. * class MyListener implements javax.management.NotificationListener {
  160. * public void handleNotification(Notification notif, Object handback) {
  161. * // handle notification
  162. * ....
  163. * }
  164. * }
  165. *
  166. * MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
  167. * NotificationEmitter emitter = (NotificationEmitter) mbean;
  168. * MyListener listener = new MyListener();
  169. * emitter.addNotificationListener(listener, null, null);
  170. * </pre></blockquote>
  171. *
  172. * @see <a href="../../../javax/management/package-summary.html">
  173. * JMX Specification.</a>
  174. * @see <a href="package-summary.html#examples">
  175. * Ways to Access MXBeans</a>
  176. *
  177. * @author Mandy Chung
  178. * @version 1.14, 04/20/04
  179. * @since 1.5
  180. */
  181. public interface MemoryMXBean {
  182. /**
  183. * Returns the approximate number of objects for which
  184. * finalization is pending.
  185. *
  186. * @return the approximate number objects for which finalization
  187. * is pending.
  188. */
  189. public int getObjectPendingFinalizationCount();
  190. /**
  191. * Returns the current memory usage of the heap that
  192. * is used for object allocation. The heap consists
  193. * of one or more memory pools. The <tt>used</tt>
  194. * and <tt>committed</tt> size of the returned memory
  195. * usage is the sum of those values of all heap memory pools
  196. * whereas the <tt>init</tt> and <tt>max</tt> size of the
  197. * returned memory usage represents the setting of the heap
  198. * memory which may not be the sum of those of all heap
  199. * memory pools.
  200. * <p>
  201. * The amount of used memory in the returned memory usage
  202. * is the amount of memory occupied by both live objects
  203. * and garbage objects that have not been collected, if any.
  204. *
  205. * <p>
  206. * <b>MBeanServer access</b>:<br>
  207. * The mapped type of <tt>MemoryUsage</tt> is
  208. * <tt>CompositeData</tt> with attributes as specified in
  209. * {@link MemoryUsage#from MemoryUsage}.
  210. *
  211. * @return a {@link MemoryUsage} object representing
  212. * the heap memory usage.
  213. */
  214. public MemoryUsage getHeapMemoryUsage();
  215. /**
  216. * Returns the current memory usage of non-heap memory that
  217. * is used by the Java virtual machine.
  218. * The non-heap memory consists of one or more memory pools.
  219. * The <tt>used</tt> and <tt>committed</tt> size of the
  220. * returned memory usage is the sum of those values of
  221. * all non-heap memory pools whereas the <tt>init</tt>
  222. * and <tt>max</tt> size of the returned memory usage
  223. * represents the setting of the non-heap
  224. * memory which may not be the sum of those of all non-heap
  225. * memory pools.
  226. *
  227. * <p>
  228. * <b>MBeanServer access</b>:<br>
  229. * The mapped type of <tt>MemoryUsage</tt> is
  230. * <tt>CompositeData</tt> with attributes as specified in
  231. * {@link MemoryUsage#from MemoryUsage}.
  232. *
  233. * @return a {@link MemoryUsage} object representing
  234. * the non-heap memory usage.
  235. */
  236. public MemoryUsage getNonHeapMemoryUsage();
  237. /**
  238. * Tests if verbose output for the memory system is enabled.
  239. *
  240. * @return <tt>true</tt> if verbose output for the memory
  241. * system is enabled; <tt>false</tt> otherwise.
  242. */
  243. public boolean isVerbose();
  244. /**
  245. * Enables or disables verbose output for the memory
  246. * system. The verbose output information and the output stream
  247. * to which the verbose information is emitted are implementation
  248. * dependent. Typically, a Java virtual machine implementation
  249. * prints a message whenever it frees memory at garbage collection.
  250. *
  251. * <p>
  252. * Each invocation of this method enables or disables verbose
  253. * output globally.
  254. *
  255. * @param value <tt>true</tt> to enable verbose output;
  256. * <tt>false</tt> to disable.
  257. *
  258. * @exception java.lang.SecurityException if a security manager
  259. * exists and the caller does not have
  260. * ManagementPermission("control").
  261. */
  262. public void setVerbose(boolean value);
  263. /**
  264. * Runs the garbage collector.
  265. * The call <code>gc()</code> is effectively equivalent to the
  266. * call:
  267. * <blockquote><pre>
  268. * System.gc()
  269. * </pre></blockquote>
  270. *
  271. * @see java.lang.System#gc()
  272. */
  273. public void gc();
  274. }