How does Android calculate the percentage of memory used currently?

Source: Internet
Author: User

How does Android calculate the percentage of memory used currently?

Some Mobile Phone Manager software can display the percentage of memory used by the current system. This number is calculated by the ratio of the total memory size, the available memory size, and the total memory size, the following code calculates the memory usage.

 

/*** Calculate the percentage of memory in use **/public static String getUsedPercentValue (Context context) {String dir = "/proc/meminfo "; try {FileReader fr = new FileReader (dir); BufferedReader br = new BufferedReader (fr, 2048); String memoryLine = br. readLine (); String subMemoryLine = memoryLine. substring (memoryLine. indexOf ("MemTotal:"); br. close (); long totalMemorySize = Integer. parseInt (subMemoryLine. replaceAll ("\ D +", ""); l Ong availableSize = getAvailableMemory (context)/1024; int percent = (int) (totalMemorySize-availableSize)/(float) totalMemorySize * 100); return percent + "% ";} catch (IOException e) {e. printStackTrace ();} return "no result";}/*** get the current available memory. The returned data is in bytes. * @ Return the current available memory. */Private static long getAvailableMemory (Context context) {ActivityManager. MemoryInfo mi = new ActivityManager. MemoryInfo (); getActivityManager (context). getMemoryInfo (mi); return mi. availMem ;}


 

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.