jdk/src/java.management/share/classes/java/lang/management/MemoryUsage.java
changeset 32034 05676cfd40b5
parent 25859 3317bb8137f4
child 44858 7183899b064b
equal deleted inserted replaced
32033:bf24e33c7919 32034:05676cfd40b5
    27 
    27 
    28 import javax.management.openmbean.CompositeData;
    28 import javax.management.openmbean.CompositeData;
    29 import sun.management.MemoryUsageCompositeData;
    29 import sun.management.MemoryUsageCompositeData;
    30 
    30 
    31 /**
    31 /**
    32  * A <tt>MemoryUsage</tt> object represents a snapshot of memory usage.
    32  * A {@code MemoryUsage} object represents a snapshot of memory usage.
    33  * Instances of the <tt>MemoryUsage</tt> class are usually constructed
    33  * Instances of the {@code MemoryUsage} class are usually constructed
    34  * by methods that are used to obtain memory usage
    34  * by methods that are used to obtain memory usage
    35  * information about individual memory pool of the Java virtual machine or
    35  * information about individual memory pool of the Java virtual machine or
    36  * the heap or non-heap memory of the Java virtual machine as a whole.
    36  * the heap or non-heap memory of the Java virtual machine as a whole.
    37  *
    37  *
    38  * <p> A <tt>MemoryUsage</tt> object contains four values:
    38  * <p> A {@code MemoryUsage} object contains four values:
    39  * <table summary="Describes the MemoryUsage object content">
    39  * <table summary="Describes the MemoryUsage object content">
    40  * <tr>
    40  * <tr>
    41  * <td valign=top> <tt>init</tt> </td>
    41  * <td valign=top> {@code init} </td>
    42  * <td valign=top> represents the initial amount of memory (in bytes) that
    42  * <td valign=top> represents the initial amount of memory (in bytes) that
    43  *      the Java virtual machine requests from the operating system
    43  *      the Java virtual machine requests from the operating system
    44  *      for memory management during startup.  The Java virtual machine
    44  *      for memory management during startup.  The Java virtual machine
    45  *      may request additional memory from the operating system and
    45  *      may request additional memory from the operating system and
    46  *      may also release memory to the system over time.
    46  *      may also release memory to the system over time.
    47  *      The value of <tt>init</tt> may be undefined.
    47  *      The value of {@code init} may be undefined.
    48  * </td>
    48  * </td>
    49  * </tr>
    49  * </tr>
    50  * <tr>
    50  * <tr>
    51  * <td valign=top> <tt>used</tt> </td>
    51  * <td valign=top> {@code used} </td>
    52  * <td valign=top> represents the amount of memory currently used (in bytes).
    52  * <td valign=top> represents the amount of memory currently used (in bytes).
    53  * </td>
    53  * </td>
    54  * </tr>
    54  * </tr>
    55  * <tr>
    55  * <tr>
    56  * <td valign=top> <tt>committed</tt> </td>
    56  * <td valign=top> {@code committed} </td>
    57  * <td valign=top> represents the amount of memory (in bytes) that is
    57  * <td valign=top> represents the amount of memory (in bytes) that is
    58  *      guaranteed to be available for use by the Java virtual machine.
    58  *      guaranteed to be available for use by the Java virtual machine.
    59  *      The amount of committed memory may change over time (increase
    59  *      The amount of committed memory may change over time (increase
    60  *      or decrease).  The Java virtual machine may release memory to
    60  *      or decrease).  The Java virtual machine may release memory to
    61  *      the system and <tt>committed</tt> could be less than <tt>init</tt>.
    61  *      the system and {@code committed} could be less than {@code init}.
    62  *      <tt>committed</tt> will always be greater than
    62  *      {@code committed} will always be greater than
    63  *      or equal to <tt>used</tt>.
    63  *      or equal to {@code used}.
    64  * </td>
    64  * </td>
    65  * </tr>
    65  * </tr>
    66  * <tr>
    66  * <tr>
    67  * <td valign=top> <tt>max</tt> </td>
    67  * <td valign=top> {@code max} </td>
    68  * <td valign=top> represents the maximum amount of memory (in bytes)
    68  * <td valign=top> represents the maximum amount of memory (in bytes)
    69  *      that can be used for memory management. Its value may be undefined.
    69  *      that can be used for memory management. Its value may be undefined.
    70  *      The maximum amount of memory may change over time if defined.
    70  *      The maximum amount of memory may change over time if defined.
    71  *      The amount of used and committed memory will always be less than
    71  *      The amount of used and committed memory will always be less than
    72  *      or equal to <tt>max</tt> if <tt>max</tt> is defined.
    72  *      or equal to {@code max} if {@code max} is defined.
    73  *      A memory allocation may fail if it attempts to increase the
    73  *      A memory allocation may fail if it attempts to increase the
    74  *      used memory such that <tt>used &gt; committed</tt> even
    74  *      used memory such that {@code used > committed} even
    75  *      if <tt>used &lt;= max</tt> would still be true (for example,
    75  *      if {@code used <= max} would still be true (for example,
    76  *      when the system is low on virtual memory).
    76  *      when the system is low on virtual memory).
    77  * </td>
    77  * </td>
    78  * </tr>
    78  * </tr>
    79  * </table>
    79  * </table>
    80  *
    80  *
    95  *        |----------------------------------------------|
    95  *        |----------------------------------------------|
    96  *                            max
    96  *                            max
    97  * </pre>
    97  * </pre>
    98  *
    98  *
    99  * <h3>MXBean Mapping</h3>
    99  * <h3>MXBean Mapping</h3>
   100  * <tt>MemoryUsage</tt> is mapped to a {@link CompositeData CompositeData}
   100  * {@code MemoryUsage} is mapped to a {@link CompositeData CompositeData}
   101  * with attributes as specified in the {@link #from from} method.
   101  * with attributes as specified in the {@link #from from} method.
   102  *
   102  *
   103  * @author   Mandy Chung
   103  * @author   Mandy Chung
   104  * @since   1.5
   104  * @since   1.5
   105  */
   105  */
   108     private final long used;
   108     private final long used;
   109     private final long committed;
   109     private final long committed;
   110     private final long max;
   110     private final long max;
   111 
   111 
   112     /**
   112     /**
   113      * Constructs a <tt>MemoryUsage</tt> object.
   113      * Constructs a {@code MemoryUsage} object.
   114      *
   114      *
   115      * @param init      the initial amount of memory in bytes that
   115      * @param init      the initial amount of memory in bytes that
   116      *                  the Java virtual machine allocates;
   116      *                  the Java virtual machine allocates;
   117      *                  or <tt>-1</tt> if undefined.
   117      *                  or {@code -1} if undefined.
   118      * @param used      the amount of used memory in bytes.
   118      * @param used      the amount of used memory in bytes.
   119      * @param committed the amount of committed memory in bytes.
   119      * @param committed the amount of committed memory in bytes.
   120      * @param max       the maximum amount of memory in bytes that
   120      * @param max       the maximum amount of memory in bytes that
   121      *                  can be used; or <tt>-1</tt> if undefined.
   121      *                  can be used; or {@code -1} if undefined.
   122      *
   122      *
   123      * @throws IllegalArgumentException if
   123      * @throws IllegalArgumentException if
   124      * <ul>
   124      * <ul>
   125      * <li> the value of <tt>init</tt> or <tt>max</tt> is negative
   125      * <li> the value of {@code init} or {@code max} is negative
   126      *      but not <tt>-1</tt>; or</li>
   126      *      but not {@code -1}; or</li>
   127      * <li> the value of <tt>used</tt> or <tt>committed</tt> is negative;
   127      * <li> the value of {@code used} or {@code committed} is negative;
   128      *      or</li>
   128      *      or</li>
   129      * <li> <tt>used</tt> is greater than the value of <tt>committed</tt>;
   129      * <li> {@code used} is greater than the value of {@code committed};
   130      *      or</li>
   130      *      or</li>
   131      * <li> <tt>committed</tt> is greater than the value of <tt>max</tt>
   131      * <li> {@code committed} is greater than the value of {@code max}
   132      *      <tt>max</tt> if defined.</li>
   132      *      {@code max} if defined.</li>
   133      * </ul>
   133      * </ul>
   134      */
   134      */
   135     public MemoryUsage(long init,
   135     public MemoryUsage(long init,
   136                        long used,
   136                        long used,
   137                        long committed,
   137                        long committed,
   166         this.committed = committed;
   166         this.committed = committed;
   167         this.max = max;
   167         this.max = max;
   168     }
   168     }
   169 
   169 
   170     /**
   170     /**
   171      * Constructs a <tt>MemoryUsage</tt> object from a
   171      * Constructs a {@code MemoryUsage} object from a
   172      * {@link CompositeData CompositeData}.
   172      * {@link CompositeData CompositeData}.
   173      */
   173      */
   174     private MemoryUsage(CompositeData cd) {
   174     private MemoryUsage(CompositeData cd) {
   175         // validate the input composite data
   175         // validate the input composite data
   176         MemoryUsageCompositeData.validateCompositeData(cd);
   176         MemoryUsageCompositeData.validateCompositeData(cd);
   182     }
   182     }
   183 
   183 
   184     /**
   184     /**
   185      * Returns the amount of memory in bytes that the Java virtual machine
   185      * Returns the amount of memory in bytes that the Java virtual machine
   186      * initially requests from the operating system for memory management.
   186      * initially requests from the operating system for memory management.
   187      * This method returns <tt>-1</tt> if the initial memory size is undefined.
   187      * This method returns {@code -1} if the initial memory size is undefined.
   188      *
   188      *
   189      * @return the initial size of memory in bytes;
   189      * @return the initial size of memory in bytes;
   190      * <tt>-1</tt> if undefined.
   190      * {@code -1} if undefined.
   191      */
   191      */
   192     public long getInit() {
   192     public long getInit() {
   193         return init;
   193         return init;
   194     }
   194     }
   195 
   195 
   215         return committed;
   215         return committed;
   216     };
   216     };
   217 
   217 
   218     /**
   218     /**
   219      * Returns the maximum amount of memory in bytes that can be
   219      * Returns the maximum amount of memory in bytes that can be
   220      * used for memory management.  This method returns <tt>-1</tt>
   220      * used for memory management.  This method returns {@code -1}
   221      * if the maximum memory size is undefined.
   221      * if the maximum memory size is undefined.
   222      *
   222      *
   223      * <p> This amount of memory is not guaranteed to be available
   223      * <p> This amount of memory is not guaranteed to be available
   224      * for memory management if it is greater than the amount of
   224      * for memory management if it is greater than the amount of
   225      * committed memory.  The Java virtual machine may fail to allocate
   225      * committed memory.  The Java virtual machine may fail to allocate
   226      * memory even if the amount of used memory does not exceed this
   226      * memory even if the amount of used memory does not exceed this
   227      * maximum size.
   227      * maximum size.
   228      *
   228      *
   229      * @return the maximum amount of memory in bytes;
   229      * @return the maximum amount of memory in bytes;
   230      * <tt>-1</tt> if undefined.
   230      * {@code -1} if undefined.
   231      */
   231      */
   232     public long getMax() {
   232     public long getMax() {
   233         return max;
   233         return max;
   234     };
   234     };
   235 
   235 
   245         buf.append("max = " + max + "(" + (max >> 10) + "K)");
   245         buf.append("max = " + max + "(" + (max >> 10) + "K)");
   246         return buf.toString();
   246         return buf.toString();
   247     }
   247     }
   248 
   248 
   249     /**
   249     /**
   250      * Returns a <tt>MemoryUsage</tt> object represented by the
   250      * Returns a {@code MemoryUsage} object represented by the
   251      * given <tt>CompositeData</tt>. The given <tt>CompositeData</tt>
   251      * given {@code CompositeData}. The given {@code CompositeData}
   252      * must contain the following attributes:
   252      * must contain the following attributes:
   253      *
   253      *
   254      * <blockquote>
   254      * <blockquote>
   255      * <table border summary="The attributes and the types the given CompositeData contains">
   255      * <table border summary="The attributes and the types the given CompositeData contains">
   256      * <tr>
   256      * <tr>
   257      *   <th align=left>Attribute Name</th>
   257      *   <th align=left>Attribute Name</th>
   258      *   <th align=left>Type</th>
   258      *   <th align=left>Type</th>
   259      * </tr>
   259      * </tr>
   260      * <tr>
   260      * <tr>
   261      *   <td>init</td>
   261      *   <td>init</td>
   262      *   <td><tt>java.lang.Long</tt></td>
   262      *   <td>{@code java.lang.Long}</td>
   263      * </tr>
   263      * </tr>
   264      * <tr>
   264      * <tr>
   265      *   <td>used</td>
   265      *   <td>used</td>
   266      *   <td><tt>java.lang.Long</tt></td>
   266      *   <td>{@code java.lang.Long}</td>
   267      * </tr>
   267      * </tr>
   268      * <tr>
   268      * <tr>
   269      *   <td>committed</td>
   269      *   <td>committed</td>
   270      *   <td><tt>java.lang.Long</tt></td>
   270      *   <td>{@code java.lang.Long}</td>
   271      * </tr>
   271      * </tr>
   272      * <tr>
   272      * <tr>
   273      *   <td>max</td>
   273      *   <td>max</td>
   274      *   <td><tt>java.lang.Long</tt></td>
   274      *   <td>{@code java.lang.Long}</td>
   275      * </tr>
   275      * </tr>
   276      * </table>
   276      * </table>
   277      * </blockquote>
   277      * </blockquote>
   278      *
   278      *
   279      * @param cd <tt>CompositeData</tt> representing a <tt>MemoryUsage</tt>
   279      * @param cd {@code CompositeData} representing a {@code MemoryUsage}
   280      *
   280      *
   281      * @throws IllegalArgumentException if <tt>cd</tt> does not
   281      * @throws IllegalArgumentException if {@code cd} does not
   282      *   represent a <tt>MemoryUsage</tt> with the attributes described
   282      *   represent a {@code MemoryUsage} with the attributes described
   283      *   above.
   283      *   above.
   284      *
   284      *
   285      * @return a <tt>MemoryUsage</tt> object represented by <tt>cd</tt>
   285      * @return a {@code MemoryUsage} object represented by {@code cd}
   286      *         if <tt>cd</tt> is not <tt>null</tt>;
   286      *         if {@code cd} is not {@code null};
   287      *         <tt>null</tt> otherwise.
   287      *         {@code null} otherwise.
   288      */
   288      */
   289     public static MemoryUsage from(CompositeData cd) {
   289     public static MemoryUsage from(CompositeData cd) {
   290         if (cd == null) {
   290         if (cd == null) {
   291             return null;
   291             return null;
   292         }
   292         }