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 ;}