📄 heapmonitor.java
字号:
package view;//Copyright (C) 2008 Harald Unander//// This file is part of WlanTV.//// WlanTV is free software: you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation, either version 3 of the License, or// (at your option) any later version.//// WlanTV is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with WlanTV. If not, see <http://www.gnu.org/licenses/>.import java.awt.BasicStroke;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.util.Timer;import java.util.TimerTask;import javax.swing.JPanel;import main.Main;@SuppressWarnings("serial")public class HeapMonitor extends JPanel { private final int HISTORY_COUNT = 120; private final int UNIT = 10; private final Runtime rt = Runtime.getRuntime(); private final int maxMem = (int)rt.maxMemory()/1000000; private final BasicStroke STROKE = new BasicStroke(0); private Sample[] history = new Sample[HISTORY_COUNT]; private int pos; private BufferedImage buffer; private int allocatedMem; private int freeMem; private int usedMem; public HeapMonitor() { for (int i=0; i<HISTORY_COUNT; i++) { history[i] = new Sample(); } setPreferredSize(new Dimension(HISTORY_COUNT,0)); setMaximumSize(new Dimension(HISTORY_COUNT,30)); Timer t = new Timer(); t.scheduleAtFixedRate(new Refresh(),1000,1000); } @Override public void paint(Graphics g) { if (buffer != null) g.drawImage(buffer, 0, 0, this); } class Refresh extends TimerTask { @Override public void run() { allocatedMem = (int)rt.totalMemory()/1000000; usedMem = allocatedMem - (int)rt.freeMemory()/1000000; freeMem = maxMem - usedMem; buffer = (BufferedImage)createImage(getWidth(),getHeight()); Graphics2D g2d = buffer.createGraphics(); g2d.setFont(Main.SMALLFONT.deriveFont(9F)); g2d.setPaint(Color.RED); g2d.drawString(Integer.toString(maxMem)+"MB",2,10); double w = ((double) getWidth()-1)/HISTORY_COUNT; double h = ((double) getHeight()-1)/maxMem*UNIT; g2d.translate(0, getHeight()-1); g2d.scale(w,-h); g2d.setStroke(STROKE); history[pos].used = usedMem; history[pos].allocated = allocatedMem;// System.out.print(pos+":"); for (int i=0, x=HISTORY_COUNT-pos; i<HISTORY_COUNT; i++) { g2d.setPaint(Color.BLACK); g2d.drawRect((x+i)%HISTORY_COUNT, 0, 1, history[i].used/UNIT); g2d.fillRect((x+i)%HISTORY_COUNT, 0, 1, history[i].used/UNIT); g2d.setPaint(Color.GREEN); g2d.drawRect((x+i)%HISTORY_COUNT, history[i].allocated/UNIT, 1, 0);// System.out.print(history[i]+" "); } pos++; pos %= HISTORY_COUNT;// System.out.println(); g2d.setPaint(Color.BLACK); g2d.drawRect(0, 0, HISTORY_COUNT, maxMem/UNIT); g2d.setPaint(Color.RED); g2d.drawLine(0, maxMem/UNIT, HISTORY_COUNT, maxMem/UNIT); setToolTipText(getMemReport()); repaint(); } } public boolean memOk(boolean print) {// count++;// if (count%100 == 0 || print) {// if (count%1000 == 0 || print) {// System.out.println(count+// " Used:" + usedMem +// " Max:" + maxMem);// }// return freeMem > 495; //test return freeMem > 25;// }// else {// return true;// } } public String getMemReport() { return ( "Heap used (black color)" + usedMem + "MB, max (red color) " + maxMem + "MB" ); } class Sample { int allocated = 0; int used = 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -