Several methods of obtaining performance indicators commonly used in Android (pure code)

Source: Internet
Author: User

Share some of the code you've used to get some performance metrics for app performance

Get the corresponding process memory consumption information:

1      Public voidMemmonitor () {2 3 4        NewThread () {5                 Public voidrun () {6                    FinalActivitymanager activitylist=(Activitymanager) Getapplicationcontext (). Getsystemservice (Activity_service);7                    Try {8Runtime.getruntime (). EXEC ("AM start--user 0-n package name/mainactivity name");9Starttime=System.currenttimemillis ();Ten  One}Catch(IOException e) { ALOG.E ("Baih", "Start app failed"); -                    } -                     while(true) { theLOG.E ("Baih", "Start calculation ..."); -Activitymanager.memoryinfo Mem1 =Newactivitymanager.memoryinfo (); - Activitylist.getmemoryinfo (Mem1); -Long memsize =Mem1.availmem; +Long mem12=Mem1.threshold; -String leftmemsize =formatter.formatfilesize (Getbasecontext (), memsize); +String leftMemSize1 =formatter.formatfilesize (Getbasecontext (), MEM12); A  at                        //get the controlled limit heap value, get the non-controlled limit heap value -                        intI=activitylist.getmemoryclass ();//controlled Heap -                        intY=activitylist.getlargememoryclass ();//non-controlled heap -  -                        //Calculate process Memory consumption -List<activitymanager.runningappprocessinfo> applist =activitylist.getrunningappprocesses (); in                         for(Activitymanager.runningappprocessinfo am:applist) { -                            if(Am.processName.equals ("Package name"))) { to                                LongStartedtime=System.currenttimemillis (); +                                Longtime=startedtime-StartTime; -LOG.E ("Baih", "Start Time:" +time+ "MS"); the                                int[] cmpid=New int[]{am.pid}; *debug.memoryinfo[] Meminfo =Activitylist.getprocessmemoryinfo (cmpid); $Apppid=Am.pid;Panax Notoginseng                                DoubleMemorySize1 = meminfo[0].dalvikprivatedirty/1024.0; -                                inttemp = (int) (MEMORYSIZE1 * 100); theMemorySize1 = temp/100.0; +String Proinfo = ""; AProinfo + = "Native Memory threshold:" + i + "mb\n" + the"Package Name:" + am.processname + "+" \npid: "+Am.pid ++ "+" \ n memory consumption: "+ MemorySize1 +" mb\n "+" \ r \ n "; -LOG.E ("Baih", proinfo); $                                if(apppid!=0) $                                { -Cpuinfo ();//Call Compute CPU usage function -                                } the  -Mem1 =NULL;Wuyi System.GC (); the                                 Break; -}Else { WuLOG.E ("Baih", "app not Running"); -Mem1 =NULL; About System.GC (); $                                 Break; -                            } -                        } -                        Try { AThread.Sleep (5000); +}Catch(interruptedexception e) { the                            //TODO auto-generated Catch block - e.printstacktrace (); $                        } the                    } the                } the }.start (); the  -}

Get Process CPU Usage:

    //Calculate total CPU usage time    Private Static LongGettotalcputime () {//get total system CPU usage timestring[] Cpuinfos =NULL; Try{BufferedReader Reader=NewBufferedReader (NewInputStreamReader (NewFileInputStream ("/proc/stat")), 1000); String Load=Reader.readline ();            Reader.close (); Cpuinfos= Load.split (""); } Catch(IOException ex) {ex.printstacktrace (); }        LongTOTALCPU = 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]) +long.parselong (cpuinfos[9]) +long.parselong (cpuInfos[ 10]); //log.e ("Baih", "Total CPU Usage Time:" + totalcpu + "======" + op);        returntotalcpu; }    //CPU time used by the calculation process    Private Static LongGetappcputime () {//get the CPU time that the app consumesstring[] Cpuinfos =NULL; Try{BufferedReader Reader=NewBufferedReader (NewInputStreamReader (NewFileInputStream ("/proc/" +apppid + "/stat")), 1000); String Load=Reader.readline ();            Reader.close (); Cpuinfos= Load.split (""); } Catch(IOException ex) {ex.printstacktrace (); }        LongAppcputime = Long.parselong (cpuinfos[13])                + Long.parselong (cpuinfos[14]) + Long.parselong (cpuinfos[15])                + Long.parselong (cpuinfos[16]); //log.e ("Baih", "app uses CPU time:" + appcputime);        returnAppcputime; }    //calculation process consumes CPU rate     Public voidCpuinfo () {//Calculate CPU usage        NewThread () { Public voidrun () {LongTOTALCPU1 =Gettotalcputime (); LongAPPCPU1 =Getappcputime (); Try{Thread.Sleep (3000); } Catch(interruptedexception e) {e.printstacktrace (); }                    LongTOTALCPU2 =Gettotalcputime (); LongAPPCPU2 =Getappcputime (); LongCpuInfo = (APPCPU2-APPCPU1)/(TOTALCPU2-TotalCpu1); LOG.E ("Baih", "CPU utilization =" + CpuInfo + "%"); Try{Thread.Sleep (3000); } Catch(interruptedexception e) {e.printstacktrace ();    }}}.start (); }

Get app Startup time:

     Public voidStartTime () {//Calculate start TimeActivitymanager ac1=(Activitymanager) Getapplicationcontext (). Getsystemservice (Activity_service); List<ActivityManager.RunningAppProcessInfo> applist1=ac1.getrunningappprocesses ();  while(true) {             for(Activitymanager.runningappprocessinfo mc1:applist1) {if(Mc1.processName.equals ("Package name")) ) {Startedtime=System.currenttimemillis (); Continue; } Else{starttime=System.currenttimemillis (); }} Time= Startedtime-StartTime; LOG.E ("Baih", "start at:" + Time + "MS"); }    }

Several methods of obtaining performance indicators commonly used in Android (pure code)

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.