How to obtain process memory usage by Android

Source: Internet
Author: User

ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();activityManager.getMemoryInfo(memoryInfo);Log.i(TAG, " memoryInfo.availMem " + memoryInfo.availMem + "n" );Log.i(TAG, " memoryInfo.lowMemory " + memoryInfo.lowMemory + "n" );Log.i(TAG, " memoryInfo.threshold " + memoryInfo.threshold + "n" );List<RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();Map<Integer, String> pidMap = new TreeMap<Integer, String>();for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses){    pidMap.put(runningAppProcessInfo.pid, runningAppProcessInfo.processName);}Collection<Integer> keys = pidMap.keySet();for(int key : keys){    int pids[] = new int[1];    pids[0] = key;    android.os.Debug.MemoryInfo[] memoryInfoArray = activityManager.getProcessMemoryInfo(pids);    for(android.os.Debug.MemoryInfo pidMemoryInfo: memoryInfoArray)    {        Log.i(TAG, String.format("** MEMINFO in pid %d [%s] **n",pids[0],pidMap.get(pids[0])));        Log.i(TAG, " pidMemoryInfo.getTotalPrivateDirty(): " + pidMemoryInfo.getTotalPrivateDirty() + "n");        Log.i(TAG, " pidMemoryInfo.getTotalPss(): " + pidMemoryInfo.getTotalPss() + "n");        Log.i(TAG, " pidMemoryInfo.getTotalSharedDirty(): " + pidMemoryInfo.getTotalSharedDirty() + "n");    }}

Before seeing the above code, I wrote similar code according to the API instructions as follows, including more output parameters. If you want to check the memory usage of other processes, you can skip the condition judgment in the loop. According to the post in stackoverflow.com, the value of Pss indicates the memory usage of the process.

Public long getmem_SELF () {ActivityManager am = (ActivityManager) getSystemService (Context. ACTIVITY_SERVICE); List <RunningAppProcessInfo> procInfo = am. getRunningAppProcesses (); for (RunningAppProcessInfo runningAppProcessInfo: procInfo) {System. out. println (runningAppProcessInfo. processName + String. format (", pid = % d", runningAppProcessInfo. pid); if (runningAppProcessInfo. processName. indexOf (thi S. getPackageName ())! =-1) {int pids [] = {runningAppProcessInfo. pid}; Debug. memoryInfo self_mi [] = am. getProcessMemoryInfo (pids); StringBuffer strbuf = new StringBuffer (); strbuf. append ("proccess Name :"). append (runningAppProcessInfo. processName ). append ("n pid :"). append (runningAppProcessInfo. pid ). append ("n dalvikPrivateDirty :"). append (self_mi [0]. dalvikPrivateDirty ). append ("n dalvikPss :"). append (self_mi [0]. dalvikPs S ). append ("n dalvikSharedDirty :"). append (self_mi [0]. dalvikSharedDirty ). append ("n nativePrivateDirty :"). append (self_mi [0]. nativePrivateDirty ). append ("n nativePss :"). append (self_mi [0]. nativePss ). append ("n nativeSharedDirty :"). append (self_mi [0]. nativeSharedDirty ). append ("n otherPrivateDirty :"). append (self_mi [0]. otherPrivateDirty ). append ("n otherPss :"). append (self_mi [0]. otherPss ). append ("n othe RSharedDirty :"). append (self_mi [0]. otherSharedDirty ). append ("n TotalPrivateDirty :"). append (self_mi [0]. getTotalPrivateDirty ()). append ("n TotalPss :"). append (self_mi [0]. getTotalPss ()). append ("n TotalSharedDirty :"). append (self_mi [0]. getTotalSharedDirty (); Log. v ("TEST", strbuf. toString ());}} <div class = "dp-highlighter bg_java"> <div class = "bar"> <div class = "tools"> <B> [java] </B> <a href = "#" class = "ViewS Ource "title =" view plain "onclick =" dp. sh. toolbar. command ('viewsource', this); return false; "> view plain </a> <a href =" # "class =" CopyToClipboard "title =" copy onclick = "dp. sh. toolbar. command ('copytoclipboard', this); return false; "> copy </a> <a href =" # "class =" PrintSource "title =" print "onclick =" dp. sh. toolbar. command ('printsource', this); return false; "> print </a> <a href =" # "class =" About "title = "? "Onclick =" dp. sh. Toolbar. Command ('about', this); return false; ">? </A> <div style = "position: absolute; left: 0px; top: 0px; width: 0px; height: 0px; z-index: 99;> <embed id = "ZeroClipboardMovie_3" src = "http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop = "false" menu = "false" quality = "best" bgcolor = "# ffffff" width = "0" height = "0" name = "ZeroClipboardMovie_3" align = "middle" allowscriptaccess = "always" allowfullscreen = "false" type = "application/x-sho Ckwave-flash "pluginspage =" http://www.macromedia.com/go/getflashplayer "flashvars =" id = 3 & width = 0 & height = 0 "wmode =" transparent "> </div>/ div> <ol start = "1" class = "dp-j"> <li class = "alt"> <span class = "keyword"> return </span> <span> </span> <span class = "number"> 0 </span> <span>; </span> </li> <li class = ""> <span> </li> </ol> </div> <pre name = "code" class = "java" style = "display: none; "> retur N 0 ;}</pre> <br> <p> translation: "Pss ", what is the difference between "PrivateDirty" and "SharedDirty" </p> <p> in Android (that is, Linux), a large amount of memory is actually shared by multiple processes, therefore, it is not clear how much memory a process actually occupies. It is even unclear which pages are added to the disk. </P> <p> in this way, if you want to obtain the physical memory value mapped to each process, and then try to add the total sum, you may get a value larger than the actual total memory size. </P> <p> Pss is the kernel computing scale considering the shared memory. Basically, each memory page of a process is reduced by a ratio, this ratio is related to the number of other processes that use the page. In theory, you can accumulate the number of Pss used by all processes to check the memory usage of all processes. You can also roughly compare the number of Pss used by processes to find their respective weights. </P> <p> another interesting parameter is PrivateDirty, which is basically the memory that cannot be paged to the disk in the process or shared with other processes. Another way to view the memory usage of a process is to change the available memory of the system at the end of the process (it may also be quickly incorporated into the high-speed buffer or other processes that use the memory area, [ in this way, the attempt fails:-)]). <Br> </p> <p> original <br> </p> <p> But as to what the difference is between "Pss", "PrivateDirty ", and "SharedDirty "... well now the fun begins. </p> <p> A lot of memory in Android (and Linux systems in general) is actually shared memory SS multiple processes. so how much memory a processes uses is really not clear. add on top of that paging out to disk (let alone swap which we don't use on Android) and it is even less clear. </p> <p> Thus if you were to take all of the physical RAM actually mapped in to each process, and add up all of the processes, you wowould probably end up with a number much greater than the actual total RAM. </p> <p> The Pss number is a metric the kernel computes that takes into account memory sharing -- basically each page of RAM in a process is scaled by a ratio of the number other processes also using that page. this way you can (in theory) add up the pss processing SS all processes to see the total RAM they are using, and compare pss between processes to get a rough idea of their relative weight. </p> <p> The other interesting metric here is PrivateDirty, which is basically the amount of RAM inside the process that can not be paged to disk (it is not backed by the same data on disk), and is not shared with any other processes. another way to look at this is the RAM that will become available to the system when that process goes away (and probably quickly subsumed into caches and other uses of it ). </p> <br> </p> <p> <br> </p> <br>

Related Article

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.