📄 memorymonitorpanel.java
字号:
/*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Created on 2006/8/7
*
* @Author: Xiaojun Chen
* $Revision$ 1.0
*
*/
package eti.bi.alphaminer.tools.SystemTools.SystemToolPage;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import javax.swing.JPanel;
import eti.bi.util.MemoryUtil;
public class MemoryMonitorPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
private static final Color MEMORY_COLOR = new Color(0, 200, 0); // ,128);
private static final Color TEXT_COLOR = new Color(200, 10, 0);
private static final Color GRID_COLOR = Color.gray;
private static final Color BACKGROUND = new Color(0, 60, 0);
private static final String[] MEMORY_UNITS = { "b", "kB", "MB", "GB", "TB" };
private static final int NUMBER_OF_MEASUREMENTS = 20;
private static final int GRID_X = 10;
private static final int GRID_Y = 10;
private static final int MARGIN = 10;
private static final int TEXT_MARGIN = 65;
private long delay = 1000;
private long[] memory = new long[NUMBER_OF_MEASUREMENTS];
private int currentMeasurement = 0;
private boolean monitoring;
private boolean monitorable;
private Thread monitorThread = new Thread() {
{
setDaemon(true);
}
public void run() {
setPriority(MIN_PRIORITY);
while (true) {
memory[currentMeasurement] = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
currentMeasurement = (currentMeasurement + 1) % memory.length;
repaint();
try {
sleep(delay);
} catch (InterruptedException e) {}
}
}
};
public MemoryMonitorPanel() {
setBackground(BACKGROUND);
startmonitor();
}
private void startmonitor(){
monitoring = true;
monitorThread.start();
}
private void resumemonitor() {
monitoring = true;
Runnable runnable = new Runnable() {
@SuppressWarnings("deprecation")
public void run() {
monitorThread.resume();
}
};
runnable.run();
}
private void pasueMonitor(){
monitoring = false;
Runnable runnable = new Runnable() {
@SuppressWarnings({ "static-access", "deprecation" })
public void run() {
monitorThread.suspend();
}
};
runnable.run();
}
@SuppressWarnings("unused")
private void stopMonitor(){
monitoring = false;
Runnable runnable = new Runnable() {
@SuppressWarnings("deprecation")
public void run() {
monitorThread.stop();
}
};
runnable.run();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
GeneralPath path = new GeneralPath();
Dimension d = getSize();
int monitorWidth = (int) d.getWidth() - 2 * MARGIN;
int monitorHeight = (int) d.getHeight() - 2 * MARGIN-TEXT_MARGIN;
int topMargin = MARGIN+TEXT_MARGIN;
long total = Runtime.getRuntime().totalMemory();
path.moveTo(MARGIN, topMargin + monitorHeight);
for (int i = 0; i < memory.length; i++) {
int index = (currentMeasurement + i) % memory.length;
path.lineTo(MARGIN + i * monitorWidth / (memory.length - 1), topMargin+ monitorHeight - monitorHeight * memory[index] / total);
}
path.lineTo(MARGIN + monitorWidth, topMargin + monitorHeight);
path.closePath();
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(GRID_COLOR);
for (int x = 0; x < GRID_X + 1; x++) {
g2d.drawLine(MARGIN + x * monitorWidth / GRID_X, topMargin, MARGIN + x * monitorWidth / GRID_X, topMargin + monitorHeight);
}
for (int y = 0; y < GRID_Y + 1; y++) {
g2d.drawLine(MARGIN, topMargin + y * monitorHeight / GRID_Y, MARGIN + monitorWidth, topMargin + y * monitorHeight / GRID_Y);
}
g2d.setColor(MEMORY_COLOR);
g2d.fill(path);
g2d.setColor(Color.green);
g2d.draw(path);
g2d.setColor(TEXT_COLOR);
Font font = new Font("Courier", Font.PLAIN, 12);
g2d.setFont(font);
try {
g2d.drawString(" Max in System: " + humanReadable(Runtime.getRuntime().maxMemory()), MARGIN, MARGIN + 3 * font.getSize() / 2);
g2d.drawString(" Max in JVM: " + humanReadable(total), MARGIN, MARGIN + 5 * font.getSize() / 2);
g2d.drawString(" Used: "+humanReadable(MemoryUtil.usedMemory()), MARGIN, MARGIN + 7 * font.getSize() / 2);
g2d.drawString(" Free: "+humanReadable(MemoryUtil.freeMemory()), MARGIN, MARGIN + 9 * font.getSize() / 2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String humanReadable(long bytes) {
long result = bytes;
long rest = 0;
int unit = 0;
while (result > 1024) {
rest = result % 1024;
result /= 1024;
unit++;
if (unit >= MEMORY_UNITS.length - 1)
break;
}
if ((result < 10) && (unit > 0)) {
return result + "." + (10 * rest / 1024) + " " + MEMORY_UNITS[unit];
} else {
return result + " " + MEMORY_UNITS[unit];
}
}
/**
* set if monitoring
* @param monitor 'true' if monitor, else 'false'
* */
public void setMonitorable(boolean monitor){
this.monitorable = monitor;
}
public boolean isMonitorable() {
return monitorable;
}
/**
* @return if monitoring
* */
public boolean isMonitoring(){
return monitoring;
}
/**
* set if starting monitoring
* @param monitor if starting monitorg
* */
public void monitor(boolean monitoring){
if(!monitorable) {
return;
}
if(monitoring){
resumemonitor();
}
else{
pasueMonitor();
}
}
public void pasued(){
if(!monitorable) {
return;
}
pasueMonitor();
}
public void dispose(){
Runnable runnable = new Runnable() {
@SuppressWarnings("deprecation")
public void run() {
monitorThread.destroy();
}
};
runnable.run();
dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -