Android obtains cpu usage, remaining memory, and hard disk capacity

Source: Internet
Author: User

Android obtains cpu usage, remaining memory, and hard disk capacity

1. Memory Information

Detailed memory usage is available in proc/meminfo. The memory information I obtain here is obtained from this file. after obtaining detailed memory information, I extracted the remaining memory capacity from bufferdreader based on my own needs.

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. Disk Information

Use StatFs in Android. OS to obtain the file system status and disk information.

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 usage, and CPU usage of the current process

3.1 Total CPU usage

Detailed CPU usage In proc/stat. The detailed format is as follows:

CPU 152342 1421 28562 1600830 12389 553 273 0 0

The number of digits after the CPU is

The running time of the user from the start of the system to the current time. The nice value is not included in the process.

The CPU time occupied by the nice process from system startup to the current time.

The running time of the system in the core State is accumulated from the start of the system to the current time.

Idle accumulates from the start of the system to the current time, except for the IO wait time.

Iowait accumulates from system startup to current time, IO wait time

Irq accumulates from system startup to current time, hard interruption time

Softirq accumulates from the system startup to the current time, Soft Interrupt time

So the sum of the seven attributes of totalCpuTime.

The total CPU usage algorithm is as follows: 100 * (totalCpuTimeS-totalCpuTimeF)-(idelS-idelF)/(totalCpuTimeS-totalCpuTimeF)


3.2 CPU usage of the current process

The CPU usage of the pid is shown in/proc/pid/stat. The detailed format is as follows:

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

The four highlighted numbers are:

Utime the time when the task is in the user's running status

Stime the time when the task runs at the core

Cutime the time when all dead threads are running in the user State

Running time of all dead threads in the core of cstime

So processCpuTime is the sum of the four attributes.

The CPU occupied currently is calculated as follows: 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;}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.