1. /*
  2. * @(#)MemoryUsage.java 1.16 04/05/25
  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. import sun.management.MemoryUsageCompositeData;
  10. /**
  11. * A <tt>MemoryUsage</tt> object represents a snapshot of memory usage.
  12. * Instances of the <tt>MemoryUsage</tt> class are usually constructed
  13. * by methods that are used to obtain memory usage
  14. * information about individual memory pool of the Java virtual machine or
  15. * the heap or non-heap memory of the Java virtual machine as a whole.
  16. *
  17. * <p> A <tt>MemoryUsage</tt> object contains four values:
  18. * <ul>
  19. * <table>
  20. * <tr>
  21. * <td valign=top> <tt>init</tt> </td>
  22. * <td valign=top> represents the initial amount of memory (in bytes) that
  23. * the Java virtual machine requests from the operating system
  24. * for memory management during startup. The Java virtual machine
  25. * may request additional memory from the operating system and
  26. * may also release memory to the system over time.
  27. * The value of <tt>init</tt> may be undefined.
  28. * </td>
  29. * </tr>
  30. * <tr>
  31. * <td valign=top> <tt>used</tt> </td>
  32. * <td valign=top> represents the amount of memory currently used (in bytes).
  33. * </td>
  34. * </tr>
  35. * <tr>
  36. * <td valign=top> <tt>committed</tt> </td>
  37. * <td valign=top> represents the amount of memory (in bytes) that is
  38. * guaranteed to be available for use by the Java virtual machine.
  39. * The amount of committed memory may change over time (increase
  40. * or decrease). The Java virtual machine may release memory to
  41. * the system and <tt>committed</tt> could be less than <tt>init</tt>.
  42. * <tt>committed</tt> will always be greater than
  43. * or equal to <tt>used</tt>.
  44. * </td>
  45. * </tr>
  46. * <tr>
  47. * <td valign=top> <tt>max</tt> </td>
  48. * <td valign=top> represents the maximum amount of memory (in bytes)
  49. * that can be used for memory management. Its value may be undefined.
  50. * The maximum amount of memory may change over time if defined.
  51. * The amount of used and committed memory will always be less than
  52. * or equal to <tt>max</tt> if <tt>max</tt> is defined.
  53. * A memory allocation may fail if it attempts to increase the
  54. * used memory such that <tt>used > committed</tt> even
  55. * if <tt>used <= max</tt> would still be true (for example,
  56. * when the system is low on virtual memory).
  57. * </td>
  58. * </tr>
  59. * </table>
  60. * </ul>
  61. *
  62. * Below is a picture showing an example of a memory pool:
  63. * <p>
  64. * <pre>
  65. * +----------------------------------------------+
  66. * +//////////////// | +
  67. * +//////////////// | +
  68. * +----------------------------------------------+
  69. *
  70. * |--------|
  71. * init
  72. * |---------------|
  73. * used
  74. * |---------------------------|
  75. * committed
  76. * |----------------------------------------------|
  77. * max
  78. * </pre>
  79. *
  80. * <h4>MXBean Mapping</h4>
  81. * <tt>MemoryUsage</tt> is mapped to a {@link CompositeData CompositeData}
  82. * with attributes as specified in the {@link #from from} method.
  83. *
  84. * @author Mandy Chung
  85. * @version 1.16, 05/25/04
  86. * @since 1.5
  87. */
  88. public class MemoryUsage {
  89. private final long init;
  90. private final long used;
  91. private final long committed;
  92. private final long max;
  93. /**
  94. * Constructs a <tt>MemoryUsage</tt> object.
  95. *
  96. * @param init the initial amount of memory in bytes that
  97. * the Java virtual machine allocates;
  98. * or <tt>-1</tt> if undefined.
  99. * @param used the amount of used memory in bytes.
  100. * @param committed the amount of committed memory in bytes.
  101. * @param max the maximum amount of memory in bytes that
  102. * can be used; or <tt>-1</tt> if undefined.
  103. *
  104. * @throws IllegalArgumentException if
  105. * <ul>
  106. * <li> the value of <tt>init</tt> or <tt>max</tt> is negative
  107. * but not <tt>-1</tt> or</li>
  108. * <li> the value of <tt>used</tt> or <tt>committed</tt> is negative;
  109. * or</li>
  110. * <li> <tt>used</tt> is greater than the value of <tt>committed</tt>
  111. * or</li>
  112. * <li> <tt>committed</tt> is greater than the value of <tt>max</tt>
  113. * <tt>max</tt> if defined.</li>
  114. * </ul>
  115. */
  116. public MemoryUsage(long init,
  117. long used,
  118. long committed,
  119. long max) {
  120. if (init < -1) {
  121. throw new IllegalArgumentException( "init parameter = " +
  122. init + " is negative but not -1.");
  123. }
  124. if (max < -1) {
  125. throw new IllegalArgumentException( "max parameter = " +
  126. max + " is negative but not -1.");
  127. }
  128. if (used < 0) {
  129. throw new IllegalArgumentException( "used parameter = " +
  130. used + " is negative.");
  131. }
  132. if (committed < 0) {
  133. throw new IllegalArgumentException( "committed parameter = " +
  134. committed + " is negative.");
  135. }
  136. if (used > committed) {
  137. throw new IllegalArgumentException( "used = " + used +
  138. " should be <= committed = " + committed);
  139. }
  140. if (max >= 0 && committed > max) {
  141. throw new IllegalArgumentException( "committed = " + committed +
  142. " should be < max = " + max);
  143. }
  144. this.init = init;
  145. this.used = used;
  146. this.committed = committed;
  147. this.max = max;
  148. }
  149. /**
  150. * Constructs a <tt>MemoryUsage</tt> object from a
  151. * {@link CompositeData CompositeData}.
  152. */
  153. private MemoryUsage(CompositeData cd) {
  154. // validate the input composite data
  155. MemoryUsageCompositeData.validateCompositeData(cd);
  156. this.init = MemoryUsageCompositeData.getInit(cd);
  157. this.used = MemoryUsageCompositeData.getUsed(cd);
  158. this.committed = MemoryUsageCompositeData.getCommitted(cd);
  159. this.max = MemoryUsageCompositeData.getMax(cd);
  160. }
  161. /**
  162. * Returns the amount of memory in bytes that the Java virtual machine
  163. * initially requests from the operating system for memory management.
  164. * This method returns <tt>-1</tt> if the initial memory size is undefined.
  165. *
  166. * @return the initial size of memory in bytes;
  167. * <tt>-1</tt> if undefined.
  168. */
  169. public long getInit() {
  170. return init;
  171. }
  172. /**
  173. * Returns the amount of used memory in bytes.
  174. *
  175. * @return the amount of used memory in bytes.
  176. *
  177. */
  178. public long getUsed() {
  179. return used;
  180. };
  181. /**
  182. * Returns the amount of memory in bytes that is committed for
  183. * the Java virtual machine to use. This amount of memory is
  184. * guaranteed for the Java virtual machine to use.
  185. *
  186. * @return the amount of committed memory in bytes.
  187. *
  188. */
  189. public long getCommitted() {
  190. return committed;
  191. };
  192. /**
  193. * Returns the maximum amount of memory in bytes that can be
  194. * used for memory management. This method returns <tt>-1</tt>
  195. * if the maximum memory size is undefined.
  196. *
  197. * <p> This amount of memory is not guaranteed to be available
  198. * for memory management if it is greater than the amount of
  199. * committed memory. The Java virtual machine may fail to allocate
  200. * memory even if the amount of used memory does not exceed this
  201. * maximum size.
  202. *
  203. * @return the maximum amount of memory in bytes;
  204. * <tt>-1</tt> if undefined.
  205. */
  206. public long getMax() {
  207. return max;
  208. };
  209. /**
  210. * Returns a descriptive representation of this memory usage.
  211. */
  212. public String toString() {
  213. StringBuffer buf = new StringBuffer();
  214. buf.append("init = " + init + "(" + (init >> 10) + "K) ");
  215. buf.append("used = " + used + "(" + (used >> 10) + "K) ");
  216. buf.append("committed = " + committed + "(" +
  217. (committed >> 10) + "K) " );
  218. buf.append("max = " + max + "(" + (max >> 10) + "K)");
  219. return buf.toString();
  220. }
  221. /**
  222. * Returns a <tt>MemoryUsage</tt> object represented by the
  223. * given <tt>CompositeData</tt>. The given <tt>CompositeData</tt>
  224. * must contain the following attributes:
  225. * <p>
  226. * <blockquote>
  227. * <table border>
  228. * <tr>
  229. * <th align=left>Attribute Name</th>
  230. * <th align=left>Type</th>
  231. * </tr>
  232. * <tr>
  233. * <td>init</td>
  234. * <td><tt>java.lang.Long</tt></td>
  235. * </tr>
  236. * <tr>
  237. * <td>used</td>
  238. * <td><tt>java.lang.Long</tt></td>
  239. * </tr>
  240. * <tr>
  241. * <td>committed</td>
  242. * <td><tt>java.lang.Long</tt></td>
  243. * </tr>
  244. * <tr>
  245. * <td>max</td>
  246. * <td><tt>java.lang.Long</tt></td>
  247. * </tr>
  248. * </table>
  249. * </blockquote>
  250. *
  251. * @param cd <tt>CompositeData</tt> representing a <tt>MemoryUsage</tt>
  252. *
  253. * @throws IllegalArgumentException if <tt>cd</tt> does not
  254. * represent a <tt>MemoryUsage</tt> with the attributes described
  255. * above.
  256. *
  257. * @return a <tt>MemoryUsage</tt> object represented by <tt>cd</tt>
  258. * if <tt>cd</tt> is not <tt>null</tt>
  259. * <tt>null</tt> otherwise.
  260. */
  261. public static MemoryUsage from(CompositeData cd) {
  262. if (cd == null) {
  263. return null;
  264. }
  265. if (cd instanceof MemoryUsageCompositeData) {
  266. return ((MemoryUsageCompositeData) cd).getMemoryUsage();
  267. } else {
  268. return new MemoryUsage(cd);
  269. }
  270. }
  271. }