Obtain the total memory and available memory of the Android phone

Source: Internet
Author: User

In Android development, sometimes we want to obtain some hardware information about the mobile phone, such as the total memory and available memory size of the Android phone. How can this be implemented?
By reading the "/proc/meminfo" file, you can obtain the total memory volume on your mobile phone. By using activitymanager. getmemoryinfo (activitymanager. memoryinfo), you can obtain the current available memory volume.
The "/proc/meminfo" file records some memory information of the Android mobile phone. In the command line window, enter "ADB shell" to enter the shell environment, enter "cat/proc/meminfo" to display the content of the meminfo file in the command line, as shown below.

C:/users/Figo> ADB Shell
# Cat/proc/meminfo
CAT/proc/meminfo
Memtotal: 94096 KB
Memfree: 1684 KB
Buffers: 16 KB
Cached: 27160 KB
Swapcached: 0 KB
Active: 35392 KB
Inactive: 44180 KB
Active (Anon): 26540 KB
Inactive (Anon): 28244 KB
Active (File): 8852 KB
Inactive (File): 15936 KB
Unevictable: 280 KB
Mlocked: 0 KB
Swaptotal: 0 KB
Swapfree: 0 KB
Dirty: 0 KB
Writeback: 0 KB
Anonpages: 52688 KB
Mapped: 17960 KB
Slab: 3816 KB
Sreclaimable: 936 KB
Sunreclaim: 2880 KB
Pagetables: 5260 KB
Nfs_unstable: 0 KB
Bounce: 0 KB
Writebacktmp: 0 KB
Commitlimit: 47048 KB
Committed_as: 1483784 KB
Vmalloctotal: 876544 KB
Vmallocused: 15456 KB
Vmallocchunk: 829444 KB
#

The following describes the fields listed in the/proc/meminfo file:

Memtotal: The size of all available Ram.

Memfree: Sum of lowfree and highfree, which is reserved by the system for unused memory.

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 swap space used by cache memory. The memory that has been swapped out is still stored in swapfile, which is used to quickly replace it as needed without opening the I/O port again.

Active: The size of page files in active or high-speed buffer memory. It is not moved for other purposes unless necessary.

Inactive: The size of page files in infrequently used buffer or high-speed buffer storage, which may be used in other ways.

Swaptotal: the total size of swap space.

Swapfree: The size of swap space not used.

Dirty: the size of the memory to be written back to the disk.

Writeback: the memory size that is being written back to the disk.

Anonpages: memory size of pages not mapped.

Mapped: The ing size of devices and files.

Slab: the size of the kernel data structure cache, which can reduce the consumption caused by memory application and release.

Sreclaimable: the slab size can be withdrawn.

Sunreclaim: The size of Slab (sunreclaim + sreclaimable = slab ).

Pagetables: Manage the size of the index table on the memory paging page.

Nfs_unstable: the size of the unstable page table.

To obtain the total memory size of the Android phone, you only need to read the 1st lines of the "/proc/meminfo" file and perform simple string processing.

The following describes the detailed steps. You can expand the parameters based on the actual situation.
Step 1: Create an android project named readsystemmemory, as shown in.

 

Step 2: Modify the main. xml file in the layout directory. The Code is as follows:<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: textstyle = "bold" <br/> Android: id = "@ + ID/system_memory"/> <br/> </linearlayout>Step 3: complete the readsystemmemory class with the following code:Package COM. figo. readsyememory; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import Java. io. bufferedreader; <br/> Import Java. io. filereader; <br/> Import Java. io. ioexception; <br/> Import android. app. activitymanager; <br/> Import android. app. activitymanager. memoryinfo; <br/> Import android. content. context; <br/> Import android. text. format. formatter; <br/> Import android. util. log; <br/> Import android. widget. textview; </P> <p> public class readsystemmemory extends activity {</P> <p> textview TV = NULL; </P> <p> private string getavailmemory () {// obtain the current available memory size of Android </P> <p> activitymanager AM = (activitymanager) getsystemservice (context. activity_service); <br/> memoryinfo MI = new memoryinfo (); <br/> AM. getmemoryinfo (MI); <br/> // mi. availmem; available memory of the current system </P> <p> return formatter. formatfilesize (getbasecontext (), MI. availmem); // normalize the obtained memory size <br/>}</P> <p> private string gettotalmemory () {<br/> string str1 = "/proc/meminfo"; // system memory information file <br/> string str2; <br/> string [] arrayofstring; <br/> long initial_memory = 0; </P> <p> try {<br/> filereader localfilereader = new filereader (str1 ); <br/> bufferedreader localbufferedreader = new bufferedreader (<br/> localfilereader, 8192); <br/> str2 = localbufferedreader. readline (); // read the first line of meminfo, total system memory size </P> <p> arrayofstring = str2.split ("/S + "); <br/> for (string num: arrayofstring) {<br/> log. I (str2, num + "/t"); <br/>}</P> <p> initial_memory = integer. valueof (arrayofstring [1]). intvalue () * 1024; // obtain the total system memory, in KB, multiplied by 1024 to byte <br/> localbufferedreader. close (); </P> <p >}catch (ioexception e) {<br/>}< br/> return formatter. formatfilesize (getbasecontext (), initial_memory); // convert byte to kb or MB, memory size normalization <br/>}</P> <p>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> TV = (textview) findviewbyid (R. id. system_memory); <br/> TV. settext ("total phone memory:" + this. gettotalmemory () + "," + "available memory:" <br/> + this. getavailmemory (); <br/>}< br/>Step 4: run the project. The result is as follows:

 

The success is that the total memory and current available memory of the Android mobile phone are successfully read. This is just an example. You can expand it in the opposite way. Of course, we can also read "/proc/cupinfo" to obtain the CPU parameters of the Android phone, and read the "/proc/STAT" file to calculate the CPU usage, which is not described here.

I am a newbie and I am new to Android. It is inevitable that I write a blog post for the first time. You are welcome to correct me and make progress together.

 

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.