author | jgish |
Thu, 18 Apr 2013 16:33:11 -0400 | |
changeset 17159 | bb566a21b661 |
parent 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
12866
diff
changeset
|
2 |
* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
|
5 |
* modification, are permitted provided that the following conditions |
|
6 |
* are met: |
|
7 |
* |
|
8 |
* - Redistributions of source code must retain the above copyright |
|
9 |
* notice, this list of conditions and the following disclaimer. |
|
10 |
* |
|
11 |
* - Redistributions in binary form must reproduce the above copyright |
|
12 |
* notice, this list of conditions and the following disclaimer in the |
|
13 |
* documentation and/or other materials provided with the distribution. |
|
14 |
* |
|
5506 | 15 |
* - Neither the name of Oracle nor the names of its |
2 | 16 |
* contributors may be used to endorse or promote products derived |
17 |
* from this software without specific prior written permission. |
|
18 |
* |
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
30 |
*/ |
|
31 |
||
32 |
/* |
|
10292
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
33 |
* This source code is provided to illustrate the usage of a given feature |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
34 |
* or technique and has been deliberately simplified. Additional steps |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
35 |
* required for a production-quality application, such as security checks, |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
36 |
* input validation and proper error handling, might not be present in |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
37 |
* this sample code. |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
38 |
*/ |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
39 |
|
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
40 |
|
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
9520
diff
changeset
|
41 |
/* |
2 | 42 |
*/ |
43 |
||
44 |
import java.awt.*; |
|
45 |
import java.awt.event.*; |
|
46 |
import java.awt.image.BufferedImage; |
|
47 |
import java.awt.geom.Line2D; |
|
48 |
import java.awt.geom.Rectangle2D; |
|
49 |
import java.util.Date; |
|
50 |
import javax.swing.*; |
|
51 |
import javax.swing.border.EtchedBorder; |
|
52 |
import javax.swing.border.TitledBorder; |
|
53 |
import java.lang.management.*; |
|
54 |
/** |
|
55 |
* Demo code which plots the memory usage by all memory pools. |
|
56 |
* The memory usage is sampled at some time interval using |
|
57 |
* java.lang.management API. This demo code is modified based |
|
58 |
* java2d MemoryMonitor demo. |
|
59 |
*/ |
|
60 |
public class MemoryMonitor extends JPanel { |
|
61 |
||
9520 | 62 |
private static final long serialVersionUID = -3463003810776195761L; |
2 | 63 |
static JCheckBox dateStampCB = new JCheckBox("Output Date Stamp"); |
64 |
public Surface surf; |
|
65 |
JPanel controls; |
|
66 |
boolean doControls; |
|
67 |
JTextField tf; |
|
68 |
// Get memory pools. |
|
69 |
static java.util.List<MemoryPoolMXBean> mpools = |
|
70 |
ManagementFactory.getMemoryPoolMXBeans(); |
|
71 |
// Total number of memory pools. |
|
72 |
static int numPools = mpools.size(); |
|
73 |
||
74 |
public MemoryMonitor() { |
|
75 |
setLayout(new BorderLayout()); |
|
76 |
setBorder(new TitledBorder(new EtchedBorder(), "Memory Monitor")); |
|
77 |
add(surf = new Surface()); |
|
78 |
controls = new JPanel(); |
|
79 |
controls.setPreferredSize(new Dimension(135,80)); |
|
80 |
Font font = new Font("serif", Font.PLAIN, 10); |
|
81 |
JLabel label = new JLabel("Sample Rate"); |
|
82 |
label.setFont(font); |
|
83 |
label.setForeground(Color.red); |
|
84 |
controls.add(label); |
|
85 |
tf = new JTextField("1000"); |
|
86 |
tf.setPreferredSize(new Dimension(45,20)); |
|
87 |
controls.add(tf); |
|
88 |
controls.add(label = new JLabel("ms")); |
|
89 |
label.setFont(font); |
|
90 |
label.setForeground(Color.red); |
|
91 |
controls.add(dateStampCB); |
|
92 |
dateStampCB.setFont(font); |
|
93 |
addMouseListener(new MouseAdapter() { |
|
9520 | 94 |
@Override |
2 | 95 |
public void mouseClicked(MouseEvent e) { |
96 |
removeAll(); |
|
97 |
if ((doControls = !doControls)) { |
|
98 |
surf.stop(); |
|
99 |
add(controls); |
|
100 |
} else { |
|
101 |
try { |
|
102 |
surf.sleepAmount = Long.parseLong(tf.getText().trim()); |
|
103 |
} catch (Exception ex) {} |
|
104 |
surf.start(); |
|
105 |
add(surf); |
|
106 |
} |
|
107 |
validate(); |
|
108 |
repaint(); |
|
109 |
} |
|
110 |
}); |
|
111 |
} |
|
112 |
||
113 |
||
114 |
public class Surface extends JPanel implements Runnable { |
|
115 |
||
116 |
public Thread thread; |
|
117 |
public long sleepAmount = 1000; |
|
118 |
public int usageHistCount = 20000; |
|
119 |
private int w, h; |
|
120 |
private BufferedImage bimg; |
|
121 |
private Graphics2D big; |
|
122 |
private Font font = new Font("Times New Roman", Font.PLAIN, 11); |
|
123 |
private int columnInc; |
|
124 |
private float usedMem[][]; |
|
12866
48fd3e04f1f6
7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents:
11841
diff
changeset
|
125 |
private float usedMemMax[]; // Used when max pool size is undefined |
2 | 126 |
private int ptNum[]; |
127 |
private int ascent, descent; |
|
128 |
private Rectangle graphOutlineRect = new Rectangle(); |
|
129 |
private Rectangle2D mfRect = new Rectangle2D.Float(); |
|
130 |
private Rectangle2D muRect = new Rectangle2D.Float(); |
|
131 |
private Line2D graphLine = new Line2D.Float(); |
|
132 |
private Color graphColor = new Color(46, 139, 87); |
|
133 |
private Color mfColor = new Color(0, 100, 0); |
|
134 |
private String usedStr; |
|
135 |
||
136 |
||
137 |
public Surface() { |
|
138 |
setBackground(Color.black); |
|
139 |
addMouseListener(new MouseAdapter() { |
|
9520 | 140 |
@Override |
2 | 141 |
public void mouseClicked(MouseEvent e) { |
142 |
if (thread == null) start(); else stop(); |
|
143 |
} |
|
144 |
}); |
|
145 |
usedMem = new float[numPools][]; |
|
12866
48fd3e04f1f6
7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents:
11841
diff
changeset
|
146 |
usedMemMax = new float[numPools]; |
48fd3e04f1f6
7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents:
11841
diff
changeset
|
147 |
for (int i = 0; i < numPools; i++) { |
48fd3e04f1f6
7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents:
11841
diff
changeset
|
148 |
usedMemMax[i] = 1024f * 1024f ; |
48fd3e04f1f6
7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents:
11841
diff
changeset
|
149 |
} |
2 | 150 |
ptNum = new int[numPools]; |
151 |
} |
|
152 |
||
9520 | 153 |
@Override |
2 | 154 |
public Dimension getMinimumSize() { |
155 |
return getPreferredSize(); |
|
156 |
} |
|
157 |
||
9520 | 158 |
@Override |
2 | 159 |
public Dimension getMaximumSize() { |
160 |
return getPreferredSize(); |
|
161 |
} |
|
162 |
||
9520 | 163 |
@Override |
2 | 164 |
public Dimension getPreferredSize() { |
165 |
return new Dimension(135,80); |
|
166 |
} |
|
167 |
||
168 |
||
9520 | 169 |
@Override |
2 | 170 |
public void paint(Graphics g) { |
171 |
||
172 |
if (big == null) { |
|
173 |
return; |
|
174 |
} |
|
175 |
||
176 |
big.setBackground(getBackground()); |
|
177 |
big.clearRect(0,0,w,h); |
|
178 |
||
179 |
||
180 |
h = h / ((numPools + numPools%2) / 2); |
|
181 |
w = w / 2; |
|
182 |
||
183 |
int k=0; // index of memory pool. |
|
184 |
for (int i=0; i < 2;i++) { |
|
185 |
for (int j=0; j < (numPools + numPools%2)/ 2; j++) { |
|
186 |
plotMemoryUsage(w*i,h*j,w,h,k); |
|
187 |
if (++k >= numPools) { |
|
188 |
i = 3; |
|
189 |
j = (numPools + numPools%2)/ 2; |
|
190 |
break; |
|
191 |
} |
|
192 |
} |
|
193 |
} |
|
194 |
g.drawImage(bimg, 0, 0, this); |
|
195 |
} |
|
196 |
||
197 |
public void plotMemoryUsage(int x1, int y1, int x2, int y2, int npool) { |
|
198 |
||
199 |
MemoryPoolMXBean mp = mpools.get(npool); |
|
200 |
float usedMemory = mp.getUsage().getUsed(); |
|
201 |
float totalMemory = mp.getUsage().getMax(); |
|
12866
48fd3e04f1f6
7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents:
11841
diff
changeset
|
202 |
if (totalMemory < 0) { // Max is undefined for this pool |
48fd3e04f1f6
7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents:
11841
diff
changeset
|
203 |
if (usedMemory > usedMemMax[npool]) { |
48fd3e04f1f6
7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents:
11841
diff
changeset
|
204 |
usedMemMax[npool] = usedMemory; |
48fd3e04f1f6
7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents:
11841
diff
changeset
|
205 |
} |
48fd3e04f1f6
7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents:
11841
diff
changeset
|
206 |
totalMemory = usedMemMax[npool]; |
48fd3e04f1f6
7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents:
11841
diff
changeset
|
207 |
} |
2 | 208 |
|
209 |
// .. Draw allocated and used strings .. |
|
210 |
big.setColor(Color.green); |
|
211 |
||
212 |
// Print Max memory allocated for this memory pool. |
|
213 |
big.drawString(String.valueOf((int)totalMemory/1024) + "K Max ", x1+4.0f, (float) y1 + ascent+0.5f); |
|
214 |
big.setColor(Color.yellow); |
|
215 |
||
216 |
// Print the memory pool name. |
|
217 |
big.drawString(mp.getName(), x1+x2/2, (float) y1 + ascent+0.5f); |
|
218 |
||
219 |
// Print the memory used by this memory pool. |
|
220 |
usedStr = String.valueOf((int)usedMemory/1024) |
|
221 |
+ "K used"; |
|
222 |
big.setColor(Color.green); |
|
223 |
big.drawString(usedStr, x1+4, y1+y2-descent); |
|
224 |
||
225 |
// Calculate remaining size |
|
226 |
float ssH = ascent + descent; |
|
11841
38a39c748880
7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents:
10292
diff
changeset
|
227 |
float remainingHeight = y2 - (ssH*2) - 0.5f; |
2 | 228 |
float blockHeight = remainingHeight/10; |
229 |
float blockWidth = 20.0f; |
|
11841
38a39c748880
7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents:
10292
diff
changeset
|
230 |
float remainingWidth = x2 - blockWidth - 10; |
2 | 231 |
|
232 |
// .. Memory Free .. |
|
233 |
big.setColor(mfColor); |
|
234 |
int MemUsage = (int) (((totalMemory - usedMemory) / totalMemory) * 10); |
|
235 |
int i = 0; |
|
236 |
for ( ; i < MemUsage ; i++) { |
|
237 |
mfRect.setRect(x1+5,(float) y1+ssH+i*blockHeight, |
|
11841
38a39c748880
7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents:
10292
diff
changeset
|
238 |
blockWidth, blockHeight-1); |
2 | 239 |
big.fill(mfRect); |
240 |
} |
|
241 |
||
242 |
// .. Memory Used .. |
|
243 |
big.setColor(Color.green); |
|
244 |
for ( ; i < 10; i++) { |
|
245 |
muRect.setRect(x1+5,(float) y1 + ssH+i*blockHeight, |
|
11841
38a39c748880
7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents:
10292
diff
changeset
|
246 |
blockWidth, blockHeight-1); |
2 | 247 |
big.fill(muRect); |
248 |
} |
|
249 |
||
250 |
// .. Draw History Graph .. |
|
251 |
if (remainingWidth <= 30) remainingWidth = (float)30; |
|
11841
38a39c748880
7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents:
10292
diff
changeset
|
252 |
if (remainingHeight <= ssH) remainingHeight = ssH; |
2 | 253 |
big.setColor(graphColor); |
254 |
int graphX = x1+30; |
|
255 |
int graphY = y1 + (int) ssH; |
|
256 |
int graphW = (int) remainingWidth; |
|
257 |
int graphH = (int) remainingHeight; |
|
258 |
||
259 |
graphOutlineRect.setRect(graphX, graphY, graphW, graphH); |
|
260 |
big.draw(graphOutlineRect); |
|
261 |
||
262 |
int graphRow = graphH/10; |
|
263 |
||
264 |
// .. Draw row .. |
|
265 |
for (int j = graphY; j <= graphH+graphY; j += graphRow) { |
|
266 |
graphLine.setLine(graphX,j,graphX+graphW,j); |
|
267 |
big.draw(graphLine); |
|
268 |
} |
|
269 |
||
270 |
// .. Draw animated column movement .. |
|
271 |
int graphColumn = graphW/15; |
|
272 |
||
273 |
if (columnInc == 0) { |
|
274 |
columnInc = graphColumn; |
|
275 |
} |
|
276 |
||
277 |
for (int j = graphX+columnInc; j < graphW+graphX; j+=graphColumn) { |
|
278 |
graphLine.setLine(j,graphY,j,graphY+graphH); |
|
279 |
big.draw(graphLine); |
|
280 |
} |
|
281 |
||
282 |
--columnInc; |
|
283 |
||
284 |
// Plot memory usage by this memory pool. |
|
285 |
if (usedMem[npool] == null) { |
|
286 |
usedMem[npool] = new float[usageHistCount]; |
|
287 |
ptNum[npool] = 0; |
|
288 |
} |
|
289 |
||
290 |
// save memory usage history. |
|
291 |
usedMem[npool][ptNum[npool]] = usedMemory; |
|
292 |
||
293 |
big.setColor(Color.yellow); |
|
294 |
||
295 |
int w1; // width of memory usage history. |
|
296 |
if (ptNum[npool] > graphW) { |
|
297 |
w1 = graphW; |
|
298 |
} else { |
|
299 |
w1 = ptNum[npool]; |
|
300 |
} |
|
301 |
||
302 |
||
303 |
for (int j=graphX+graphW-w1, k=ptNum[npool]-w1; k < ptNum[npool]; |
|
304 |
k++, j++) { |
|
305 |
if (k != 0) { |
|
306 |
if (usedMem[npool][k] != usedMem[npool][k-1]) { |
|
307 |
int h1 = (int)(graphY + graphH * ((totalMemory -usedMem[npool][k-1])/totalMemory)); |
|
308 |
int h2 = (int)(graphY + graphH * ((totalMemory -usedMem[npool][k])/totalMemory)); |
|
309 |
big.drawLine(j-1, h1, j, h2); |
|
310 |
} else { |
|
311 |
int h1 = (int)(graphY + graphH * ((totalMemory -usedMem[npool][k])/totalMemory)); |
|
312 |
big.fillRect(j, h1, 1, 1); |
|
313 |
} |
|
314 |
} |
|
315 |
} |
|
316 |
if (ptNum[npool]+2 == usedMem[npool].length) { |
|
317 |
// throw out oldest point |
|
318 |
for (int j = 1;j < ptNum[npool]; j++) { |
|
319 |
usedMem[npool][j-1] = usedMem[npool][j]; |
|
320 |
} |
|
321 |
--ptNum[npool]; |
|
322 |
} else { |
|
323 |
ptNum[npool]++; |
|
324 |
} |
|
325 |
} |
|
326 |
||
327 |
||
328 |
public void start() { |
|
329 |
thread = new Thread(this); |
|
330 |
thread.setPriority(Thread.MIN_PRIORITY); |
|
331 |
thread.setName("MemoryMonitor"); |
|
332 |
thread.start(); |
|
333 |
} |
|
334 |
||
335 |
||
336 |
public synchronized void stop() { |
|
337 |
thread = null; |
|
338 |
notify(); |
|
339 |
} |
|
340 |
||
9520 | 341 |
@Override |
2 | 342 |
public void run() { |
343 |
||
344 |
Thread me = Thread.currentThread(); |
|
345 |
||
346 |
while (thread == me && !isShowing() || getSize().width == 0) { |
|
347 |
try { |
|
9520 | 348 |
Thread.sleep(500); |
2 | 349 |
} catch (InterruptedException e) { return; } |
350 |
} |
|
351 |
||
352 |
while (thread == me && isShowing()) { |
|
353 |
Dimension d = getSize(); |
|
354 |
if (d.width != w || d.height != h) { |
|
355 |
w = d.width; |
|
356 |
h = d.height; |
|
357 |
bimg = (BufferedImage) createImage(w, h); |
|
358 |
big = bimg.createGraphics(); |
|
359 |
big.setFont(font); |
|
360 |
FontMetrics fm = big.getFontMetrics(font); |
|
11841
38a39c748880
7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents:
10292
diff
changeset
|
361 |
ascent = fm.getAscent(); |
38a39c748880
7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents:
10292
diff
changeset
|
362 |
descent = fm.getDescent(); |
2 | 363 |
} |
364 |
repaint(); |
|
365 |
try { |
|
9520 | 366 |
Thread.sleep(sleepAmount); |
2 | 367 |
} catch (InterruptedException e) { break; } |
368 |
if (MemoryMonitor.dateStampCB.isSelected()) { |
|
369 |
System.out.println(new Date().toString() + " " + usedStr); |
|
370 |
} |
|
371 |
} |
|
372 |
thread = null; |
|
373 |
} |
|
374 |
} |
|
375 |
||
376 |
||
377 |
// Test thread to consume memory |
|
378 |
static class Memeater extends ClassLoader implements Runnable { |
|
379 |
Object y[]; |
|
380 |
public Memeater() {} |
|
9520 | 381 |
@Override |
2 | 382 |
public void run() { |
383 |
y = new Object[10000000]; |
|
384 |
int k =0; |
|
385 |
while(true) { |
|
386 |
if (k == 5000000) k=0; |
|
387 |
y[k++] = new Object(); |
|
388 |
try { |
|
389 |
Thread.sleep(20); |
|
390 |
} catch (Exception x){} |
|
391 |
||
392 |
// to consume perm gen storage |
|
393 |
try { |
|
394 |
// the classes are small so we load 10 at a time |
|
395 |
for (int i=0; i<10; i++) { |
|
396 |
loadNext(); |
|
397 |
} |
|
398 |
} catch (ClassNotFoundException x) { |
|
399 |
// ignore exception |
|
400 |
} |
|
401 |
||
402 |
} |
|
403 |
||
404 |
} |
|
405 |
||
9520 | 406 |
Class<?> loadNext() throws ClassNotFoundException { |
2 | 407 |
|
408 |
// public class TestNNNNNN extends java.lang.Object{ |
|
409 |
// public TestNNNNNN(); |
|
410 |
// Code: |
|
411 |
// 0: aload_0 |
|
412 |
// 1: invokespecial #1; //Method java/lang/Object."<init>":()V |
|
413 |
// 4: return |
|
414 |
// } |
|
415 |
||
416 |
int begin[] = { |
|
417 |
0xca, 0xfe, 0xba, 0xbe, 0x00, 0x00, 0x00, 0x30, |
|
418 |
0x00, 0x0a, 0x0a, 0x00, 0x03, 0x00, 0x07, 0x07, |
|
419 |
0x00, 0x08, 0x07, 0x00, 0x09, 0x01, 0x00, 0x06, |
|
420 |
0x3c, 0x69, 0x6e, 0x69, 0x74, 0x3e, 0x01, 0x00, |
|
421 |
0x03, 0x28, 0x29, 0x56, 0x01, 0x00, 0x04, 0x43, |
|
422 |
0x6f, 0x64, 0x65, 0x0c, 0x00, 0x04, 0x00, 0x05, |
|
423 |
0x01, 0x00, 0x0a, 0x54, 0x65, 0x73, 0x74 }; |
|
424 |
||
425 |
int end [] = { |
|
426 |
0x01, 0x00, 0x10, |
|
427 |
0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e, |
|
428 |
0x67, 0x2f, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, |
|
429 |
0x00, 0x21, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, |
|
430 |
0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x04, |
|
431 |
0x00, 0x05, 0x00, 0x01, 0x00, 0x06, 0x00, 0x00, |
|
432 |
0x00, 0x11, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, |
|
433 |
0x00, 0x05, 0x2a, 0xb7, 0x00, 0x01, 0xb1, 0x00, |
|
434 |
0x00, 0x00, 0x00, 0x00, 0x00 }; |
|
435 |
||
436 |
||
437 |
// TestNNNNNN |
|
438 |
||
439 |
String name = "Test" + Integer.toString(count++); |
|
440 |
||
441 |
byte value[]; |
|
442 |
try { |
|
443 |
value = name.substring(4).getBytes("UTF-8"); |
|
444 |
} catch (java.io.UnsupportedEncodingException x) { |
|
445 |
throw new Error(); |
|
446 |
} |
|
447 |
||
448 |
// construct class file |
|
449 |
||
450 |
int len = begin.length + value.length + end.length; |
|
451 |
byte b[] = new byte[len]; |
|
9520 | 452 |
int pos=0; |
453 |
for (int i: begin) { |
|
454 |
b[pos++] = (byte) i; |
|
2 | 455 |
} |
9520 | 456 |
for (byte v: value) { |
457 |
b[pos++] = v; |
|
2 | 458 |
} |
9520 | 459 |
for (int e: end) { |
460 |
b[pos++] = (byte) e; |
|
2 | 461 |
} |
462 |
||
463 |
return defineClass(name, b, 0, b.length); |
|
464 |
||
465 |
} |
|
466 |
static int count = 100000; |
|
467 |
||
468 |
} |
|
469 |
||
470 |
public static void main(String s[]) { |
|
471 |
final MemoryMonitor demo = new MemoryMonitor(); |
|
472 |
WindowListener l = new WindowAdapter() { |
|
9520 | 473 |
@Override |
2 | 474 |
public void windowClosing(WindowEvent e) {System.exit(0);} |
9520 | 475 |
@Override |
2 | 476 |
public void windowDeiconified(WindowEvent e) { demo.surf.start(); } |
9520 | 477 |
@Override |
2 | 478 |
public void windowIconified(WindowEvent e) { demo.surf.stop(); } |
479 |
}; |
|
480 |
JFrame f = new JFrame("MemoryMonitor"); |
|
481 |
f.addWindowListener(l); |
|
482 |
f.getContentPane().add("Center", demo); |
|
483 |
f.pack(); |
|
484 |
f.setSize(new Dimension(400,500)); |
|
485 |
f.setVisible(true); |
|
486 |
demo.surf.start(); |
|
487 |
Thread thr = new Thread(new Memeater()); |
|
488 |
thr.start(); |
|
489 |
} |
|
490 |
||
491 |
} |