Android gets CPU usage, remaining memory and hard disk capacity

Source: Internet
Author: User

1. Memory Information

With detailed memory usage under Proc/meminfo, the memory information I get here is obtained from this file. After obtaining the detailed memory information, the remaining memory capacity is extracted from the bufferdreader separately according to my own requirements.

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

Use the Statfs under Android.os to get the file system status and some disk information.

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

3.1 Total CPU Usage

There is detailed CPU usage under PROC/STAT. The detailed format is as follows:

CPU 152342 1421 28562 1600830 12389 553 273 0 0

The numbers behind the CPU are

  user      &NBSP; nice &NBSP;

   nice       Span style= "color: #000000; Word-break:break-all ">nice cpu time

   system       from the start of the system start up to the current moment, Run time in nuclear mindset &NBSP;

   idle &NBSP;       Accumulates from the start of the system to the current moment, except for io &NBSP;

   iowait      Span style= "color: #000000; Word-break:break-all ">io wait time &NBSP;

IRQ accumulated from the start of the system to the current moment, hard interrupt time

Softirq accumulated from the start of the system to the current moment, soft interrupt time

So totalcputime this 7 attributes of the and.

The algorithm for total CPU utilization is: 100* ((TOTALCPUTIMES-TOTALCPUTIMEF)-(idels-idelf))/ (Totalcputimes-totalcputimEF)


3.2 CPU utilization of the current process

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

2341 (Cn.jesse.camera) S 1131 1131 0 0-1 3912246 12450 0 2 0 3321 612 0 0 0

The four digits of the light red are:

Utime when the task is running in the user's state

Stime the time the task is running in the core

Cutime the time that all dead threads are running in user state

Cstime uptime of all dead threads at core

so processcputime for this four attributes of the and.

                  The current algorithm for the CPU occupied is: 100* (processcputimes-PROCESSCPUTIMEF )/ (TOTALCPUTIMES-TOTALCPUTIMEF)

<span style= "Font-family:microsoft yahei;font-size:14px;" >string[] Cpuinfos = null;try{bufferedreader reader = new BufferedReader (New InputStreamReader                   FileInputStream ("/proc/stat"));        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"), 100 0);        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 gets CPU usage, remaining memory and hard disk capacity

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.