android性能1-内存、CPU获取
2016-06-15 16:15
369 查看
内存
通过读取文件”/proc/meminfo”的信息能够获取手机Memory的总量,而通过ActivityManager.getMemoryInfo(ActivityManager.MemoryInfo)方法可以获取当前的可用Memory量。获取总内存,剩余内存,pid内存
public long getTotalMemory() { String memInfoPath = "/proc/meminfo"; String readTemp = ""; String memTotal = ""; long memory = 0; try { FileReader fr = new FileReader(memInfoPath); BufferedReader localBufferedReader = new BufferedReader(fr, 8192); while ((readTemp = localBufferedReader.readLine()) != null) { if (readTemp.contains("MemTotal")) { String[] total = readTemp.split(":"); memTotal = total[1].trim(); } } localBufferedReader.close(); String[] memKb = memTotal.split(" "); memTotal = memKb[0].trim(); Log.d(LOG_TAG, "memTotal: " + memTotal); memory = Long.parseLong(memTotal); } catch (IOException e) { Log.e(LOG_TAG, "IOException: " + e.getMessage()); } return memory; } public long getFreeMemorySize(Context context) { ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo(); ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); am.getMemoryInfo(outInfo); long avaliMem = outInfo.availMem; return avaliMem / 1024; } public int getPidMemorySize(int pid, Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); int[] myMempid = new int[] { pid }; Debug.MemoryInfo[] memoryInfo = am.getProcessMemoryInfo(myMempid); memoryInfo[0].getTotalSharedDirty(); int memSize = memoryInfo[0].getTotalPss(); return memSize; }
CPU
同样的,通过通过读取文件”/proc/cpuinfo”和”/proc/pid/stat”的信息能够获取手机Cpu的总量,pid应用占用率获取总cpu和pid应用cpu
public void readCpu(int pid) { String processPid = Integer.toString(pid); String cpuStatPath = "/proc/" + processPid + "/stat"; try { // monitor cpu stat of certain process RandomAccessFile processCpuInfo = new RandomAccessFile(cpuStatPath , "r"); String line = ""; StringBuffer stringBuffer = new StringBuffer(); stringBuffer.setLength(0); while ((line = processCpuInfo.readLine()) != null) { stringBuffer.append(line + "\n"); } String[] tok = stringBuffer.toString().split(" "); processCpu = Long.parseLong(tok[13]) + Long.parseLong(tok[14]); processCpuInfo.close(); } catch (FileNotFoundException e) { Log.w(LOG_TAG, "FileNotFoundException: " + e.getMessage()); } catch (IOException e) { e.printStackTrace(); } try { // monitor total and idle cpu stat of certain process RandomAccessFile cpuInfo = new RandomAccessFile("/proc/cpuinfo", "r"); String line = ""; while ((null != (line = cpuInfo.readLine())) && line.startsWith("cpu")) { String[] toks = line.split("\\s+"); idleCpu.add(Long.parseLong(toks[4])); totalCpu.add(Long.parseLong(toks[1]) + Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4]) + Long.parseLong(toks[6]) + Long.parseLong(toks[5]) + Long.parseLong(toks[7])); } cpuInfo.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories