72 Get Memory information (number of processes running, total available memory, remaining memory) && resolution of bugs that get available total memory

Source: Internet
Author: User

Getting memory information (number of processes running, total available memory, remaining memory) is a tool method for the system, and the starting tool method is this:

Package Com.ustc.mobilemanager.utils;import Java.util.list;import Android.app.activitymanager;import Android.app.activitymanager.memoryinfo;import Android.app.activitymanager.runningappprocessinfo;import android.content.context;/** * System Information Tool class * * @author * */public class Systeminfoutils {/** * Get the number of running processes * * @param cont EXT * @return */public static int Getrunningprocesscount (context context) {Activitymanager am = (activitymanager) Context. Getsystemservice (Context.activity_service); list<runningappprocessinfo> infos = am.getrunningappprocesses (); return infos.size ();} /** * Get the remaining memory available on the phone * * @param context * @return */public static long Getavailmem (context context) {Activitymanager am = ( Activitymanager) Context.getsystemservice (Context.activity_service); Memoryinfo outinfo = new Memoryinfo (); Am.getmemoryinfo (outinfo); return outinfo.availmem;} /** * Get Phone total Memory * * @param context * @return */public static long Gettotalmem (context context) {Activitymanager am = (Activ Itymanager) ConteXt.getsystemservice (Context.activity_service); Memoryinfo outinfo = new Memoryinfo (); Am.getmemoryinfo (outinfo); return outinfo.totalmem;}}

So for the total number of running processes, the display needs to get the Activitymanager:activity manager, A Runningappprocessinfo list collection is obtained through the getrunningappprocesses of the activity manager, and the size of the collection is the total number of running processes that we need to know;

For obtaining the remaining memory:

The first is also obtained Activitymanager, using the Activitymanager Getmemoryinfo method, the Memoryinfo object is worn as his parameters, so that you can return memoryinfo field Availmem, This is the rest of the memory we need;

In the same way, to get the memory available, found that there is no totalmem this property, the program is the lowest compatible version to 16, I am, I actually have, and then in the activity call:

Package Com.ustc.mobilemanager;import Com.ustc.mobilemanager.utils.systeminfoutils;import android.app.Activity; Import Android.os.bundle;import Android.text.format.formatter;import Android.view.window;import Android.widget.textview;public class Taskmanageractivity extends Activity {/* * Total number of processes running */private TextView Tv_process_ count;/* * remaining/Total memory */private TextView tv_mem_info; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); SetContentView ( R.layout.activity_task_manager); tv_mem_info = (TextView) Findviewbyid (r.id.tv_mem_info); Tv_process_count = ( TextView) Findviewbyid (r.id.tv_process_count); int processcount = Systeminfoutils.getrunningprocesscount (this); tv_ Process_count.settext ("Running process:" + Processcount + "x"); Long Availmem = Systeminfoutils.getavailmem (this); Long Totalmem = Systeminfoutils.gettotalmem (this); Tv_mem_info.settext ("Remaining/Total Memory:" + formatter.formatfilesize (this, availmem) + "/" + Formatter.formAtfilesize (this, totalmem));}} 


One runs on the 4.0 phone, hangs up! Open Logcat to find the following error:



The error is: Nosuchfielderror.

So you need to rewrite this method, through the terminal into the phone shell, into the proc folder, view the Meminfo file, as follows:


We can view the source code under setting to see how Google wrote it:


/** * Get phone Total memory *  * @param context * @return long byte */public static long Gettotalmem (context context) {//Activityman Ager am = (activitymanager) context//. Getsystemservice (Context.activity_service);//Memoryinfo outInfo = new MemoryInfo ();//Am.getmemoryinfo (outinfo);////return outinfo.totalmem;try {File File = new file ("/proc/meminfo"); FileInputStream fis = new FileInputStream (file); BufferedReader br = new BufferedReader (new InputStreamReader (FIS)); String line = Br.readline ();  StringBuilder sb = new StringBuilder ();//memtotal:484792 Kbfor (char c:line.tochararray ()) {if (c >= ' 0 ' && C <= ' 9 ') {sb.append (c);}} Unit is byte convenient conversion return Long.parselong (sb.tostring ()) * 1024;} catch (Exception e) {e.printstacktrace (); return 0;}}

Just read the first line and then just intercept the numbers. Finally, in order to use Formatter.formatfilesize convenience, the final result is multiplied by 1024 for convenient conversion.

Operation Result:






72 Get Memory information (number of processes running, total available memory, remaining memory) && resolution of bugs that get available total memory

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.