Path: blob/master/src/java.management/share/classes/java/lang/management/MemoryUsage.java
41159 views
/*1* Copyright (c) 2003, 2019, 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;28import sun.management.MemoryUsageCompositeData;2930/**31* A {@code MemoryUsage} object represents a snapshot of memory usage.32* Instances of the {@code MemoryUsage} class are usually constructed33* by methods that are used to obtain memory usage34* information about individual memory pool of the Java virtual machine or35* the heap or non-heap memory of the Java virtual machine as a whole.36*37* <p> A {@code MemoryUsage} object contains four values:38* <table class="striped">39* <caption style="display:none">Describes the MemoryUsage object content</caption>40* <thead>41* <tr><th scope="col">Value</th><th scope="col">Description</th></tr>42* </thead>43* <tbody style="text-align:left">44* <tr>45* <th scope="row" style="vertical-align:top"> {@code init} </th>46* <td style="vertical-align:top"> represents the initial amount of memory (in bytes) that47* the Java virtual machine requests from the operating system48* for memory management during startup. The Java virtual machine49* may request additional memory from the operating system and50* may also release memory to the system over time.51* The value of {@code init} may be undefined.52* </td>53* </tr>54* <tr>55* <th scope="row" style="vertical-align:top"> {@code used} </th>56* <td style="vertical-align:top"> represents the amount of memory currently used (in bytes).57* </td>58* </tr>59* <tr>60* <th scope="row" style="vertical-align:top"> {@code committed} </th>61* <td style="vertical-align:top"> represents the amount of memory (in bytes) that is62* guaranteed to be available for use by the Java virtual machine.63* The amount of committed memory may change over time (increase64* or decrease). The Java virtual machine may release memory to65* the system and {@code committed} could be less than {@code init}.66* {@code committed} will always be greater than67* or equal to {@code used}.68* </td>69* </tr>70* <tr>71* <th scope="row" style="vertical-align:top"> {@code max} </th>72* <td style="vertical-align:top"> represents the maximum amount of memory (in bytes)73* that can be used for memory management. Its value may be undefined.74* The maximum amount of memory may change over time if defined.75* The amount of used and committed memory will always be less than76* or equal to {@code max} if {@code max} is defined.77* A memory allocation may fail if it attempts to increase the78* used memory such that {@code used > committed} even79* if {@code used <= max} would still be true (for example,80* when the system is low on virtual memory).81* </td>82* </tr>83* </tbody>84* </table>85*86* Below is a picture showing an example of a memory pool:87*88* <pre>89* +----------------------------------------------+90* +//////////////// | +91* +//////////////// | +92* +----------------------------------------------+93*94* |--------|95* init96* |---------------|97* used98* |---------------------------|99* committed100* |----------------------------------------------|101* max102* </pre>103*104* <h2>MXBean Mapping</h2>105* {@code MemoryUsage} is mapped to a {@link CompositeData CompositeData}106* with attributes as specified in the {@link #from from} method.107*108* @author Mandy Chung109* @since 1.5110*/111public class MemoryUsage {112private final long init;113private final long used;114private final long committed;115private final long max;116117/**118* Constructs a {@code MemoryUsage} object.119*120* @param init the initial amount of memory in bytes that121* the Java virtual machine allocates;122* or {@code -1} if undefined.123* @param used the amount of used memory in bytes.124* @param committed the amount of committed memory in bytes.125* @param max the maximum amount of memory in bytes that126* can be used; or {@code -1} if undefined.127*128* @throws IllegalArgumentException if129* <ul>130* <li> the value of {@code init} or {@code max} is negative131* but not {@code -1}; or</li>132* <li> the value of {@code used} or {@code committed} is negative;133* or</li>134* <li> {@code used} is greater than the value of {@code committed};135* or</li>136* <li> {@code committed} is greater than the value of {@code max}137* {@code max} if defined.</li>138* </ul>139*/140public MemoryUsage(long init,141long used,142long committed,143long max) {144if (init < -1) {145throw new IllegalArgumentException( "init parameter = " +146init + " is negative but not -1.");147}148if (max < -1) {149throw new IllegalArgumentException( "max parameter = " +150max + " is negative but not -1.");151}152if (used < 0) {153throw new IllegalArgumentException( "used parameter = " +154used + " is negative.");155}156if (committed < 0) {157throw new IllegalArgumentException( "committed parameter = " +158committed + " is negative.");159}160if (used > committed) {161throw new IllegalArgumentException( "used = " + used +162" should be <= committed = " + committed);163}164if (max >= 0 && committed > max) {165throw new IllegalArgumentException( "committed = " + committed +166" should be < max = " + max);167}168169this.init = init;170this.used = used;171this.committed = committed;172this.max = max;173}174175/**176* Constructs a {@code MemoryUsage} object from a177* {@link CompositeData CompositeData}.178*/179private MemoryUsage(CompositeData cd) {180// validate the input composite data181MemoryUsageCompositeData.validateCompositeData(cd);182183this.init = MemoryUsageCompositeData.getInit(cd);184this.used = MemoryUsageCompositeData.getUsed(cd);185this.committed = MemoryUsageCompositeData.getCommitted(cd);186this.max = MemoryUsageCompositeData.getMax(cd);187}188189/**190* Returns the amount of memory in bytes that the Java virtual machine191* initially requests from the operating system for memory management.192* This method returns {@code -1} if the initial memory size is undefined.193*194* @return the initial size of memory in bytes;195* {@code -1} if undefined.196*/197public long getInit() {198return init;199}200201/**202* Returns the amount of used memory in bytes.203*204* @return the amount of used memory in bytes.205*206*/207public long getUsed() {208return used;209};210211/**212* Returns the amount of memory in bytes that is committed for213* the Java virtual machine to use. This amount of memory is214* guaranteed for the Java virtual machine to use.215*216* @return the amount of committed memory in bytes.217*218*/219public long getCommitted() {220return committed;221};222223/**224* Returns the maximum amount of memory in bytes that can be225* used for memory management. This method returns {@code -1}226* if the maximum memory size is undefined.227*228* <p> This amount of memory is not guaranteed to be available229* for memory management if it is greater than the amount of230* committed memory. The Java virtual machine may fail to allocate231* memory even if the amount of used memory does not exceed this232* maximum size.233*234* @return the maximum amount of memory in bytes;235* {@code -1} if undefined.236*/237public long getMax() {238return max;239};240241/**242* Returns a descriptive representation of this memory usage.243*/244public String toString() {245StringBuilder buf = new StringBuilder();246buf.append("init = " + init + "(" + (init >> 10) + "K) ");247buf.append("used = " + used + "(" + (used >> 10) + "K) ");248buf.append("committed = " + committed + "(" +249(committed >> 10) + "K) " );250buf.append("max = " + max + "(" + (max >> 10) + "K)");251return buf.toString();252}253254/**255* Returns a {@code MemoryUsage} object represented by the256* given {@code CompositeData}. The given {@code CompositeData}257* must contain the following attributes:258*259* <table class="striped" style="margin-left:2em;">260* <caption style="display:none">The attributes and the types the given CompositeData contains</caption>261* <thead>262* <tr>263* <th scope="col">Attribute Name</th>264* <th scope="col">Type</th>265* </tr>266* </thead>267* <tbody style="text-align:left">268* <tr>269* <th scope="row">init</th>270* <td>{@code java.lang.Long}</td>271* </tr>272* <tr>273* <th scope="row">used</th>274* <td>{@code java.lang.Long}</td>275* </tr>276* <tr>277* <th scope="row">committed</th>278* <td>{@code java.lang.Long}</td>279* </tr>280* <tr>281* <th scope="row">max</th>282* <td>{@code java.lang.Long}</td>283* </tr>284* </tbody>285* </table>286*287* @param cd {@code CompositeData} representing a {@code MemoryUsage}288*289* @throws IllegalArgumentException if {@code cd} does not290* represent a {@code MemoryUsage} with the attributes described291* above.292*293* @return a {@code MemoryUsage} object represented by {@code cd}294* if {@code cd} is not {@code null};295* {@code null} otherwise.296*/297public static MemoryUsage from(CompositeData cd) {298if (cd == null) {299return null;300}301302if (cd instanceof MemoryUsageCompositeData) {303return ((MemoryUsageCompositeData) cd).getMemoryUsage();304} else {305return new MemoryUsage(cd);306}307308}309}310311312