1.内存信息

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

<span style="font-family:Microsoft YaHei;font-size:14px;">		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);</span>

2.磁盘信息

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

<span style="font-family:Microsoft YaHei;font-size:14px;">		File root = Environment.getRootDirectory();
StatFs sf = new StatFs(root.getPath());
long blockSize = sf.getBlockSize();
long availCount = sf.getAvailableBlocks();
return (availCount * blockSize) / 1024 / 1024 + "MB";</span>

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)

<span style="font-family:Microsoft YaHei;font-size:14px;">		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;
}</span>
<span style="font-family:Microsoft YaHei;font-size:14px;">		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;
}</span>

Android获取cpu使用率,剩余内存和硬盘容量的更多相关文章

  1. Qt linux获取cpu使用率、内存、网络收发速度、磁盘读写速度、磁盘剩余空间等

    #include "resource_minitor.h" #include "sys/statfs.h" resource_minitor::resource ...

  2. 简单获取cpu使用率,以及后台运行的问题

    做了一个运维平台,有一个功能定时执行一个脚本,获取cpu使用率和内存使用情况到监控平台. 获取cpu使用率使用的是top中的信息.直接运行没有问题.通过nohup xxx.sh & 之后获取不 ...

  3. [No0000112]ComputerInfo,C#获取计算机信息(cpu使用率,内存占用率,硬盘,网络信息)

    github地址:https://github.com/charygao/SmsComputerMonitor 软件用于实时监控当前系统资源等情况,并调用接口,当资源被超额占用时,发送警报到个人手机: ...

  4. Android获取cpu和内存信息、网址的代码

      android获取手机cpu并判断是单核还是多核 /** * Gets the number of cores available in this device, across all proce ...

  5. C# 获取Windows系统:Cpu使用率,内存使用率,Mac地址,磁盘使用率

    一.获取CPU使用率: #region 获取CPU使用率         #region AIP声明          [DllImport("IpHlpApi.dll")]   ...

  6. 使用python函数持续监控电脑cpu使用率、内存、c盘使用率等

    方法一: # import time 导入time模块 # import psutil 导入psutil模块 # def func(): # while True: ------->持续监控得w ...

  7. linux下实现CPU使用率和内存使用率获取方法

    想获取一下目标机运行时linux系统的硬件占用情况,写了这几个小程序,以后直接用了. 方法就是读取proc下的文件来获取了. cpu使用率:    /proc/stat ,内存使用情况:     /p ...

  8. 获取Windows操作系统的CPU使用率以及内存使用率

    此功能参考了ProcessHacker项目的代码. 声明定义 typedef struct _UINT64_DELTA { ULONG64 Value; ULONG64 Delta; } UINT64 ...

  9. Golang利用第三方包获取本机cpu使用率以及内存使用情况

    第三方包下载 $ github.com/shirou/gopsutil 获取内存方面的信息 package main import ( "fmt" "github.com ...

随机推荐

  1. 【Python初级】StringIO和BytesIO读写操作的小思考

    from io import StringIO; f = StringIO(); f.write('Hello World'); s = f.readline(); print s; 上面这种方法“无 ...

  2. RxSwift 系列(三)

    RxSwift 系列(三) -- Combination Operators 前言 本篇文章将要学习如何将多个Observables组合成一个Observable.Combination Operat ...

  3. [BZOJ4517][SDOI2016]排列计数(错位排列)

    4517: [Sdoi2016]排列计数 Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 1616  Solved: 985[Submit][Statu ...

  4. BZOJ 1497 JZYZOJ 1344 [NOI2006]最大获利 网络流 最大权闭合图

    http://www.lydsy.com/JudgeOnline/problem.php?id=1497 http://172.20.6.3/Problem_Show.asp?id=1344   思路 ...

  5. Codeforces 839E Mother of Dragons(极大团)

    [题目链接] http://codeforces.com/contest/839/problem/E [题目大意] 现在有一些点,现在你有k的液体,随意分配给这些点, 当两个点有边相连的时候,他们能产 ...

  6. ms08-067漏洞的复现

    MS08-067漏洞重现 (1):MS08-067远程溢出漏洞描述 MS08-067漏洞的全称为“Windows Server服务RPC请求缓冲区溢出漏洞”,如果用户在受影响的系统上收到特制的 RPC ...

  7. asp.net 去除数据中带有的html标签

    1,在控制器中实现去除html标签的静态方法 //去除html标签 public static string ReplaceHtmlMark(object Contents) { string Htm ...

  8. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) C. Bear and Up-Down 暴力

    C. Bear and Up-Down 题目连接: http://www.codeforces.com/contest/653/problem/C Description The life goes ...

  9. centos安装jdk文件

    1.到oracle官网选择要安装的jdk版本 http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html ...

  10. 移动应用安全开发指南(Android)--数据验证

    概述 移动应用往往通过数据的发送.接收和处理来完成一系列功能,通常情况下,处理的数据绝大部分都来源于外部(比如网络.内部或外部存储和用户输入等),对这些数据处理不当会导致各种各样的漏洞和风险,比代码执 ...