KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MemoryMonitor


1 /*
2  * @(#)MemoryMonitor.java 1.3 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)MemoryMonitor.java 1.3 05/11/17
39  */

40
41 import java.awt.*;
42 import java.awt.event.*;
43 import java.awt.image.BufferedImage JavaDoc;
44 import java.awt.geom.Line2D JavaDoc;
45 import java.awt.geom.Rectangle2D JavaDoc;
46 import java.util.Date JavaDoc;
47 import javax.swing.*;
48 import javax.swing.border.EtchedBorder JavaDoc;
49 import javax.swing.border.TitledBorder JavaDoc;
50 import java.lang.management.*;
51 import java.util.*;
52
53
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
62     static JCheckBox dateStampCB = new JCheckBox("Output Date Stamp");
63     public Surface surf;
64     JPanel controls;
65     boolean doControls;
66     JTextField tf;
67     // Get memory pools.
68
static java.util.List JavaDoc<MemoryPoolMXBean> mpools =
69         ManagementFactory.getMemoryPoolMXBeans();
70     // Total number of memory pools.
71
static int numPools = mpools.size();
72
73     public MemoryMonitor() {
74         setLayout(new BorderLayout());
75         setBorder(new TitledBorder JavaDoc(new EtchedBorder JavaDoc(), "Memory Monitor"));
76         add(surf = new Surface());
77         controls = new JPanel();
78         controls.setPreferredSize(new Dimension(135,80));
79         Font font = new Font("serif", Font.PLAIN, 10);
80         JLabel label = new JLabel("Sample Rate");
81         label.setFont(font);
82         label.setForeground(Color.red);
83         controls.add(label);
84         tf = new JTextField("1000");
85         tf.setPreferredSize(new Dimension(45,20));
86         controls.add(tf);
87         controls.add(label = new JLabel("ms"));
88         label.setFont(font);
89         label.setForeground(Color.red);
90         controls.add(dateStampCB);
91         dateStampCB.setFont(font);
92         addMouseListener(new MouseAdapter() {
93             public void mouseClicked(MouseEvent e) {
94                removeAll();
95                if ((doControls = !doControls)) {
96                    surf.stop();
97                    add(controls);
98                } else {
99                    try {
100                        surf.sleepAmount = Long.parseLong(tf.getText().trim());
101                    } catch (Exception JavaDoc ex) {}
102                    surf.start();
103                    add(surf);
104                }
105                validate();
106                repaint();
107             }
108         });
109     }
110
111
112     public class Surface extends JPanel implements Runnable JavaDoc {
113
114         public Thread JavaDoc thread;
115         public long sleepAmount = 1000;
116     public int usageHistCount = 20000;
117         private int w, h;
118         private BufferedImage JavaDoc bimg;
119         private Graphics2D big;
120         private Font font = new Font("Times New Roman", Font.PLAIN, 11);
121         private int columnInc;
122         private float usedMem[][];
123         private int ptNum[];
124         private int ascent, descent;
125         private Rectangle graphOutlineRect = new Rectangle();
126         private Rectangle2D JavaDoc mfRect = new Rectangle2D.Float JavaDoc();
127         private Rectangle2D JavaDoc muRect = new Rectangle2D.Float JavaDoc();
128         private Line2D JavaDoc graphLine = new Line2D.Float JavaDoc();
129         private Color graphColor = new Color(46, 139, 87);
130         private Color mfColor = new Color(0, 100, 0);
131         private String JavaDoc usedStr;
132       
133
134         public Surface() {
135             setBackground(Color.black);
136             addMouseListener(new MouseAdapter() {
137                 public void mouseClicked(MouseEvent e) {
138                     if (thread == null) start(); else stop();
139                 }
140             });
141         int i = 0;
142         usedMem = new float[numPools][];
143         ptNum = new int[numPools];
144         }
145
146         public Dimension getMinimumSize() {
147             return getPreferredSize();
148         }
149
150         public Dimension getMaximumSize() {
151             return getPreferredSize();
152         }
153
154         public Dimension getPreferredSize() {
155             return new Dimension(135,80);
156         }
157
158             
159         public void paint(Graphics g) {
160
161             if (big == null) {
162                 return;
163             }
164
165             big.setBackground(getBackground());
166             big.clearRect(0,0,w,h);
167
168
169         h = h / ((numPools + numPools%2) / 2);
170         w = w / 2;
171
172         int k=0; // index of memory pool.
173
for (int i=0; i < 2;i++) {
174            for (int j=0; j < (numPools + numPools%2)/ 2; j++) {
175              plotMemoryUsage(w*i,h*j,w,h,k);
176          if (++k >= numPools) {
177             i = 3;
178             j = (numPools + numPools%2)/ 2;
179             break;
180          }
181            }
182         }
183             g.drawImage(bimg, 0, 0, this);
184         }
185
186     public void plotMemoryUsage(int x1, int y1, int x2, int y2, int npool) {
187
188         MemoryPoolMXBean mp = mpools.get(npool);
189         float usedMemory = mp.getUsage().getUsed();
190             float totalMemory = mp.getUsage().getMax();
191
192             // .. Draw allocated and used strings ..
193
big.setColor(Color.green);
194
195         // Print Max memory allocated for this memory pool.
196
big.drawString(String.valueOf((int)totalMemory/1024) + "K Max ", x1+4.0f, (float) y1 + ascent+0.5f);
197             big.setColor(Color.yellow);
198
199         // Print the memory pool name.
200
big.drawString(mp.getName(), x1+x2/2, (float) y1 + ascent+0.5f);
201
202         // Print the memory used by this memory pool.
203
usedStr = String.valueOf((int)usedMemory/1024)
204                 + "K used";
205             big.setColor(Color.green);
206             big.drawString(usedStr, x1+4, y1+y2-descent);
207
208             // Calculate remaining size
209
float ssH = ascent + descent;
210             float remainingHeight = (float) (y2 - (ssH*2) - 0.5f);
211             float blockHeight = remainingHeight/10;
212             float blockWidth = 20.0f;
213             float remainingWidth = (float) (x2 - blockWidth - 10);
214
215             // .. Memory Free ..
216
big.setColor(mfColor);
217             int MemUsage = (int) (((totalMemory - usedMemory) / totalMemory) * 10);
218             int i = 0;
219             for ( ; i < MemUsage ; i++) {
220                 mfRect.setRect(x1+5,(float) y1+ssH+i*blockHeight,
221                                 blockWidth,(float) blockHeight-1);
222                 big.fill(mfRect);
223             }
224
225             // .. Memory Used ..
226
big.setColor(Color.green);
227             for ( ; i < 10; i++) {
228                 muRect.setRect(x1+5,(float) y1 + ssH+i*blockHeight,
229                                 blockWidth,(float) blockHeight-1);
230                 big.fill(muRect);
231             }
232
233             // .. Draw History Graph ..
234
if (remainingWidth <= 30) remainingWidth = (float)30;
235         if (remainingHeight <= ssH) remainingHeight = (float)ssH;
236             big.setColor(graphColor);
237             int graphX = x1+30;
238             int graphY = y1 + (int) ssH;
239             int graphW = (int) remainingWidth;
240             int graphH = (int) remainingHeight;
241
242             graphOutlineRect.setRect(graphX, graphY, graphW, graphH);
243             big.draw(graphOutlineRect);
244
245             int graphRow = graphH/10;
246
247             // .. Draw row ..
248
for (int j = graphY; j <= graphH+graphY; j += graphRow) {
249                 graphLine.setLine(graphX,j,graphX+graphW,j);
250                 big.draw(graphLine);
251             }
252         
253             // .. Draw animated column movement ..
254
int graphColumn = graphW/15;
255
256             if (columnInc == 0) {
257                 columnInc = graphColumn;
258             }
259
260             for (int j = graphX+columnInc; j < graphW+graphX; j+=graphColumn) {
261                 graphLine.setLine(j,graphY,j,graphY+graphH);
262                 big.draw(graphLine);
263             }
264
265             --columnInc;
266
267             // Plot memory usage by this memory pool.
268
if (usedMem[npool] == null) {
269         usedMem[npool] = new float[usageHistCount];
270                 ptNum[npool] = 0;
271             }
272
273         // save memory usage history.
274
usedMem[npool][ptNum[npool]] = usedMemory;
275
276             big.setColor(Color.yellow);
277
278         int w1; // width of memory usage history.
279
if (ptNum[npool] > graphW) {
280             w1 = graphW;
281         } else {
282         w1 = ptNum[npool];
283             }
284
285
286             for (int j=graphX+graphW-w1, k=ptNum[npool]-w1; k < ptNum[npool];
287                                 k++, j++) {
288                  if (k != 0) {
289                      if (usedMem[npool][k] != usedMem[npool][k-1]) {
290                  int h1 = (int)(graphY + graphH * ((totalMemory -usedMem[npool][k-1])/totalMemory));
291                  int h2 = (int)(graphY + graphH * ((totalMemory -usedMem[npool][k])/totalMemory));
292                          big.drawLine(j-1, h1, j, h2);
293                      } else {
294                  int h1 = (int)(graphY + graphH * ((totalMemory -usedMem[npool][k])/totalMemory));
295                          big.fillRect(j, h1, 1, 1);
296                      }
297                  }
298             }
299             if (ptNum[npool]+2 == usedMem[npool].length) {
300                 // throw out oldest point
301
for (int j = 1;j < ptNum[npool]; j++) {
302                      usedMem[npool][j-1] = usedMem[npool][j];
303                 }
304                 --ptNum[npool];
305             } else {
306                 ptNum[npool]++;
307             }
308         }
309
310
311         public void start() {
312             thread = new Thread JavaDoc(this);
313             thread.setPriority(Thread.MIN_PRIORITY);
314             thread.setName("MemoryMonitor");
315             thread.start();
316         }
317
318
319         public synchronized void stop() {
320             thread = null;
321             notify();
322         }
323
324         public void run() {
325
326             Thread JavaDoc me = Thread.currentThread();
327
328             while (thread == me && !isShowing() || getSize().width == 0) {
329                 try {
330                     thread.sleep(500);
331                 } catch (InterruptedException JavaDoc e) { return; }
332             }
333     
334             while (thread == me && isShowing()) {
335                 Dimension d = getSize();
336                 if (d.width != w || d.height != h) {
337                     w = d.width;
338                     h = d.height;
339                     bimg = (BufferedImage JavaDoc) createImage(w, h);
340                     big = bimg.createGraphics();
341                     big.setFont(font);
342                     FontMetrics fm = big.getFontMetrics(font);
343                     ascent = (int) fm.getAscent();
344                     descent = (int) fm.getDescent();
345                 }
346                 repaint();
347                 try {
348                     thread.sleep(sleepAmount);
349                 } catch (InterruptedException JavaDoc e) { break; }
350                 if (MemoryMonitor.dateStampCB.isSelected()) {
351                      System.out.println(new Date JavaDoc().toString() + " " + usedStr);
352                 }
353             }
354             thread = null;
355         }
356     }
357
358
359     // Test thread to consume memory
360
static class Memeater extends ClassLoader JavaDoc implements Runnable JavaDoc {
361     Object JavaDoc y[];
362     public Memeater() {}
363     public void run() {
364         y = new Object JavaDoc[10000000];
365         int k =0;
366         while(true) {
367              if (k == 5000000) k=0;
368              y[k++] = new Object JavaDoc();
369              try {
370              Thread.sleep(20);
371              } catch (Exception JavaDoc x){}
372
373          // to consume perm gen storage
374
try {
375                      // the classes are small so we load 10 at a time
376
for (int i=0; i<10; i++) {
377                         loadNext();
378                      }
379                  } catch (ClassNotFoundException JavaDoc x) {
380            // ignore exception
381
}
382
383        }
384
385     }
386
387     Class JavaDoc loadNext() throws ClassNotFoundException JavaDoc {
388
389             // public class TestNNNNNN extends java.lang.Object{
390
// public TestNNNNNN();
391
// Code:
392
// 0: aload_0
393
// 1: invokespecial #1; //Method java/lang/Object."<init>":()V
394
// 4: return
395
// }
396

397             int begin[] = {
398                 0xca, 0xfe, 0xba, 0xbe, 0x00, 0x00, 0x00, 0x30,
399                 0x00, 0x0a, 0x0a, 0x00, 0x03, 0x00, 0x07, 0x07,
400                 0x00, 0x08, 0x07, 0x00, 0x09, 0x01, 0x00, 0x06,
401                 0x3c, 0x69, 0x6e, 0x69, 0x74, 0x3e, 0x01, 0x00,
402                 0x03, 0x28, 0x29, 0x56, 0x01, 0x00, 0x04, 0x43,
403                 0x6f, 0x64, 0x65, 0x0c, 0x00, 0x04, 0x00, 0x05,
404                 0x01, 0x00, 0x0a, 0x54, 0x65, 0x73, 0x74 };
405
406             int end [] = {
407                 0x01, 0x00, 0x10,
408                 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e,
409                 0x67, 0x2f, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74,
410                 0x00, 0x21, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00,
411                 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x04,
412                 0x00, 0x05, 0x00, 0x01, 0x00, 0x06, 0x00, 0x00,
413                 0x00, 0x11, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
414                 0x00, 0x05, 0x2a, 0xb7, 0x00, 0x01, 0xb1, 0x00,
415                 0x00, 0x00, 0x00, 0x00, 0x00 };
416
417
418             // TestNNNNNN
419

420             String JavaDoc name = "Test" + Integer.toString(count++);
421
422             byte value[];
423             try {
424                 value = name.substring(4).getBytes("UTF-8");
425             } catch (java.io.UnsupportedEncodingException JavaDoc x) {
426                 throw new Error JavaDoc();
427             }
428
429             // construct class file
430

431             int len = begin.length + value.length + end.length;
432             byte b[] = new byte[len];
433             int i, pos=0;
434             for (i=0; i<begin.length; i++) {
435                 b[pos++] = (byte)begin[i];
436             }
437             for (i=0; i<value.length; i++) {
438                 b[pos++] = value[i];
439             }
440             for (i=0; i<end.length; i++) {
441                 b[pos++] = (byte)end[i];
442             }
443
444             return defineClass(name, b, 0, b.length);
445
446     }
447     static int count = 100000;
448     
449     }
450     
451     public static void main(String JavaDoc s[]) {
452         final MemoryMonitor demo = new MemoryMonitor();
453         WindowListener l = new WindowAdapter() {
454             public void windowClosing(WindowEvent e) {System.exit(0);}
455             public void windowDeiconified(WindowEvent e) { demo.surf.start(); }
456             public void windowIconified(WindowEvent e) { demo.surf.stop(); }
457         };
458         JFrame f = new JFrame("MemoryMonitor");
459         f.addWindowListener(l);
460         f.getContentPane().add("Center", demo);
461         f.pack();
462         f.setSize(new Dimension(400,500));
463         f.setVisible(true);
464         demo.surf.start();
465     Thread JavaDoc thr = new Thread JavaDoc(new Memeater());
466     thr.start();
467     }
468
469 }
470
471
Popular Tags