This article refers to: http://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-android# 2299813 the memory usage of modern operating systems like Linux is very 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. Mode one, Running services is viewed through the activity of the Running services on the mobile phone, and can be entered through setting->applications->running services. For more information about running services, please refer to theuse "Running services" to view service process memory in Android"Mode two, using Activitymanager's Getmemoryinfo (activitymanager.memoryinfo outinfo) Activitymanager.getmemoryinfo () It is mainly used to get the remaining memory of the current system and to determine whether it is running in low memory. Instance 1: private void Displaybriefmemory () { final Activitymanager Activitymanager = (activitymanager) getsystemservice (ACTIVITY_SERVICE); activitymanager.memoryinfo info = new Activitymanager.memoryinfo (); activitymanager.getmemoryinfo (info); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I (tag, "System remaining Memory:" + (info.availmem > >) + "K"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I (Tag, "System is running low memory:" +info.lowmemory); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I (Tag, "when the system's remaining memory is lower than" +info.threshold+ "is considered a low memory run"); } activitymanager.getmemoryinfo () is used Activitymanager.memoRyinfo return results, not debug.memoryinfo, they are not the same. Activitymanager.memoryinfo only three field:availmem: Represents the system's remaining memory Lowmemory: It is a Boolean value, Indicates whether the system is in a low memory run Hreshold: it means that when the system has less memory left than a lot, it is considered as low memory run mode three, using Debug Getmemoryinfo in code (Debug.memoryinfo memoryinfo) Or Activitymanager's memoryinfo[] Getprocessmemoryinfo (int[] PIDs) The memoryinfo described in this way is more detailed in memory usage. The unit of data is Kb.memoryinfo field as follows Dalvikprivatedirty: the private dirty pages used by Dalvik Dalvikpss :the proportional set size for dalvik.dalvikshareddirty :the GKFX dirty pages used by dalvik.native Privatedirty :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& Nbsp;: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 outTo disk (page change), so 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 private. Non-shared. Share: Refers to shared memory. PSS: Physically used physical memory (proportional allocation of memory consumed by shared libraries) PSS: It is allocating shared memory to the individual processes that share it to compute the resulting process using memory. 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: Refer to Privatedirty I think it should refer to 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. For specific code, refer to Example 1 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: You can list<<a href= "http://developer.android.com/reference/android/app/by Activitymanager. ActivityManager.RunningAppProcessInfo.html "rel=" nofollow "style=" Color:rgb (207, 121, 28); Text-decoration:none; " >activitymanager.runningappprocessinfo> getrunningappprocesses () gets all the currently running process information. 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 the 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. ReturnedIs 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 memory size that is already remaining in the current process navtive heap static long getnativeheapsize () Returns the size of the native Heap. Returns the total memory size of the current process navtive heap itself sample code: &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I (Tag, " Nativeheapsizetotal: "+ (Debug.getnativeheapsize () >>10)); &NBSP;&NBSP;LOG.I (Tag, "nativeallocatedheapsize:" + (Debug.getnativeheapallocatedsize () >>10)); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;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 total size: 3456 3139 &NBSP;&NBSP;&NBSP;N/A 6595 allocated: 3432 2823 n/a 6255 free: 23 316 n/a 339 (PSS): 724 1101 1070 2895 (Shared dirty): 1584 4540 1668 7792 (Priv Dirty): 644 608 688 1940 Objects views: 0 viewroots: 0 appcontexts: 0 activities: 0 assets: 3 assetmanagers: 3 local binders: 5 proxy Binders: &nbSp; 11death Recipients: 0 openssl Sockets: 0 SQL heap: 0 memoryused: 0pagecacheoverflo: 0 Largestmemalloc: 0 asset Allocations zip:/data/app/com.teleca.robin.test-1.apk:/resources.arsc:1K "Size" represents the total memory size (in kilobytes). , "Allocated" represents the memory size (KB) that has been used, and "free" represents the remaining memory size (KB), and more can be referenced in the description of mode three and mode four now has a tool to automatically extract summary Dumpsys meminfo information, for details, refer to theAndroid Memory Leak tool (memory statistics chapter)and its series of articles.Way VI,Use the "adb shell procrank" command if 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 &NBSP;&NBSP;PSS uss cmdline 188 75832K 51628K 24824k 19028k system_server 308 50676k 26476K 9839k 6844k system_server 2834 35896K 31892k 9201k 6740k com.sec.android.app.twlauncher 265 28536k 28532k 7985k 5824k com.android.phone 100 29052k 29048k 7299k 4984k zygote 258 27128k 27124k 7067k 5248k com.swype.android.inputmethod 25820k 25816k 6752k 5420k com.android.kineto 1253 27004k 27000k 6489k 4880k com.google.android.voicesearch 2898 26620k 26616k 6204K 3408k com.google.android.apps.maps:friendservice 297 26180K 26176K 5886k 4548k com.google.process.gapps 3157 24140K 24136k 5191k 4272k android.process.acore 2854 23304k 23300k 4067k 2788k com.android.vending 3604 22844k 22840k 4036k 3060k &NBSP;COM.WSSYNCMLDM 592 23372k 23368k 3987k 2812k com.google.android.googlequicksearchbox 3000 22768k 22764k 3844K 2724k com.tmobile.selfhelp 101 8128k 8124K 3649k 2996k /system/bin/mediaserver 3473 21792K 21784K 3103k 2164k com.android.providers.calendar 3407 22092K 22088k 2982k 1980k com.teleca.robin.test 2840 21380k 21376k 2953k 1996k Com.sec.android.app.controlpanel ..... ..... ........................... ....... ..... ................. ......... ............................. For the meaning of Vss,rss,pss,uss, please refer to theVss/rss/pss/uss of Android memoryNote 1: Here PSS and mode four PSS's total is 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 will be slightly different Note 2: Here the USS and the way four of the Priv Dirtyd total 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!Way 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 KB memfree:184936 KB buffers:880 KB cached:84104 KB swapcached : 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,The main benefit of using the "adb shell ps-x" command is that the memory information is vsize and RSS. USER pid ppid vsize rss wchan pc name ......... .......... Omit ... app_70 3407 100 267104 22056 ffffffff, ..... ....., .....-------... Afd0eb18 S com.teleca.robin.test (u:55, s:12) app_7 3473 100 268780 21784 FFFFFFFF afd0eb18 S Com.android.providers.calendar (u:16, s:8) Radio 3487 100 267980 21140 ffffffff afd0eb18 S com.osp.app.signin (u:11, s:12) system 3511 100 & nbsp;273232 22024 ffffffff afd0eb18 S com.android.settings (u:11, s:4) app_15 3546 100 & nbsp;267900 20300 ffffffff afd0eb18 S com.sec.android.providers.drm (u:15, s:6) app_59 3604 100 & nbsp; 272028 22856 ffffffff afd0eb18 S com.wssyncmldm (u:2s:54) root 4528 2 0 0 c0141e4c 00000000 S flush-138:13 (u:0, s:0) root 4701 152 & nbsp; 676 336 c00a68c8 afd0e7cc s/system/bin/sh (u:0, s:0) root & nbsp; 4702 4701 820 340 00000000 AFD0D8BC R PS (u:0, s:5) VSZIE: meaning temporarily unknown. VSS: Please refer to theVss/rss/pss/uss of Android memoryNote 1: Because the value of RSS is not very big, so generally do not use. Note 2: Using this command to extract RSS, there is already a tool, specific reference to theAndroid Memory leak tool (RSS memory statistics)"and its series.
Example 1
int cnt=0;
final static int kbufferminsize=1000;
final static int kbuffermaxsize=2000;
StringBuffer strbuffer=new StringBuffer (kbufferminsize);
StringBuffer strbuffer2=new StringBuffer (kbufferminsize);
StringBuffer strbuffer3=new StringBuffer (kbufferminsize);
StringBuffer strbuffernativepss=new StringBuffer (kbufferminsize);
StringBuffer strbufferdalvikpss=new StringBuffer (kbufferminsize);
StringBuffer strbufferotherpss=new StringBuffer (kbufferminsize);
Debug.memoryinfo memoryinfo=new debug.memoryinfo ();
Final static String tag= "Robin";
void Printmemory ()
{
Long Totalmemory=runtime.getruntime (). TotalMemory ();
Long Freememory=runtime.getruntime (). Freememory ();
Long usedmemory= (totalmemory-freememory) >>10;
totalmemory=totalmemory>>10;
freememory=freememory>>10;
if (Strbuffer.length () >kbuffermaxsize)
{
Strbuffer.delete (0,strbuffer.length ());
Strbuffer2.delete (0,strbuffer2.length ());
Strbuffer3.delete (0,strbuffer3.length ());
Strbuffernativepss.delete (0,strbuffernativepss.length ());
Strbufferdalvikpss.delete (0,strbufferdalvikpss.length ());
}
Strbuffer.append (usedmemory+ ",");
Strbuffer2.append (totalmemory+ ",");
Strbuffer3.append ((Debug.getnativeheapsize () >>10) + ",");
Debug.getmemoryinfo (Memoryinfo);
Strbuffernativepss.append ((MEMORYINFO.NATIVEPSS) + ",");
Strbufferdalvikpss.append ((MEMORYINFO.DALVIKPSS) + ",");
if (cnt++==0)
{
LOG.I (Tag, "Usedmemory:" +strbuffer.tostring ());
LOG.I (Tag, "TotalMemory:" +strbuffer2.tostring ());
LOG.I (Tag, "Nativeheapsize:" +strbuffer3.tostring ());
LOG.I (Tag, "Native PSS:" +strbuffernativepss.tostring ());
LOG.I (Tag, "Dalvik PSS:" +strbufferdalvikpss.tostring ());
}
}
Note that for the output of the memory information log, we can use a little editing to be used in the Excel production chart, more intuitive analysis. http://blog.csdn.net/hudashi/article/details/7050897http://blog.csdn.net/hudashi/article/details/7050905 Transferred from: http://blog.sina.com.cn/s/blog_765e27790101azse.html
Android View Memory