站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2ME 专题Color类和测试内存的类MemoryMonitor

Color类和测试内存的类MemoryMonitor

摘要: Color类和测试内存的类MemoryMonitor
<tr><td>

[本文章最后由 rocks 在2006-05-27 23:34:16编辑过]

http:///tech/article1905.html
[转载于CrazyWind的博客]

原文:http://blog.csdn.net/dongfengsun/archive/2006/05/19/745624.aspx

//Color.java

public final class Color //final表示该类不可被重写
{
static final int RED=0xff0000;
[]static final int GREEN=0xff00;
static final int BLUE=0xff;
static final int YELLOW=0xffff00;
static final int BLACK=0;
static final int WHITE=0xffffff;

public static int newColor(int r,int g,int b)
{
return (r&0xff)<<16|(g&0xff)<<8|(b&0xff);
}
}



//MemoryMonitor.java 测试内存使用峰值

import java.util.Date;
import javax.microedition.lcdui.Graphics;
class MemoryMonitor implements Runnable
[]{
private static final long defaultInterval=10;//默认的时间间隔是10ms
[]
[]private static final long minInterval=1;//最小的时间间隔是1ms
private long interval;//定义时间间隔

private Runtime rt;//lang包里的一个类,每个java应用程序都有一个独立的Runtime类实例,用于和应用程序运行的环境交互
private long usedMemory;//已经用掉的内存
private long maxUsedMemory;//已经被用掉的最大内存
private boolean alive=true;//是否存活

MemoryMonitor()
{
this(defaultInterval);
}
MemoryMonitor(long interval)
{
if(interval>minInterval)//如果时间间隔大于最小时间间隔
this.interval=interval;
else
this.interval=defaultInterval;//否则时间间隔是默认的时间间隔
new Thread(this).start();//启动新线程
}
public void run()
{
while(alive)
{
[] rt=Runtime.getRuntime();//取得一个Runtime的实例
usedMemory=rt.totalMemory()-rt.freeMemory();//已经用的内存=总内存-剩余内存
[] rt=null;
maxUsedMemory=Math.max(usedMemory,maxUsedMemory);//最大内存是已用内存和已用最大内存里的教大的一个
try
{
Thread.sleep(interval);//让线程休息一个时间间隔拉
}
catch(Exception e)
{
}
}
}
public void end()
{
alive=false;
}
public void paint(Graphics g,int x,int y,int anchor)
[]{

g.drawString("maxUsedMemory="+maxUsedMemory,x,y,anchor);//在屏幕上画出已经用的最大内存
}
[]}
http:///tech/article1905.html
</td></tr></table></td> </tr> <tr> <td background="/pic/split.gif" height=1></td> </tr> <tr> <td class="tdMargin1">
↑返回目录
前一篇: J2ME里排序算法探究
后一篇: J2ME键盘响应祥解及处理方法