0N/A/*
2362N/A * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.lang.management;
0N/A
0N/Aimport javax.management.openmbean.CompositeData;
0N/Aimport sun.management.MemoryUsageCompositeData;
0N/A
0N/A/**
0N/A * A <tt>MemoryUsage</tt> object represents a snapshot of memory usage.
0N/A * Instances of the <tt>MemoryUsage</tt> class are usually constructed
0N/A * by methods that are used to obtain memory usage
0N/A * information about individual memory pool of the Java virtual machine or
0N/A * the heap or non-heap memory of the Java virtual machine as a whole.
0N/A *
0N/A * <p> A <tt>MemoryUsage</tt> object contains four values:
0N/A * <ul>
0N/A * <table>
0N/A * <tr>
0N/A * <td valign=top> <tt>init</tt> </td>
0N/A * <td valign=top> represents the initial amount of memory (in bytes) that
0N/A * the Java virtual machine requests from the operating system
0N/A * for memory management during startup. The Java virtual machine
0N/A * may request additional memory from the operating system and
0N/A * may also release memory to the system over time.
0N/A * The value of <tt>init</tt> may be undefined.
0N/A * </td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td valign=top> <tt>used</tt> </td>
0N/A * <td valign=top> represents the amount of memory currently used (in bytes).
0N/A * </td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td valign=top> <tt>committed</tt> </td>
0N/A * <td valign=top> represents the amount of memory (in bytes) that is
0N/A * guaranteed to be available for use by the Java virtual machine.
0N/A * The amount of committed memory may change over time (increase
0N/A * or decrease). The Java virtual machine may release memory to
0N/A * the system and <tt>committed</tt> could be less than <tt>init</tt>.
0N/A * <tt>committed</tt> will always be greater than
0N/A * or equal to <tt>used</tt>.
0N/A * </td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td valign=top> <tt>max</tt> </td>
0N/A * <td valign=top> represents the maximum amount of memory (in bytes)
0N/A * that can be used for memory management. Its value may be undefined.
0N/A * The maximum amount of memory may change over time if defined.
0N/A * The amount of used and committed memory will always be less than
0N/A * or equal to <tt>max</tt> if <tt>max</tt> is defined.
0N/A * A memory allocation may fail if it attempts to increase the
0N/A * used memory such that <tt>used &gt committed</tt> even
0N/A * if <tt>used &lt= max</tt> would still be true (for example,
0N/A * when the system is low on virtual memory).
0N/A * </td>
0N/A * </tr>
0N/A * </table>
0N/A * </ul>
0N/A *
0N/A * Below is a picture showing an example of a memory pool:
0N/A * <p>
0N/A * <pre>
0N/A * +----------------------------------------------+
0N/A * +//////////////// | +
0N/A * +//////////////// | +
0N/A * +----------------------------------------------+
0N/A *
0N/A * |--------|
0N/A * init
0N/A * |---------------|
0N/A * used
0N/A * |---------------------------|
0N/A * committed
0N/A * |----------------------------------------------|
0N/A * max
0N/A * </pre>
0N/A *
0N/A * <h4>MXBean Mapping</h4>
0N/A * <tt>MemoryUsage</tt> is mapped to a {@link CompositeData CompositeData}
0N/A * with attributes as specified in the {@link #from from} method.
0N/A *
0N/A * @author Mandy Chung
0N/A * @since 1.5
0N/A */
0N/Apublic class MemoryUsage {
0N/A private final long init;
0N/A private final long used;
0N/A private final long committed;
0N/A private final long max;
0N/A
0N/A /**
0N/A * Constructs a <tt>MemoryUsage</tt> object.
0N/A *
0N/A * @param init the initial amount of memory in bytes that
0N/A * the Java virtual machine allocates;
0N/A * or <tt>-1</tt> if undefined.
0N/A * @param used the amount of used memory in bytes.
0N/A * @param committed the amount of committed memory in bytes.
0N/A * @param max the maximum amount of memory in bytes that
0N/A * can be used; or <tt>-1</tt> if undefined.
0N/A *
0N/A * @throws IllegalArgumentException if
0N/A * <ul>
0N/A * <li> the value of <tt>init</tt> or <tt>max</tt> is negative
0N/A * but not <tt>-1</tt>; or</li>
0N/A * <li> the value of <tt>used</tt> or <tt>committed</tt> is negative;
0N/A * or</li>
0N/A * <li> <tt>used</tt> is greater than the value of <tt>committed</tt>;
0N/A * or</li>
0N/A * <li> <tt>committed</tt> is greater than the value of <tt>max</tt>
0N/A * <tt>max</tt> if defined.</li>
0N/A * </ul>
0N/A */
0N/A public MemoryUsage(long init,
0N/A long used,
0N/A long committed,
0N/A long max) {
0N/A if (init < -1) {
0N/A throw new IllegalArgumentException( "init parameter = " +
0N/A init + " is negative but not -1.");
0N/A }
0N/A if (max < -1) {
0N/A throw new IllegalArgumentException( "max parameter = " +
0N/A max + " is negative but not -1.");
0N/A }
0N/A if (used < 0) {
0N/A throw new IllegalArgumentException( "used parameter = " +
0N/A used + " is negative.");
0N/A }
0N/A if (committed < 0) {
0N/A throw new IllegalArgumentException( "committed parameter = " +
0N/A committed + " is negative.");
0N/A }
0N/A if (used > committed) {
0N/A throw new IllegalArgumentException( "used = " + used +
0N/A " should be <= committed = " + committed);
0N/A }
0N/A if (max >= 0 && committed > max) {
0N/A throw new IllegalArgumentException( "committed = " + committed +
0N/A " should be < max = " + max);
0N/A }
0N/A
0N/A this.init = init;
0N/A this.used = used;
0N/A this.committed = committed;
0N/A this.max = max;
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <tt>MemoryUsage</tt> object from a
0N/A * {@link CompositeData CompositeData}.
0N/A */
0N/A private MemoryUsage(CompositeData cd) {
0N/A // validate the input composite data
0N/A MemoryUsageCompositeData.validateCompositeData(cd);
0N/A
0N/A this.init = MemoryUsageCompositeData.getInit(cd);
0N/A this.used = MemoryUsageCompositeData.getUsed(cd);
0N/A this.committed = MemoryUsageCompositeData.getCommitted(cd);
0N/A this.max = MemoryUsageCompositeData.getMax(cd);
0N/A }
0N/A
0N/A /**
0N/A * Returns the amount of memory in bytes that the Java virtual machine
0N/A * initially requests from the operating system for memory management.
0N/A * This method returns <tt>-1</tt> if the initial memory size is undefined.
0N/A *
0N/A * @return the initial size of memory in bytes;
0N/A * <tt>-1</tt> if undefined.
0N/A */
0N/A public long getInit() {
0N/A return init;
0N/A }
0N/A
0N/A /**
0N/A * Returns the amount of used memory in bytes.
0N/A *
0N/A * @return the amount of used memory in bytes.
0N/A *
0N/A */
0N/A public long getUsed() {
0N/A return used;
0N/A };
0N/A
0N/A /**
0N/A * Returns the amount of memory in bytes that is committed for
0N/A * the Java virtual machine to use. This amount of memory is
0N/A * guaranteed for the Java virtual machine to use.
0N/A *
0N/A * @return the amount of committed memory in bytes.
0N/A *
0N/A */
0N/A public long getCommitted() {
0N/A return committed;
0N/A };
0N/A
0N/A /**
0N/A * Returns the maximum amount of memory in bytes that can be
0N/A * used for memory management. This method returns <tt>-1</tt>
0N/A * if the maximum memory size is undefined.
0N/A *
0N/A * <p> This amount of memory is not guaranteed to be available
0N/A * for memory management if it is greater than the amount of
0N/A * committed memory. The Java virtual machine may fail to allocate
0N/A * memory even if the amount of used memory does not exceed this
0N/A * maximum size.
0N/A *
0N/A * @return the maximum amount of memory in bytes;
0N/A * <tt>-1</tt> if undefined.
0N/A */
0N/A public long getMax() {
0N/A return max;
0N/A };
0N/A
0N/A /**
0N/A * Returns a descriptive representation of this memory usage.
0N/A */
0N/A public String toString() {
0N/A StringBuffer buf = new StringBuffer();
0N/A buf.append("init = " + init + "(" + (init >> 10) + "K) ");
0N/A buf.append("used = " + used + "(" + (used >> 10) + "K) ");
0N/A buf.append("committed = " + committed + "(" +
0N/A (committed >> 10) + "K) " );
0N/A buf.append("max = " + max + "(" + (max >> 10) + "K)");
0N/A return buf.toString();
0N/A }
0N/A
0N/A /**
0N/A * Returns a <tt>MemoryUsage</tt> object represented by the
0N/A * given <tt>CompositeData</tt>. The given <tt>CompositeData</tt>
0N/A * must contain the following attributes:
0N/A * <p>
0N/A * <blockquote>
0N/A * <table border>
0N/A * <tr>
0N/A * <th align=left>Attribute Name</th>
0N/A * <th align=left>Type</th>
0N/A * </tr>
0N/A * <tr>
0N/A * <td>init</td>
0N/A * <td><tt>java.lang.Long</tt></td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td>used</td>
0N/A * <td><tt>java.lang.Long</tt></td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td>committed</td>
0N/A * <td><tt>java.lang.Long</tt></td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td>max</td>
0N/A * <td><tt>java.lang.Long</tt></td>
0N/A * </tr>
0N/A * </table>
0N/A * </blockquote>
0N/A *
0N/A * @param cd <tt>CompositeData</tt> representing a <tt>MemoryUsage</tt>
0N/A *
0N/A * @throws IllegalArgumentException if <tt>cd</tt> does not
0N/A * represent a <tt>MemoryUsage</tt> with the attributes described
0N/A * above.
0N/A *
0N/A * @return a <tt>MemoryUsage</tt> object represented by <tt>cd</tt>
0N/A * if <tt>cd</tt> is not <tt>null</tt>;
0N/A * <tt>null</tt> otherwise.
0N/A */
0N/A public static MemoryUsage from(CompositeData cd) {
0N/A if (cd == null) {
0N/A return null;
0N/A }
0N/A
0N/A if (cd instanceof MemoryUsageCompositeData) {
0N/A return ((MemoryUsageCompositeData) cd).getMemoryUsage();
0N/A } else {
0N/A return new MemoryUsage(cd);
0N/A }
0N/A
0N/A }
0N/A}