src/java.management/share/classes/java/lang/management/MemoryUsage.java
author mgronlun
Mon, 28 Oct 2019 18:43:04 +0100
branchJEP-349-branch
changeset 58823 6a21dba79b81
parent 55120 b0513c833960
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55120
b0513c833960 8220251: fix headings in java.management
jjg
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.management.openmbean.CompositeData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.management.MemoryUsageCompositeData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    32
 * A {@code MemoryUsage} object represents a snapshot of memory usage.
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    33
 * Instances of the {@code MemoryUsage} class are usually constructed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * by methods that are used to obtain memory usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * information about individual memory pool of the Java virtual machine or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * the heap or non-heap memory of the Java virtual machine as a whole.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    38
 * <p> A {@code MemoryUsage} object contains four values:
47028
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
    39
 * <table class="striped">
44858
7183899b064b 8179415: Update java.management and java.management.rmi to be HTML-5 friendly
ksrini
parents: 32034
diff changeset
    40
 * <caption style="display:none">Describes the MemoryUsage object content</caption>
47028
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
    41
 * <thead>
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
    42
 * <tr><th scope="col">Value</th><th scope="col">Description</th></tr>
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
    43
 * </thead>
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
    44
 * <tbody style="text-align:left">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <tr>
47028
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
    46
 * <th scope="row" style="vertical-align:top"> {@code init} </th>
44858
7183899b064b 8179415: Update java.management and java.management.rmi to be HTML-5 friendly
ksrini
parents: 32034
diff changeset
    47
 * <td style="vertical-align:top"> represents the initial amount of memory (in bytes) that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *      the Java virtual machine requests from the operating system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *      for memory management during startup.  The Java virtual machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *      may request additional memory from the operating system and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *      may also release memory to the system over time.
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    52
 *      The value of {@code init} may be undefined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <tr>
47028
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
    56
 * <th scope="row" style="vertical-align:top"> {@code used} </th>
44858
7183899b064b 8179415: Update java.management and java.management.rmi to be HTML-5 friendly
ksrini
parents: 32034
diff changeset
    57
 * <td style="vertical-align:top"> represents the amount of memory currently used (in bytes).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <tr>
47028
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
    61
 * <th scope="row" style="vertical-align:top"> {@code committed} </th>
44858
7183899b064b 8179415: Update java.management and java.management.rmi to be HTML-5 friendly
ksrini
parents: 32034
diff changeset
    62
 * <td style="vertical-align:top"> represents the amount of memory (in bytes) that is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *      guaranteed to be available for use by the Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *      The amount of committed memory may change over time (increase
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *      or decrease).  The Java virtual machine may release memory to
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    66
 *      the system and {@code committed} could be less than {@code init}.
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    67
 *      {@code committed} will always be greater than
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    68
 *      or equal to {@code used}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <tr>
47028
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
    72
 * <th scope="row" style="vertical-align:top"> {@code max} </th>
44858
7183899b064b 8179415: Update java.management and java.management.rmi to be HTML-5 friendly
ksrini
parents: 32034
diff changeset
    73
 * <td style="vertical-align:top"> represents the maximum amount of memory (in bytes)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *      that can be used for memory management. Its value may be undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *      The maximum amount of memory may change over time if defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *      The amount of used and committed memory will always be less than
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    77
 *      or equal to {@code max} if {@code max} is defined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *      A memory allocation may fail if it attempts to increase the
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    79
 *      used memory such that {@code used > committed} even
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
    80
 *      if {@code used <= max} would still be true (for example,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *      when the system is low on virtual memory).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * </tr>
47028
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
    84
 * </tbody>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * Below is a picture showing an example of a memory pool:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18799
diff changeset
    88
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *        +----------------------------------------------+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *        +////////////////           |                  +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *        +////////////////           |                  +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *        +----------------------------------------------+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *        |--------|
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *           init
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *        |---------------|
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *               used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *        |---------------------------|
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *                  committed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *        |----------------------------------------------|
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *                            max
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
55120
b0513c833960 8220251: fix headings in java.management
jjg
parents: 47216
diff changeset
   105
 * <h2>MXBean Mapping</h2>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   106
 * {@code MemoryUsage} is mapped to a {@link CompositeData CompositeData}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * with attributes as specified in the {@link #from from} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * @author   Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
public class MemoryUsage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private final long init;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private final long used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private final long committed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private final long max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   119
     * Constructs a {@code MemoryUsage} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param init      the initial amount of memory in bytes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *                  the Java virtual machine allocates;
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   123
     *                  or {@code -1} if undefined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param used      the amount of used memory in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param committed the amount of committed memory in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param max       the maximum amount of memory in bytes that
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   127
     *                  can be used; or {@code -1} if undefined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @throws IllegalArgumentException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <ul>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   131
     * <li> the value of {@code init} or {@code max} is negative
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   132
     *      but not {@code -1}; or</li>
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   133
     * <li> the value of {@code used} or {@code committed} is negative;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *      or</li>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   135
     * <li> {@code used} is greater than the value of {@code committed};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *      or</li>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   137
     * <li> {@code committed} is greater than the value of {@code max}
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   138
     *      {@code max} if defined.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public MemoryUsage(long init,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                       long used,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                       long committed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                       long max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (init < -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new IllegalArgumentException( "init parameter = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                init + " is negative but not -1.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (max < -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new IllegalArgumentException( "max parameter = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                max + " is negative but not -1.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (used < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new IllegalArgumentException( "used parameter = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                used + " is negative.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (committed < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new IllegalArgumentException( "committed parameter = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                committed + " is negative.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (used > committed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throw new IllegalArgumentException( "used = " + used +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                " should be <= committed = " + committed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (max >= 0 && committed > max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new IllegalArgumentException( "committed = " + committed +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                " should be < max = " + max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        this.init = init;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        this.used = used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        this.committed = committed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        this.max = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   177
     * Constructs a {@code MemoryUsage} object from a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * {@link CompositeData CompositeData}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private MemoryUsage(CompositeData cd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // validate the input composite data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        MemoryUsageCompositeData.validateCompositeData(cd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        this.init = MemoryUsageCompositeData.getInit(cd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        this.used = MemoryUsageCompositeData.getUsed(cd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        this.committed = MemoryUsageCompositeData.getCommitted(cd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        this.max = MemoryUsageCompositeData.getMax(cd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Returns the amount of memory in bytes that the Java virtual machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * initially requests from the operating system for memory management.
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   193
     * This method returns {@code -1} if the initial memory size is undefined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @return the initial size of memory in bytes;
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   196
     * {@code -1} if undefined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public long getInit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return init;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Returns the amount of used memory in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @return the amount of used memory in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public long getUsed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Returns the amount of memory in bytes that is committed for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * the Java virtual machine to use.  This amount of memory is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * guaranteed for the Java virtual machine to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @return the amount of committed memory in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public long getCommitted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return committed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Returns the maximum amount of memory in bytes that can be
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   226
     * used for memory management.  This method returns {@code -1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * if the maximum memory size is undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <p> This amount of memory is not guaranteed to be available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * for memory management if it is greater than the amount of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * committed memory.  The Java virtual machine may fail to allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * memory even if the amount of used memory does not exceed this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * maximum size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @return the maximum amount of memory in bytes;
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   236
     * {@code -1} if undefined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public long getMax() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Returns a descriptive representation of this memory usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public String toString() {
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 21334
diff changeset
   246
        StringBuilder buf = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        buf.append("init = " + init + "(" + (init >> 10) + "K) ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        buf.append("used = " + used + "(" + (used >> 10) + "K) ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        buf.append("committed = " + committed + "(" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                   (committed >> 10) + "K) " );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        buf.append("max = " + max + "(" + (max >> 10) + "K)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   256
     * Returns a {@code MemoryUsage} object represented by the
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   257
     * given {@code CompositeData}. The given {@code CompositeData}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * must contain the following attributes:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18799
diff changeset
   259
     *
47028
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
   260
     * <table class="striped" style="margin-left:2em;">
44858
7183899b064b 8179415: Update java.management and java.management.rmi to be HTML-5 friendly
ksrini
parents: 32034
diff changeset
   261
     * <caption style="display:none">The attributes and the types the given CompositeData contains</caption>
47028
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
   262
     * <thead>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * <tr>
47028
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
   264
     *   <th scope="col">Attribute Name</th>
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
   265
     *   <th scope="col">Type</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * </tr>
47028
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
   267
     * </thead>
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
   268
     * <tbody style="text-align:left">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <tr>
47028
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
   270
     *   <th scope="row">init</th>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   271
     *   <td>{@code java.lang.Long}</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <tr>
47028
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
   274
     *   <th scope="row">used</th>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   275
     *   <td>{@code java.lang.Long}</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * <tr>
47028
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
   278
     *   <th scope="row">committed</th>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   279
     *   <td>{@code java.lang.Long}</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * <tr>
47028
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
   282
     *   <th scope="row">max</th>
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   283
     *   <td>{@code java.lang.Long}</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * </tr>
47028
6df65183aa1f 8186932: Fix accessibility issues in the java.management module
jjg
parents: 44858
diff changeset
   285
     * </tbody>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   288
     * @param cd {@code CompositeData} representing a {@code MemoryUsage}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   290
     * @throws IllegalArgumentException if {@code cd} does not
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   291
     *   represent a {@code MemoryUsage} with the attributes described
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *   above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   294
     * @return a {@code MemoryUsage} object represented by {@code cd}
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   295
     *         if {@code cd} is not {@code null};
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   296
     *         {@code null} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public static MemoryUsage from(CompositeData cd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (cd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (cd instanceof MemoryUsageCompositeData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return ((MemoryUsageCompositeData) cd).getMemoryUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            return new MemoryUsage(cd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
}