📄 memorymonitor.java
字号:
int graphX = x1+30; int graphY = y1 + (int) ssH; int graphW = (int) remainingWidth; int graphH = (int) remainingHeight; graphOutlineRect.setRect(graphX, graphY, graphW, graphH); big.draw(graphOutlineRect); int graphRow = graphH/10; // .. Draw row .. for (int j = graphY; j <= graphH+graphY; j += graphRow) { graphLine.setLine(graphX,j,graphX+graphW,j); big.draw(graphLine); } // .. Draw animated column movement .. int graphColumn = graphW/15; if (columnInc == 0) { columnInc = graphColumn; } for (int j = graphX+columnInc; j < graphW+graphX; j+=graphColumn) { graphLine.setLine(j,graphY,j,graphY+graphH); big.draw(graphLine); } --columnInc; // Plot memory usage by this memory pool. if (usedMem[npool] == null) { usedMem[npool] = new float[usageHistCount]; ptNum[npool] = 0; } // save memory usage history. usedMem[npool][ptNum[npool]] = usedMemory; big.setColor(Color.yellow); int w1; // width of memory usage history. if (ptNum[npool] > graphW) { w1 = graphW; } else { w1 = ptNum[npool]; } for (int j=graphX+graphW-w1, k=ptNum[npool]-w1; k < ptNum[npool]; k++, j++) { if (k != 0) { if (usedMem[npool][k] != usedMem[npool][k-1]) { int h1 = (int)(graphY + graphH * ((totalMemory -usedMem[npool][k-1])/totalMemory)); int h2 = (int)(graphY + graphH * ((totalMemory -usedMem[npool][k])/totalMemory)); big.drawLine(j-1, h1, j, h2); } else { int h1 = (int)(graphY + graphH * ((totalMemory -usedMem[npool][k])/totalMemory)); big.fillRect(j, h1, 1, 1); } } } if (ptNum[npool]+2 == usedMem[npool].length) { // throw out oldest point for (int j = 1;j < ptNum[npool]; j++) { usedMem[npool][j-1] = usedMem[npool][j]; } --ptNum[npool]; } else { ptNum[npool]++; } } public void start() { thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.setName("MemoryMonitor"); thread.start(); } public synchronized void stop() { thread = null; notify(); } public void run() { Thread me = Thread.currentThread(); while (thread == me && !isShowing() || getSize().width == 0) { try { thread.sleep(500); } catch (InterruptedException e) { return; } } while (thread == me && isShowing()) { Dimension d = getSize(); if (d.width != w || d.height != h) { w = d.width; h = d.height; bimg = (BufferedImage) createImage(w, h); big = bimg.createGraphics(); big.setFont(font); FontMetrics fm = big.getFontMetrics(font); ascent = (int) fm.getAscent(); descent = (int) fm.getDescent(); } repaint(); try { thread.sleep(sleepAmount); } catch (InterruptedException e) { break; } if (MemoryMonitor.dateStampCB.isSelected()) { System.out.println(new Date().toString() + " " + usedStr); } } thread = null; } } // Test thread to consume memory static class Memeater extends ClassLoader implements Runnable { Object y[]; public Memeater() {} public void run() { y = new Object[10000000]; int k =0; while(true) { if (k == 5000000) k=0; y[k++] = new Object(); try { Thread.sleep(20); } catch (Exception x){} // to consume perm gen storage try { // the classes are small so we load 10 at a time for (int i=0; i<10; i++) { loadNext(); } } catch (ClassNotFoundException x) { // ignore exception } } } Class loadNext() throws ClassNotFoundException { // public class TestNNNNNN extends java.lang.Object{ // public TestNNNNNN(); // Code: // 0: aload_0 // 1: invokespecial #1; //Method java/lang/Object."<init>":()V // 4: return // } int begin[] = { 0xca, 0xfe, 0xba, 0xbe, 0x00, 0x00, 0x00, 0x30, 0x00, 0x0a, 0x0a, 0x00, 0x03, 0x00, 0x07, 0x07, 0x00, 0x08, 0x07, 0x00, 0x09, 0x01, 0x00, 0x06, 0x3c, 0x69, 0x6e, 0x69, 0x74, 0x3e, 0x01, 0x00, 0x03, 0x28, 0x29, 0x56, 0x01, 0x00, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x0c, 0x00, 0x04, 0x00, 0x05, 0x01, 0x00, 0x0a, 0x54, 0x65, 0x73, 0x74 }; int end [] = { 0x01, 0x00, 0x10, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x00, 0x21, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x04, 0x00, 0x05, 0x00, 0x01, 0x00, 0x06, 0x00, 0x00, 0x00, 0x11, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x2a, 0xb7, 0x00, 0x01, 0xb1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // TestNNNNNN String name = "Test" + Integer.toString(count++); byte value[]; try { value = name.substring(4).getBytes("UTF-8"); } catch (java.io.UnsupportedEncodingException x) { throw new Error(); } // construct class file int len = begin.length + value.length + end.length; byte b[] = new byte[len]; int i, pos=0; for (i=0; i<begin.length; i++) { b[pos++] = (byte)begin[i]; } for (i=0; i<value.length; i++) { b[pos++] = value[i]; } for (i=0; i<end.length; i++) { b[pos++] = (byte)end[i]; } return defineClass(name, b, 0, b.length); } static int count = 100000; } public static void main(String s[]) { final MemoryMonitor demo = new MemoryMonitor(); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowDeiconified(WindowEvent e) { demo.surf.start(); } public void windowIconified(WindowEvent e) { demo.surf.stop(); } }; JFrame f = new JFrame("MemoryMonitor"); f.addWindowListener(l); f.getContentPane().add("Center", demo); f.pack(); f.setSize(new Dimension(400,500)); f.setVisible(true); demo.surf.start(); Thread thr = new Thread(new Memeater()); thr.start(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -