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