Android gets information such as total phone memory and available memory

Source: Internet
Author: User

Original address: http://www.cnblogs.com/helloandroid/articles/2210334.html

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>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 is a rough explanation of the fields listed in the "/proc/meminfo" file:

Memtotal: All available RAM sizes.

The sum of Memfree:lowfree and Highfree is left unused memory 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.

===========================================================================

The following direct detailed steps, we can according to the actual situation to expand accordingly.

1. New project, modify Main.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. Perfecting the Readsystemmemory.java class

 Packagecom.figo.readsyememory;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportJava.io.BufferedReader;ImportJava.io.FileReader;Importjava.io.IOException;ImportAndroid.app.ActivityManager;ImportAndroid.app.ActivityManager.MemoryInfo;ImportAndroid.content.Context;ImportAndroid.text.format.Formatter;ImportAndroid.util.Log;ImportAndroid.widget.TextView; Public classReadsystemmemoryextendsActivity {TextView TV=NULL;PrivateString Getavailmemory () {//get the current available memory size for AndroidActivitymanager am=(Activitymanager) Getsystemservice (Context.activity_service); Memoryinfo mi=Newmemoryinfo (); Am.getmemoryinfo (MI);//Mi.availmem; available memory for the current systemreturnFormatter.formatfilesize (Getbasecontext (), MI.AVAILMEM);//normalize the acquired memory size}Privatestring Gettotalmemory () {string str1= "/proc/meminfo";//system memory information fileString str2; String[] arrayofstring;LongInitial_memory = 0;Try{FileReader Localfilereader=NewFileReader (STR1); BufferedReader Localbufferedreader=NewBufferedReader (Localfilereader,8192); str2= Localbufferedreader.readline ();//read Meminfo First line, total system memory sizearrayofstring= Str2.split ("\\s+"); for(String num:arrayofstring) {log.i (str2, Num+ "\ T");} Initial_memory= Integer.valueof (Arrayofstring[1]). Intvalue () * 1024;//gets the total system memory, in kilobytes, multiplied by 1024 to bytelocalbufferedreader.close ();} Catch(IOException e) {}returnFormatter.formatfilesize (Getbasecontext (), initial_memory);//byte converted to KB or MB, memory size normalized}/**Called when the activity is first created.*/@Override Public voidonCreate (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. Running Results

Finished, successfully reading the total memory of the Android phone and the current available memory. 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

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.