Android gets information such as total phone memory and available memory

Source: Internet
Author: User

In Android development, sometimes we want to get some hardware information about the phone, such as the total memory and available memory size of the Android phone. How can this be achieved? By reading the file "/proc/meminfo" information can get the total amount of phone memory, and through Activitymanager.getmemoryinfo (Activitymanager.memoryinfo) method to get the current amount of memory available. The "/proc/meminfo" file records some of the memory information of the Android phone, enter "adb shell" in the Command Line window, enter the shell environment, enter "Cat/proc/meminfo" to display the contents of the Meminfo file on the command line. The details are as follows. C:\USERS\FIGO&GT;ADB shell# cat/proc/meminfocat/proc/meminfomemtotal:94096 kbmemfree:1684 kbbuffers:16 kbcached:271 kbswapcached:0 kbactive:35392 kbinactive:44180 kbactive (anon): 26540 kbinactive (anon): 28244 kbactive (file): 8852 KB Inactive (file): 15936 kbunevictable:280 kbmlocked:0 kbswaptotal:0 kbswapfree:0 kbdirty:0 kbwriteback:0 kBAnonPages: 52688 kbmapped:17960 kbslab:3816 kbsreclaimable:936 kbsunreclaim:2880 kbpagetables:5260 kbnfs_unstable:0 kbbounce:0 kbwritebacktmp:0 kbcommitlimit:47048 kbcommitted_as:1483784 kbvmalloctotal:876544 kbvmallocused:15456 KBVmallocChun k:829444 kb# The fields listed in the "/proc/meminfo" file are roughly explained below: Memtotal: All available RAM sizes. Memfree:lowfree and Highfree.Total, unused memory is kept by the system. Buffers: Used to buffer the file size. Cached: The size of the memory used by the cache memory (equal to DiskCache minus SwapCache). Swapcached: The size of the swap space used by the cache memory. The memory that has been swapped out is still stored in the Swapfile and is used to be replaced quickly when needed without having to open the I/O port again. Active: The size of the buffer or buffer memory paging file in active use, unless it is necessary, will not be moved for his use. Inactive: The size of the paging file in infrequently used buffers or cache memory may be used in other ways. Swaptotal: The total size of the swap space. Swapfree: The size of the swap space that is not being used. Dirty: The amount of memory waiting to be written back to disk. Writeback: The amount of memory that is being written back to disk. Anonpages: The memory size of the unmapped page. Mapped: Size of mappings such as devices and files. Slab: The size of the kernel data structure cache can reduce the consumption of application and release memory. Sreclaimable: The size of the slab can be retracted. Sunreclaim: The size of the Slab is not recoverable (Sunreclaim+sreclaimable=slab). Pagetables: The size of the index table that manages the paging page of memory. Nfs_unstable: The size of the page table is not stable. To get the total memory size of the Android phone, simply read the 1th line of the "/proc/meminfo" file and do a simple string processing. =========================================================================== below directly give detailed steps, we can according to the actual situation to expand accordingly. 1. New project, modify Main.xmlmain.xml<?xml version= "1.0" encoding= "Utf-8"? > <linearlayout xmlns:android= "http:/ /schemas.android.com/apk/res/android " android:orientation=" vertical " android:layout_width=" Fill_ Parent " android:layout_height="Fill_parent" > <textviewandroid:layout_width= "fill_parent"  android:layout_height= "wrap_content " android:textstyle=" bold " android:id=" @+id/system_memory "/> </linearlayout>2. Perfect Readsystemmemory.java class Readsystemmemory.javapackage Com.figo.readsyememory;import Android.app.Activity;import Android.os.bundle;import Java.io.bufferedreader;import Java.io.filereader;import Java.io.IOException;import Android.app.activitymanager;import Android.app.activitymanager.memoryinfo;import Android.content.Context;import Android.text.format.formatter;import Android.util.log;import Android.widget.textview;public class ReadSystemMemory Extends Activity {TextView TV = Null;private String getavailmemory () {//Get Android Current available memory size  activitymanager am = (Acti Vitymanager) Getsystemservice (Context.activity_service); Memoryinfo mi = new Memoryinfo (); Am.getmemoryinfo (mi);//mi.availmem; The available memory of the current system  return Formatter.formatfilesize (Getbasecontext (), mi.availmem);//Normalize the captured memory size andnbsp;} private String Gettotalmemory () {String str1 = "/proc/meminfo";//system memory information file  string str2; string[] Arrayofstring;long initial_memory = 0;try {FileReader Localfilereader = new FileReader (STR1); BufferedReader localbufferedreader = new BufferedReader (Localfilereader, 8192); str2 = Localbufferedreader.readline () ;//Read Meminfo first line, total system memory Size  arrayofstring = Str2.split ("\\s+"); for (String num:arrayofstring) {log.i (str2, num + "\ T" );} Initial_memory = integer.valueof (Arrayofstring[1]). Intvalue () * 1024;//get total system memory, in kilobytes, multiplied by 1024 to byte  Localbufferedreader.close ();} catch (IOException e) {}return formatter.formatfilesize (Getbasecontext (), initial_memory);//byte converted to KB or MB, memory size normalized}/ * * Called when the activity is first created. */  @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main); TV = (TextView) Findviewbyid (r.id.system_memory); Tv.settext ("Total Cell phone Memory:" + This.gettotalmemory () + "," + "Available Memory:"  + This.getavailMemory ());}} 3. The running result is complete, the total memory of the Android phone and the current available memory are read smoothly. Here is just a point, we can extrapolate, to expand. Of course we can also read "/proc/cupinfo" to get the CPU parameters of the Android phone, read "/proc/stat" file to calculate the CPU usage, here no longer repeat.

Android gets information such as total phone memory and available 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.