Android obtains the total memory and available memory of the mobile phone. Android obtains the total memory and available memory of the mobile phone.

Source: Internet
Author: User
Android obtains the total memory and available memory of the mobile phone.

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.

1. Create a project and modify main. xml

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">
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textstyle = "bold"
Android: Id = "@ + ID/system_memory"/>
</Linearlayout>

2. Improve the readsystemmemory. Java class

Readsystemmemory. Java

Package 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 () {// gets the current available memory size of Android

Activitymanager AM = (activitymanager) getsystemservice (context. activity_service );
Memoryinfo MI = new memoryinfo ();
Am. getmemoryinfo (MI );
// Mi. availmem; available memory of the current system

Return formatter. formatfilesize (getbasecontext (), mi. availmem); // normalize the obtained memory size
}

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 the first line of meminfo, 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; // obtain the total system memory, measured in KB, multiplied by 1024 to byte
Localbufferedreader. Close ();

} Catch (ioexception e ){
}
Return formatter. formatfilesize (getbasecontext (), initial_memory); // convert byte to kb or MB, and normalize the memory size.
}

/** Called when the activity is first created .*/
@ Override
Public 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. Running result

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.

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.

1. Create a project and modify main. xml

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">
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textstyle = "bold"
Android: Id = "@ + ID/system_memory"/>
</Linearlayout>

2. Improve the readsystemmemory. Java class

Readsystemmemory. Java

Package 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 () {// gets the current available memory size of Android

Activitymanager AM = (activitymanager) getsystemservice (context. activity_service );
Memoryinfo MI = new memoryinfo ();
Am. getmemoryinfo (MI );
// Mi. availmem; available memory of the current system

Return formatter. formatfilesize (getbasecontext (), mi. availmem); // normalize the obtained memory size
}

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 the first line of meminfo, 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; // obtain the total system memory, measured in KB, multiplied by 1024 to byte
Localbufferedreader. Close ();

} Catch (ioexception e ){
}
Return formatter. formatfilesize (getbasecontext (), initial_memory); // convert byte to kb or MB, and normalize the memory size.
}

/** Called when the activity is first created .*/
@ Override
Public 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. Running result

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.

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.