`
javasogo
  • 浏览: 1774278 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

内存监视 Java 源代码

阅读更多

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

/*
* @(#)MemoryMonitor.java1.3 05/11/17
*/

/*
* 2007.3.2 修正 被蒋彪 增加log纪录功能
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.Date;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import java.lang.management.*;
import java.util.*;
import java.io.*;

/**
* Demo code which plots the memory usage by all memory pools.
* The memory usage is sampled at some time interval using
* java.lang.management API. This demo code is modified based
* java2d MemoryMonitor demo.
*/
public class MemoryMonitor extends JPanel {

static JCheckBox dateStampCB = new JCheckBox("Output Date Stamp");
// main inferace
public Surface surf;
JPanel controls;
boolean doControls;
JTextField tf;
// Get memory pools.
static java.util.List<MemoryPoolMXBean> mpools =
ManagementFactory.getMemoryPoolMXBeans();
// Total number of memory pools.
static int numPools = mpools.size();
static String PATH = "C:\\";
static String NAME = "log.txt";
public MemoryMonitor() {
setLayout(new BorderLayout());
setBorder(new TitledBorder(new EtchedBorder(), "Memory Monitor"));
// new the main inferace class and allocate it memory
add(surf = new Surface());
controls = new JPanel();
controls.setPreferredSize(new Dimension(135,80));
Font font = new Font("serif", Font.PLAIN, 10);
JLabel label = new JLabel("Sample Rate");
label.setFont(font);
label.setForeground(Color.red);
controls.add(label);
tf = new JTextField("1000");
tf.setPreferredSize(new Dimension(45,20));
controls.add(tf);
controls.add(label = new JLabel("ms"));
label.setFont(font);
label.setForeground(Color.red);
controls.add(dateStampCB);
dateStampCB.setFont(font);
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
removeAll();
if ((doControls = !doControls)) {
surf.stop();
add(controls);
} else {
try {
surf.sleepAmount = Long.parseLong(tf.getText().trim());
} catch (Exception ex) {}
surf.start();
add(surf);
}
validate();
repaint();
}
});
}

// main inferace class
public class Surface extends JPanel implements Runnable {
// the main Thread of the main inferace class
public Thread thread;
// the sleep time
public long sleepAmount = 1000;
public int usageHistCount = 20000;
private int w, h;
// The BufferedImage can create image in the system memory
private BufferedImage bimg;
// Graphics2D is the class which can slove and paint 2D image in JPanel
private Graphics2D big;
// create the Font which is used by the Programe
private Font font = new Font("Times New Roman", Font.PLAIN, 11);
private int columnInc;
private float usedMem[][];
private int ptNum[];
private int ascent, descent;
private Rectangle graphOutlineRect = new Rectangle();
private Rectangle2D mfRect = new Rectangle2D.Float();
private Rectangle2D muRect = new Rectangle2D.Float();
private Line2D graphLine = new Line2D.Float();
// give the Color to Graphocs2D class TODO
private Color graphColor = new Color(46, 139, 87);
// give the Color to Graphocs2D class TODO
private Color mfColor = new Color(0, 100, 0);
// storage the memory that has been used
private String usedStr;
// arrayList can storge the value that will be write into "log.txt"
ArrayList arrayList = new ArrayList();
//the class bulid function
public Surface() {
// set the background color
setBackground(Color.black);
// add the mouse click event : if the class has been stop , mouse click will start it, converstion also ok
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (thread == null) {
start();
} else {
stop();
}
}
});
int i = 0;
usedMem = new float[numPools][];
ptNum = new int[numPools];
}

/**
* create a new File
* @param filePath
* @param fileContent
* @return
*/
public void createFile(String filePath, String fileName) {

try {
String fileAddress = filePath + "\\" +fileName;
//filePath = fileAddress .toString();
File myFilePath = new File(fileAddress );
if (!myFilePath.exists()) {
myFilePath.createNewFile();
}
}
catch (Exception e) {
System.err.println(e);
}
}

/**
* transfer ArrayList to String[]
*
* @param arrayList
*/
public String[] arrayToStringArray(ArrayList arrayList) {
int length = 0;
if (null != arrayList) {
length = arrayList.size();
}
String[] returnValue = new String[length];
String temp = new String();
for (int i = 0; i < arrayList.size(); i++) {
temp = (String)arrayList.get(i);
returnValue[i] = temp;
}
return returnValue;
}

/**
* write into File
* @param filePath
* @param fileName
* @param args
* @throws IOException
*/
public void writeFile(String filePath, String fileName, String args[] )throws
IOException {
FileWriter fw = new FileWriter(filePath + fileName);
PrintWriter out = new PrintWriter(fw);
for (int i = 0; i < args.length; i++) {
out.write(args[i]);
out.println();
out.flush();
}
fw.close();
out.close();
}

// Dimension is a class that including the height and width value
public Dimension getMinimumSize() {
return getPreferredSize();
}

public Dimension getMaximumSize() {
return getPreferredSize();
}

public Dimension getPreferredSize() {
return new Dimension(135,80);
}

// invoke to draw the class(JPanel)
public void paint(Graphics g) {

if (big == null) {
return;
}

big.setBackground(getBackground());
big.clearRect(0,0,w,h);


h = h / ((numPools + numPools%2) / 2);
w = w / 2;

int k=0; // index of memory pool.
for (int i=0; i < 2;i++) {
for (int j=0; j < (numPools + numPools%2)/ 2; j++) {
plotMemoryUsage(w*i,h*j,w,h,k);
if (++k >= numPools) {
i = 3;
j = (numPools + numPools%2)/ 2;
break;
}
}
}
g.drawImage(bimg, 0, 0, this);
}

public void plotMemoryUsage(int x1, int y1, int x2, int y2, int npool) {

MemoryPoolMXBean mp = mpools.get(npool);
float usedMemory = mp.getUsage().getUsed();
float totalMemory = mp.getUsage().getMax();

// .. Draw allocated and used strings ..
big.setColor(Color.green);

// Print Max memory allocated for this memory pool.
big.drawString(String.valueOf((int)totalMemory/1024) + "K Max ", x1+4.0f, (float) y1 + ascent+0.5f);
big.setColor(Color.yellow);

// Print the memory pool name.
big.drawString(mp.getName(), x1+x2/2, (float) y1 + ascent+0.5f);

// Print the memory used by this memory pool.
usedStr = String.valueOf((int)usedMemory/1024)
+ "K used";
big.setColor(Color.green);
big.drawString(usedStr, x1+4, y1+y2-descent);

// Calculate remaining size
float ssH = ascent + descent;
float remainingHeight = (float) (y2 - (ssH*2) - 0.5f);
float blockHeight = remainingHeight/10;
float blockWidth = 20.0f;
float remainingWidth = (float) (x2 - blockWidth - 10);

// .. Memory Free ..
big.setColor(mfColor);
int MemUsage = (int) (((totalMemory - usedMemory) / totalMemory) * 10);
int i = 0;
for ( ; i < MemUsage ; i++) {
mfRect.setRect(x1+5,(float) y1+ssH+i*blockHeight,
blockWidth,(float) blockHeight-1);
big.fill(mfRect);
}

// .. Memory Used ..
big.setColor(Color.green);
for ( ; i < 10; i++) {
muRect.setRect(x1+5,(float) y1 + ssH+i*blockHeight,
blockWidth,(float) blockHeight-1);
big.fill(muRect);
}

// .. Draw History Graph ..
if (remainingWidth <= 30) remainingWidth = (float)30;
if (remainingHeight <= ssH) remainingHeight = (float)ssH;
big.setColor(graphColor);
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() {
// get the current Thread
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) {
// the getSize() function can return the width/height from this component
// give the w the value from getSize() function
w = d.width;
// give the h the value from getSize() function
h = d.height;
// create the BufferImage by set the width into w and height into h
bimg = (BufferedImage) createImage(w, h);
// instance the Graphics2D class by using the BufferImage
big = bimg.createGraphics();
// set the Font of the Graphics2D
big.setFont(font);
FontMetrics fm = big.getFontMetrics(font);
ascent = (int) fm.getAscent();
descent = (int) fm.getDescent();
}
// repaint the image
repaint();

try {
// let the thread sleep just 1000 seconds
thread.sleep(sleepAmount);
} catch (InterruptedException e) {
break;
}
// the panel [dateStampCB] has been selected
// then print the information into the cmd screen
if (MemoryMonitor.dateStampCB.isSelected()) {
System.out.println(new Date().toString() + " " + usedStr);
String temp = new String(new Date().toString() + " " + usedStr);
arrayList.add(temp);
}

}
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) {
demo.surf.createFile(PATH, NAME);
String temp[] = demo.surf.arrayToStringArray(demo.surf.arrayList);
// for (int i = 0; i < temp.length; i ++) {
// System.out.println("temp is "+temp[i]);
// }
try {
demo.surf.writeFile(PATH, NAME, temp);
} catch(IOException IOe) {
System.err.println(e);
}
demo.surf.arrayList.clear();
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();
}

}

分享到:
评论

相关推荐

    JAVA上百实例源码以及开源项目源代码

    Java源代码实现部分,比较有意思,也具参考性。像坐标控制、旋转矩阵、定时器、生成图像、数据初始化、矩阵乘法、坐标旋转、判断是否是顺时针方向排列、鼠标按下、放开时的动作等,都可在本源码中得以体现。 Java...

    java源码包---java 源码 大量 实例

    Java源代码实现部分,比较有意思,也具参考性。像坐标控制、旋转矩阵、定时器、生成图像、数据初始化、矩阵乘法、坐标旋转、判断是否是顺时针方向排列、鼠标按下、放开时的动作等,都可在本源码中得以体现。 Java...

    JAVA上百实例源码以及开源项目

    Java源代码实现部分,比较有意思,也具参考性。像坐标控制、旋转矩阵、定时器、生成图像、数据初始化、矩阵乘法、坐标旋转、判断是否是顺时针方向排列、鼠标按下、放开时的动作等,都可在本源码中得以体现。 Java...

    java范例开发大全源代码

     实例154 使用静态成员变量计算内存中实例化的对象数目 239  实例155 实现加减乘除的方法 240  8.3 面向对象的设计模式 241  实例156 Singleton单例模式 242  实例157 招聘(简单工厂模式) 243  ...

    java源码包2

    Java源代码实现部分,比较有意思,也具参考性。像坐标控制、旋转矩阵、定时器、生成图像、数据初始化、矩阵乘法、坐标旋转、判断是否是顺时针方向排列、鼠标按下、放开时的动作等,都可在本源码中得以体现。 Java...

    java源码包4

    Java源代码实现部分,比较有意思,也具参考性。像坐标控制、旋转矩阵、定时器、生成图像、数据初始化、矩阵乘法、坐标旋转、判断是否是顺时针方向排列、鼠标按下、放开时的动作等,都可在本源码中得以体现。 Java...

    java源码包3

    Java源代码实现部分,比较有意思,也具参考性。像坐标控制、旋转矩阵、定时器、生成图像、数据初始化、矩阵乘法、坐标旋转、判断是否是顺时针方向排列、鼠标按下、放开时的动作等,都可在本源码中得以体现。 Java...

    【项目实战源代码】一个基于 Spring Boot + Vue 前后端分离的导航网站

    用户管理:用户是系统... 服务监控:监视当前系统CPU、内存、磁盘、堆栈等相关信息。 在线构建器:拖动表单元素生成相应的HTML代码。 连接池监视:监视当前系统数据库连接池状态,可进行分析SQL找出系统性能瓶颈。

    JAVA基于局域网的聊天室系统(源代码+论文).zip

    JAVA基于局域网的聊天室系统(源代码+论文) 摘 要 视频聊天系统作为一种新型的通信和交流工具,突破了地域的限制,可以提供更为便捷、灵活、全面的音、视频信息的传递和服务,具有极其广泛的发展前景。 本文介绍了...

    Java监视内存使用情况

    Java监视内存使用情况,运行Java 程序一般要消耗大量的内存,这曾经是java 的一个致命弱点。当然随着硬件的飞速发展,这个缺点有所弱化,同时java 虚拟机的逐渐成熟,也使内存的消耗量减少。那么一个较小java 程序 ...

    java_jsp项目源码_+SQL电量监视系统设计与实现(源代码+论文).rar

    本项目是一个基于Java和JSP技术的SQL电量监视系统,旨在实时监控和管理SQL数据库的电量使用情况。通过对数据库的电量数据进行实时采集、分析和可视化展示,帮助用户了解数据库的能耗状况,从而优化数据库性能,降低...

    成百上千个Java 源码DEMO 4(1-4是独立压缩包)

    Java源代码实现部分,比较有意思,也具参考性。像坐标控制、旋转矩阵、定时器、生成图像、数据初始化、矩阵乘法、坐标旋转、判断是否是顺时针方向排列、鼠标按下、放开时的动作等,都可在本源码中得以体现。 Java...

    成百上千个Java 源码DEMO 3(1-4是独立压缩包)

    Java源代码实现部分,比较有意思,也具参考性。像坐标控制、旋转矩阵、定时器、生成图像、数据初始化、矩阵乘法、坐标旋转、判断是否是顺时针方向排列、鼠标按下、放开时的动作等,都可在本源码中得以体现。 Java...

    java核心面试技术点

    编译源代码为本地机器码执行。 内存泄露是一部分内存无法回收。溢出是说内存不够用了。泄露可能在将来会导致溢出 当对象在程序中不会被使用,但却有其他对象持有该对象时会发生溢出。比如出现持有的环的情况。 ...

    java核心面试

    编译源代码为本地机器码执行。 内存泄露是一部分内存无法回收。溢出是说内存不够用了。泄露可能在将来会导致溢出 当对象在程序中不会被使用,但却有其他对象持有该对象时会发生溢出。比如出现持有的环的情况。 ...

    Java范例开发大全 (源程序)

     实例154 使用静态成员变量计算内存中实例化的对象数目 239  实例155 实现加减乘除的方法 240  8.3 面向对象的设计模式 241  实例156 Singleton单例模式 242  实例157 招聘(简单工厂模式) 243  实例158...

    精通qt4编程(源代码)

    \和Java的“一次编译,到处运行”跨平台不同的是,Qt是源代码级的跨平台,一次编写,随处编译。一次开发的Qt应用程序可以移植到不同的平台上,只需重新编译即可运行。Qt支持的平台有: \? Microsoft Windows,包括...

    基于 SpringBoot、Spring Security、JWT 的前后端分离权限管理系统+源代码+文档说明

    - 服务监控:监视当前系统CPU、内存、磁盘、堆栈等相关信息。 - 缓存监控:对系统的缓存信息查询,命令统计等。 - 在线构建器:拖动表单元素生成相应的HTML代码。 - 连接池监视:监视当前系统数据库连接池状态,可...

    精通Qt4编程(第二版)源代码

    \和Java的“一次编译,到处运行”跨平台不同的是,Qt是源代码级的跨平台,一次编写,随处编译。一次开发的Qt应用程序可以移植到不同的平台上,只需重新编译即可运行。Qt支持的平台有: \? Microsoft Windows,包括...

Global site tag (gtag.js) - Google Analytics