How Android calculates the percentage of memory currently in use

Source: Internet
Author: User

Some mobile phone butler software can show the current system memory usage of how much, this number is obtained by the total memory size, minus the available memory size, and then the total size of the results, the following code can calculate how much memory used.

/** * Calculates the percentage of memory used * */public static string Getusedpercentvalue (context context) {String dir = "/proc/meminfo"; try {Filerea Der 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+", "")); Long availablesize = getavailablememory (context)/1024;int Percent = (int) ((totalmemorysize-availablesize)/(float) totalmemorysize *); return percent + "%";} catch (IOException e) {e.printstacktrace ();} Return "no results";} /** * Gets the current available memory and returns the data in bytes.  * @return currently 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.