当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:13:00  来源:本站整理

<b>监督JAVA程序内存操纵情形</b>[Java编程]

赞助商链接



  本文“<b>监督JAVA程序内存操纵情形</b>[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

=========================================

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);


  以上是“<b>监督JAVA程序内存操纵情形</b>[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • <b>hosts是什么 hosts文件在什么位置 若何改正hosts</b>
  • <b>在 Windows 8 中手动安装语言包</b>
  • <b>五个常见 PHP数据库问题</b>
  • Windows中Alt键的12个高效快速的利用本领介绍
  • <b>MySQL ORDER BY 的实现解析</b>
  • <b>详解MySQL存储历程参数有三种范例(in、out、inout)</b>
  • <b>Win8系统恢复出来经典的开始菜单的办法</b>
  • <b>Win8系统花屏怎么办 Win8系统花屏的办理办法</b>
  • <b>Windows 7系统下无线网卡安装</b>
  • <b>为什么 Linux不需求碎片整理</b>
  • <b>Windows 8中删除账户的几种办法(图)</b>
  • <b>教你如安在win7下配置路由器</b>
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .