Android Get memory information

Source: Internet
Author: User

As a result of the work needed, the method of acquiring memory information on Android was studied, and summarized as follows:

1.SDK get

The Java layer takes advantage of the API to get very simple, directly using the Activitymanager.memoryinfo class, the code is as follows:

        Activitymanager activitymanager=(Activitymanager) Getsystemservice (context.activity_service);            Activitymanager.memoryinfo meminfo=new  activitymanager.memoryinfo ();        Activitymanager.getmemoryinfo (meminfo);        LOG.V ("Meminfo", "Availmem:" +meminfo.availmem/1024+ "KB");        LOG.V ("Meminfo", "Threshold:" +meminfo.threshold/1024+ "KB"); // Low memory threshold        LOG.V ("Meminfo", "Totalmem:" +meminfo.totalmem/1024+ "KB");        LOG.V ("Meminfo", "Lowmemory:" +meminfo.lowmemory);  // if current was in the low memory

2.NDK get

In the native layer to get memory information Java layer comparison, Android does not provide the corresponding API (I did not find, if a master found, welcome message). Considering that the Android system is modified based on the Linux system, there is a/proc/meminfo file to store the current memory information. This file contains a lot of content, after executing the ADB shell command on the PC, enter Cat/proc/meminfo, the following information will be displayed:

Generally we are only interested in available memory and total memory, referring to the code on the web itself encapsulates two functions as follows, if you want to get additional information, and so on, the code is as follows:

//get the available memory in KB, return-1 if get failedLongGetavailmem () {SignedLongAvailmem=-1; intMeminfofile = open ("/proc/meminfo", o_rdonly); if(Meminfofile < 0)    {returnAvailmem;} Charbuffer[256]; Const intLen = Read (meminfofile, buffer, sizeof (buffer)-1);    Close (Meminfofile); if(Len < 0)    {returnAvailmem;} Buffer[len]= 0; intNumfound = 0; Static Const Char*ConstSums[] = {"Memfree:", "Cached:", NULL}; Static Const intSumslen[] = {strlen ("Memfree:"), strlen ("Cached:"), 0 }; Char* p =buffer;  while(*p && Numfound < 2)    {        inti = 0;  while(Sums[i]) {if(STRNCMP (P, Sums[i], sumslen[i]) = = 0) {p+=Sumslen[i];  while(*p = = ") p++; Char* num =p;  while(*p >= ' 0 ' && *p <= ' 9 ') p++; if(*p! = 0)                {                    *p = 0; P++; if(*p = = 0) p--; } Availmem+=Atoll (num); Numfound++;  Break; } I++; } P++; }    returnAvailmem;}//get the total memory in KB, return-1 if get failedLongGettotalmem () {SignedLongTotalmem=-1; intMeminfofile = open ("/proc/meminfo", o_rdonly); if(Meminfofile < 0)    {returnTotalmem;} Charbuffer[256]; Const intLen = Read (meminfofile, buffer, sizeof (buffer)-1);    Close (Meminfofile); if(Len < 0)    {returnTotalmem;} Buffer[len]= 0; Static Const Char*ConstSums[] = {"Memtotal:", NULL}; Static Const intSumslen[] = {strlen ("memtotal:"), 0 }; Char* p =buffer;  while(*p) {inti = 0;  while(Sums[i]) {if(STRNCMP (P, Sums[i], sumslen[i]) = = 0) {p+=Sumslen[i];  while(*p = = ") p++; Char* num =p;  while(*p >= ' 0 ' && *p <= ' 9 ') p++; if(*p! = 0)                {                    *p = 0; P++; if(*p = = 0) p--; } Totalmem+=Atoll (num);  Break; } I++; } P++; }    returnTotalmem;}

Explain why the available memory Availmem is the sum of memfree and cached, memfree refers to completely unused memory, cached means that when you read and write files, the Linux kernel caches the files in memory in order to improve read and write performance and speed. That is, cache memory. The Cache memory will not be released automatically even after your program has finished running. This will cause you to read and write files frequently in your Linux system, and you will find that there is little physical memory available. In fact, this cache memory is automatically released when you need to use memory, so you don't have to worry about not having memory available. So available memory availmem=memfree+cached

3. References

Http://blog.chinaunix.net/uid-9465077-id-270364.html

Http://www.xuebuyuan.com/1878297.html

Http://www.ha97.com/4337.html

Http://developer.android.com/intl/zh-cn/reference/android/app/ActivityManager.MemoryInfo.html

Android Get memory information

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.