1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > android如何获取进程占用的内存大小 Android获取cpu使用率 剩余内存和硬盘容量

android如何获取进程占用的内存大小 Android获取cpu使用率 剩余内存和硬盘容量

时间:2021-01-02 11:29:47

相关推荐

android如何获取进程占用的内存大小 Android获取cpu使用率 剩余内存和硬盘容量

1.内存信息

在proc/meminfo下有详细的内存使用情况,我这里获取的内存信息就是从这个文件里获取的.获取到详细的内存信息后根据我自己的需求,从bufferdreader中单独抽取出来了剩余的内存容量.

Runtime runtime = Runtime.getRuntime();

Process p;

try {

p = runtime.exec(CMD_MEM);

} catch (IOException e) {

Log.e("CameraActivity", "run cmd("+CMD_MEM+") failed:" + e.getMessage());

return null;

}

InputStreamReader reader = new InputStreamReader(p.getInputStream());

BufferedReader buf = new BufferedReader(reader);

2.磁盘信息

使用Android.os下的StatFs来获取文件系统状态和一些磁盘信息.

File root = Environment.getRootDirectory();

StatFs sf = new StatFs(root.getPath());

long blockSize = sf.getBlockSize();

long availCount = sf.getAvailableBlocks();

return (availCount * blockSize) / 1024 / 1024 + "MB";

3.CPU使用率,和当前进程的CPU占有率

3.1 CPU总使用率

在proc/stat下有详细的CPU使用情况.详细格式如下:

CPU 152342 1421 28562 1600830 12389 553 273 0 0

CPU后面的几位数字分别是

user 从系统启动开始累计到当前时刻,处于用户态的运行时间,不包含 nice值为负进程。

nice 从系统启动开始累计到当前时刻,nice值为负的进程所占用的CPU时间

system 从系统启动开始累计到当前时刻,处于核心态的运行时间

idle

从系统启动开始累计到当前时刻,除IO等待时间以外的其它等待时间

iowait 从系统启动开始累计到当前时刻,IO等待时间

irq 从系统启动开始累计到当前时刻,硬中断时间

softirq 从系统启动开始累计到当前时刻,软中断时间

所以totalCpuTime这个7个属性的和.

CPU总数用率的算法是:100*((totalCpuTimeS-totalCpuTimeF) -(idelS-idelF))/ (totalCpuTimeS-totalCpuTim

eF)

3.2 当前进程的CPU使用率

/proc/pid/stat下则是该pid的CPU使用情况.详细格式如下:

2341 (cn.jesse.camera) S 1131 1131 0 0 -1 3912246 12450 0 2 0

3321 612 0 0 20 0

其中淡红色的四位数字分别是:

utime 该任务在用户运行状态的时间

stime 该任务在核心运行的时间

cutime 所有已死线程在用户状态运行状态的时间

cstime 所有已死线程在核心的运行时间

所以processCpuTime为这个四个属性的和.

当前进行所占CPU的算法是:100*(processCpuTimeS-processCpuTimeF)/(totalCpuTimeS-totalCpuTimeF)

String[] cpuInfos = null;

try{

BufferedReader reader = new BufferedReader(new InputStreamReader(

new FileInputStream("/proc/stat")), 1000);

String load = reader.readLine();

reader.close();

cpuInfos = load.split(" ");

}catch(IOException ex){

Log.e(TAG, "IOException" + ex.toString());

return 0;

}

long totalCpu = 0;

try{

totalCpu = Long.parseLong(cpuInfos[2])

+ Long.parseLong(cpuInfos[3]) + Long.parseLong(cpuInfos[4])

+ Long.parseLong(cpuInfos[6]) + Long.parseLong(cpuInfos[5])

+ Long.parseLong(cpuInfos[7]) + Long.parseLong(cpuInfos[8]);

}catch(ArrayIndexOutOfBoundsException e){

Log.i(TAG, "ArrayIndexOutOfBoundsException" + e.toString());

return 0;

}

String[] cpuInfos = null;

try{

int pid = android.os.Process.myPid();

BufferedReader reader = new BufferedReader(new InputStreamReader(

new FileInputStream("/proc/" + pid + "/stat")), 1000);

String load = reader.readLine();

reader.close();

cpuInfos = load.split(" ");

}catch(IOException e){

Log.e(TAG, "IOException" + e.toString());

return 0;

}

long appCpuTime = 0;

try{

appCpuTime = Long.parseLong(cpuInfos[13])

+ Long.parseLong(cpuInfos[14]) + Long.parseLong(cpuInfos[15])

+ Long.parseLong(cpuInfos[16]);

}catch(ArrayIndexOutOfBoundsException e){

Log.i(TAG, "ArrayIndexOutOfBoundsException" + e.toString());

return 0;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。