• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

JAVA程序实现监视-JAVA程序内存使用情况

发布: 2007-7-14 21:19 | 作者: 佚名    | 来源: 网络转载     | 查看: 122次 | 进入软件测试论坛讨论

领测软件测试网 =========================================
package myprojects.jmemorydemo;

import javax.swing.UIManager;
import java.awt.*;


public class JMemoryDemo {
private boolean packFrame = false;


public JMemoryDemo() {
MainFrame frame = new MainFrame();

if (packFrame) {
frame.pack();
}
else {
frame.validate();
}

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height)
/ 2);
frame.setVisible(true);
}

public static void main(String[] args)
{
new JMemoryDemo();
}
}

===================================================
package myprojects.jmemorydemo;
import javax.swing.*;



public class ProgressThread extends Thread {
JProgressBar pbar;
JLabel label;

public ProgressThread(JProgressBar pbar,JLabel label) {
this.pbar=pbar;
this.label=label;
}
public void run() {


int min=0;
int max=100;
int free=0;
int totle=0;
int status=0;

pbar.setMinimum(min);
pbar.setMaximum(max);
pbar.setValue(status);
while(true){
try{
totle=(int)(Runtime.getRuntime().totalMemory()/1024);
free=(int)(Runtime.getRuntime().freeMemory()/1024);
}catch(Exception e){
e.printStackTrace();
}
label.setText("Free Memory :"+(int)(Runtime.getRuntime().freeMemory()/1024)+"K"+" Totle Memory Allo
cated :"
+(int)(Runtime.getRuntime().totalMemory()/1024)+"K");
status=(int)(free*100/totle);
pbar.setValue(status);
pbar.setString("Free Momery "+ status+"%");
try{
this.sleep(1000);
}catch(InterruptedException err){}
}

}

}

===============================================
package myprojects.jmemorydemo;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class MainFrame extends JFrame {
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
private JProgressBar jProgressBar1 = new JProgressBar();
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();


public MainFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
(MainFrame.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(304, 215));
this.setTitle("JMemoryDemo");
jLabel1.setFont(new java.awt.Font("Dialog", 0, 14));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("Memory Monitor");
jProgressBar1.setOrientation(JProgressBar.VERTICAL);
jProgressBar1.setFont(new java.awt.Font("Dialog", 0, 14));
jProgressBar1.setToolTipText("");
jProgressBar1.setStringPainted(true);
jLabel2.setFont(new java.awt.Font("Dialog", 0, 14));
jLabel2.setText("");
contentPane.add(jProgressBar1, BorderLayout.CENTER);
contentPane.add(jLabel1, BorderLayout.NORTH);
contentPane.add(jLabel2, BorderLayout.SOUTH);

ProgressThread pThread = new ProgressThread(this.jProgressBar1,this.jLabel2);
pThread.start();
}

protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
======================================================================
提示如下:
======================================================================
问题的关键:
totle=(int)(Runtime.getRuntime().totalMemory()/1024);
free=(int)(Runtime.getRuntime().freeMemory()/1024);
===================================================
有什么问题,可以发Email:dw5189@163.com
========================================

提供最新版的JCreator PRO build 3.00.094

http://www.jcreator.com/download.htm

Username: P145460-Brian Luke

Key: 00HE3T-MX6F41-7E9CFN-68745A-RD1PAJ-0GKKPP-YYJVDR-CXJXHG-6GFXX8
========================================

提供JCreator PRO build 2.50序列号:

注册码:
etalking
niit
P-etalking niit
36336-27630-63362-56226-23628

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网