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. CSS3之背景定位原点background-origin

    背景定位原点background-origin是CSS3新添加的有关背景的属性,主要是改变背景起始的原点位置的.CSS3之背景定位原点background-origin的属性值有三个:border.p ...

  2. MySQL表设计:每一种商品有不确定个数的属性

    I personally would use a model similar to the following: The product table would be pretty basic, yo ...

  3. 【fastadmin】 _id 功能失效解决办法

    在add.html模版中修改对应的input标签的各种属性

  4. Python画一朵花

    from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import Line ...

  5. TCP/IP——IP网络协议简记

    IP提供不可靠.无连接的数据报传送服务 不可靠:不保证IP数据报能成功到达目的地,当发生错误时,IP的做法是丢弃这个数据报,然后发送ICMP消息报给信息源. 无连接:IP不维护任何关于后续数据报的状态 ...

  6. Inno Setup Winfrom 打包工具

    1.下载并安装Inno Setup 编译器 2.安装完成后打开Inno Setup 编译器: 3.选择[用「脚本向导」创建新的脚本文件(S)]: 4.点击[下一步]: 5.填写制作后程序的基本信息: ...

  7. POJ 3228 [并查集]

    题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...

  8. hiho1291(逆序思维,并查集)

    题目链接:[https://hihocoder.com/problemset/problem/1291] 题意:在<我的世界>游戏中放置沙盒,沙盒为体积为1的正方体,按顺序给你一些坐标,然 ...

  9. 【BZOJ 2039】 2039: [2009国家集训队]employ人员雇佣 (最小割)

    2039: [2009国家集训队]employ人员雇佣 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 1511  Solved: 728 Descri ...

  10. CodeForces - 1017D The Wu

    题面在这里! 比较显而易见的暴力,O(2^(2n) + 2^n * 100) 就可以直接做了 #include<bits/stdc++.h> #define ll long long us ...