How to view memory in Android (top)

Source: Internet
Author: User

This article refers to: Http://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-android #2299813The memory usage of modern operating systems like Linux is complex, so it's hard to know exactly how much memory your application uses. There are a number of ways to view memory usage, but the results may vary slightly from one way or another. Way One, Running servicesThrough the activity of Running services on the phone, you can go through the setting->applications->running services. For more information on running services, refer to "Using running services" in Android to view service process memory mode two, using Activitymanager's Getmemoryinfo (activitymanager.memoryinfo outinfo)Activitymanager.getmemoryinfo () is mainly used to get the remaining memory of the current system and to determine whether it is running in low memory. Example 1:private void Displaybriefmemory () {final Activitymanager Activitymanager = (activitymanager) getsystemservice (Activity_service); Activitymanager.memoryinfo info = new Activitymanager.memoryinfo (); Activitymanager.getmemoryinfo (info); log.i (tag, "System remaining Memory:" + (Info.availmem >>) + "K"); log.i (Tag, "whether the system is running in low memory:" +info.lowmemory);log.i (Tag, "When the system remaining memory is lower than" +info.threshold+ "as Low memory Run");    } Activitymanager.getmemoryinfo () is used activitymanager.memoryinfo to return results, not debug.memoryinfo, they are not the same. Activitymanager.memoryinfo has only three field:Availmem: Indicates system remaining memoryLowmemory: It is a Boolean value that indicates whether the system is running in low memoryHreshold: It means low memory runs when the system has less memory leftmode three, use Debug in code getmemoryinfo (Debug.memoryinfo memoryinfo) or Activitymanager memoryinfo[] Getprocessmemoryinfo (int[] PIDs)the memoryinfo described in this way is more detailed in memory usage. The data is in kilobytes.memoryinfo field is as followsdalvikprivatedirty:the private dirty pages used by Dalvik. dalvikpss:the Proportional set size for Dalvik.dalvikshareddirty:the shared dirty pages used by Dalvik.nativeprivatedirty:the private dirty pages used by the native heap.nativepss:the Proportional set size for the native heap.nativeshareddirty:the shared dirty pages used by the native heap.otherprivatedirty:the private dirty pages used by everything else.otherpss:the Proportional set size for everything else.othershareddirty:the shared dirty pages used by everything else.Android and Linux have a large number of processes that exist between processes sharing. A process that uses a lot of memory accurately is actually difficult to count. because there is paging out to disk (page change), if you add all the memory mapped to the process, it may be larger than the actual physical size of your memory. Dalvik: Refers to the memory used by the Dalvik. Native: Is the memory used by the native heap. It should refer to the memory allocated on the heap using c\c++. Other: Refers to memory used in addition to Dalvik and native. But what exactly does it mean? Include at least non-heap memory allocated in c\c++, such as memory allocated on the stack. puzlle!Private: Refers to proprietary. Non-shared. share: Refers to shared memory. PSS: Physically used physical memory (proportional allocation of memory consumed by shared libraries)Pss: It is to compute the resulting process using memory by allocating shared memory to each process that shares it, based on a certain percentage. It is also said that the proportion of shared library occupied memory, it is not clear whether the share is only the library share. Privatedirty: It refers to the size of the memory that is unshared and cannot be paged out (can not be paged to disk). For example, the small object that Linux buffers in order to increase memory speed, even if your process ends, the memory will not be released, it just back into the buffer. Shareddirty: Referring to Privatedirty I think it should mean the size of the memory that is shared and cannot be paged out (can not be paged to disk). For example, the small object that Linux buffers in order to increase the allocated memory speed, even if all the processes that share it end, the memory is not released, it just goes back to the buffer. Please refer to Example 1 for specific code .Note that the memory usage described by 1:memoryinfo can be obtained by commanding adb shell "Dumpsys meminfo%curprocessname%". NOTE 2: Use Activitymanager's memoryinfo[] Getprocessmemoryinfo (int[] pids) If you want to get memory usage for more than one process in your code, or memory usage for non-processes .otherwise the Debug getmemoryinfo (Debug.memoryinfo memoryinfo) is available. Note 3: Can be activitymanager by list<activitymanager.runningappprocessinfo> getrunningappprocesses () Gets the current process information for all running. in Activitymanager.runningappprocessinfo, there is the ID of the process, the name and all the APK package names included in the process. Note 4: The unit of data is in kilobytes.mode 4, using Debug Getnativeheapsize (), Getnativeheapallocatedsize (), Getnativeheapfreesize () method. This method can only get the memory of native heap, the data unit is byte. Static Long getnativeheapallocatedsize ()Returns The amount of allocated memory in the native heap.returns the amount of memory used in the current process navtive heap
Static Long getnativeheapfreesize ()Returns The amount of free memory in the native heap.returns the amount of memory already remaining in the current process navtive heapStatic Long getnativeheapsize ()Returns the size of the native heap.returns the total memory size of the current process navtive heap itselfExample code:log.i (Tag, "Nativeheapsizetotal:" + (Debug.getnativeheapsize () >>10));log.i (Tag, "nativeallocatedheapsize:" + (Debug.getnativeheapallocatedsize () >>10));log.i (Tag, "Nativeallocatedfree:" + (Debug.getnativeheapfreesize () >>10));Note: There is no function in debug that corresponds to the Dalvik. mode Five, use Dumpsys meminfo command. we can run the Dumpsys meminfo command in the ADB shell to get the memory information of the process. After the command, add the name of the process to determine which process it is. For example, "adb shell Dumpsys meminfo com.teleca.robin.test" will get information about the memory used by the Com.teleca.robin.test process:Applications Memory Usage (KB):uptime:12101826 realtime:270857936 * * Meminfo in PID 3407 [com.teleca.robin.test] * *native Dalvik other totalsize:3456 3139 N /a 6595allocated:3432 2823 N /a 6255free:23, n /a 339(PSS): 724 1101 1070 2895(Shared dirty): 1584 4540 1668 7792(Priv Dirty): 644 608 688 1940 Objectsviews:0 viewroots:0appcontexts:0 activities:0assets:3 Assetmanagers:3Local binders:5 Proxy binders:11Death recipients:0OpenSSL sockets:0 SQLheap:0 memoryused:0pagecacheoverflo:0 largestmemalloc:0  Asset Allocationszip:/data/app/com.teleca.robin.test-1.apk:/resources.arsc:1k"Size" represents the total memory size in kilobytes (KB). , "Allocated" represents the memory size (in kilobytes) that has been used, and "free" represents the remaining memory size (KB), which can be described in the reference to mode three and mode fourNow there is a tool to automatically extract summary Dumpsys meminfo information, refer to the " Android Memory leak tool (memory statistics)" and its series of articles. mode Six, use the "adb shell procrank" commandIf you want to see memory usage for all processes, you can use the "adb shell procrank" command. The command return will be as follows:PID Vss Rss Pss Uss cmdline188 75832K 51628K 24824K 19028K system_server308 50676K 26476K 9839K 6844K system_server2834 35896K 31892K 9201K 6740K com.sec.android.app.twlauncher265 28536K 28532K 7985K 5824K com.android.phone29052K 29048K 7299K 4984K zygote258 27128K 27124K 7067K 5248K com.swype.android.inputmethod25820K 25816K 6752K 5420K com.android.kineto1253 27004K 27000K 6489K 4880K com.google.android.voicesearch2898 26620K 26616K 6204K 3408K com.google.android.apps.maps:FriendService297 26180K 26176K 5886K 4548K Com.google.process.gapps3157 24140K 24136K 5191K 4272K android.process.acore2854 23304K 23300K 4067K 2788K com.android.vending3604 22844K 22840K 4036K 3060K com.wssyncmldm592 23372K 23368K 3987K 2812K com.google.android.googlequicksearchbox22768K 22764K 3844K 2724K com.tmobile.selfhelp101 8128K 8124K 3649K 2996k/system/bin/mediaserver3473 21792K 21784K 3103K 2164K Com.android.providers.calendar3407 22092K 22088K 2982K 1980K com.teleca.robin.test2840 21380K 21376K 2953K 1996K com.sec.android.app.controlpanel..................................................................................................................... .please refer to " Android memory Vss/rss/pss/uss" for the meaning of Vss,rss,pss,uss.Note 1: Here PSS and mode four PSS Total are not consistent, there are subtle differences. Why is it? This is because the Procrank command and the Meminfo command use a different kernel mechanism, so the results are slightly different .NOTE 2: Here the USS and the PRIV Dirtyd of the way four are almost equal. They seem to express the same meaning. But the explanations for what they are now are not quite the same. is the private dirty here (this means that the page cannot be changed)? puzzle! mode Seven, use the "adb shell cat/proc/meminfo" command. This method can only obtain the approximate usage of the whole memory of the system. memtotal:395144 KBmemfree:184936 KBbuffers:880 KBcached:84104 KBswapcached:0 KB................................................................................................Memtotal: The total amount of memory available to the system and the user (it is smaller than the actual physical memory, as there is some memory to use for radio, DMA buffers, etc.).Memfree: The remaining available memory size. This value is relatively large here, in fact, the average Android system is usually very small, because we try to keep the process running, which consumes a lot of memory. Cached: This is the memory used by the system for file buffering and so on. Usually systems need 20MB to avoid bad paging states;. When memory is tight, the Android out of memory killer will kill some background processes to avoid them consuming too much cached RAM, of course, if you use them again next time, you will need paging. So is the memory of the background process included in the item? mode Eight, use the "adb shell ps-x" commandthe main way to get is that the memory information is vsize and RSS. USER PID PPID vsize RSS wchan PC NAME......................... To omit ....... .................app_70 3407 267104 22056 ffffffff afd0eb18 S com.teleca.robin.test (u:55, s:12)app_7 3473 268780 21784 ffffffff afd0eb18 S Com.android.providers.calendar (u:16, s:8)Radio 3487 267980 21140 ffffffff afd0eb18 S com.osp.app.signin (u:11, s:12)system 3511 273232 22024 ffffffff afd0eb18 S com.android.settings (u:11, s:4)app_15 3546 267900 20300 ffffffff afd0eb18 S com.sec.android.providers.drm (u:15, s:6)app_59 3604 272028 22856 ffffffff afd0eb18 S com.wssyncmldm (u:231, s:54)Root 4528 2 0 0 c0141e4c 00000000 S flush-138:13 (u:0, s:0)Root 4701 676 336 c00a68c8 afd0e7cc s/system/bin/sh (u:0, s:0)Root 4702 4701 820 340 00000000 AFD0D8BC R PS (u:0, s:5)Vszie: The meaning is temporarily unknown. VSS: Please refer to " Android memory Vss/rss/pss/uss"Note 1: Because the value of RSS is not very large, so generally do not use. NOTE 2: Using this command to extract RSS, there is already a tool, specific to the " Android Memory leak tool (RSS memory statistics)" and its series.

How to view memory in Android (top)

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.