1 36 37 40 41 import java.awt.*; 42 import java.awt.event.*; 43 import java.awt.image.BufferedImage ; 44 import java.awt.geom.Line2D ; 45 import java.awt.geom.Rectangle2D ; 46 import java.util.Date ; 47 import javax.swing.*; 48 import javax.swing.border.EtchedBorder ; 49 import javax.swing.border.TitledBorder ; 50 import java.lang.management.*; 51 import java.util.*; 52 53 54 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 static java.util.List <MemoryPoolMXBean> mpools = 69 ManagementFactory.getMemoryPoolMXBeans(); 70 static int numPools = mpools.size(); 72 73 public MemoryMonitor() { 74 setLayout(new BorderLayout()); 75 setBorder(new TitledBorder (new EtchedBorder (), "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 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 { 113 114 public Thread thread; 115 public long sleepAmount = 1000; 116 public int usageHistCount = 20000; 117 private int w, h; 118 private BufferedImage 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 mfRect = new Rectangle2D.Float (); 127 private Rectangle2D muRect = new Rectangle2D.Float (); 128 private Line2D graphLine = new Line2D.Float (); 129 private Color graphColor = new Color(46, 139, 87); 130 private Color mfColor = new Color(0, 100, 0); 131 private String 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; 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 big.setColor(Color.green); 194 195 big.drawString(String.valueOf((int)totalMemory/1024) + "K Max ", x1+4.0f, (float) y1 + ascent+0.5f); 197 big.setColor(Color.yellow); 198 199 big.drawString(mp.getName(), x1+x2/2, (float) y1 + ascent+0.5f); 201 202 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 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 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 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 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 for (int j = graphY; j <= graphH+graphY; j += graphRow) { 249 graphLine.setLine(graphX,j,graphX+graphW,j); 250 big.draw(graphLine); 251 } 252 253 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 if (usedMem[npool] == null) { 269 usedMem[npool] = new float[usageHistCount]; 270 ptNum[npool] = 0; 271 } 272 273 usedMem[npool][ptNum[npool]] = usedMemory; 275 276 big.setColor(Color.yellow); 277 278 int w1; 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 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 (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 me = Thread.currentThread(); 327 328 while (thread == me && !isShowing() || getSize().width == 0) { 329 try { 330 thread.sleep(500); 331 } catch (InterruptedException 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 ) 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 e) { break; } 350 if (MemoryMonitor.dateStampCB.isSelected()) { 351 System.out.println(new Date ().toString() + " " + usedStr); 352 } 353 } 354 thread = null; 355 } 356 } 357 358 359 static class Memeater extends ClassLoader implements Runnable { 361 Object y[]; 362 public Memeater() {} 363 public void run() { 364 y = new Object [10000000]; 365 int k =0; 366 while(true) { 367 if (k == 5000000) k=0; 368 y[k++] = new Object (); 369 try { 370 Thread.sleep(20); 371 } catch (Exception x){} 372 373 try { 375 for (int i=0; i<10; i++) { 377 loadNext(); 378 } 379 } catch (ClassNotFoundException x) { 380 } 382 383 } 384 385 } 386 387 Class loadNext() throws ClassNotFoundException { 388 389 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 420 String name = "Test" + Integer.toString(count++); 421 422 byte value[]; 423 try { 424 value = name.substring(4).getBytes("UTF-8"); 425 } catch (java.io.UnsupportedEncodingException x) { 426 throw new Error (); 427 } 428 429 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 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 thr = new Thread (new Memeater()); 466 thr.start(); 467 } 468 469 } 470 471 | Popular Tags |