Android MemInfo 各项的意义(转)

蹉跎莫遣韶光老,人生唯有读书好。这篇文章主要讲述Android MemInfo 各项的意义(转)相关的知识,希望能为你提供帮助。
http://gdgzzch.blog.163.com/blog/static/37640452201371483147573/
 
 
http://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-android#2299813
http://my.oschina.net/shaorongjie/blog/128442


可以使用adb shell dumpsys meminfo -a < process id> /< process name> 来查看一个进程的memory。截图如下:


Naitve Heap Size: 从mallinfo usmblks获得,代表最大总共分配空间
Native Heap Alloc: 从mallinfo uorblks获得,总共分配空间
Native Heap Free: 从mallinfo fordblks获得,代表总共剩余空间 
Native Heap Size 约等于Native Heap Alloc + Native Heap Free
mallinfo是一个C库, mallinfo 函数提供了各种各样的通过C的malloc()函数分配的内存的统计信息。
Dalvik Heap Size:从Runtime totalMemory()获得,Dalvik Heap总共的内存大小。
Dalvik Heap Alloc: Runtime totalMemory()-freeMemory() ,Dalvik Heap分配的内存大小。
Dalvik Heap Free:从Runtime freeMemory()获得,Dalvik Heap剩余的内存大小。
Dalvik  Heap Size 约等于Dalvik  Heap Alloc + Dalvik  Heap Free

OtherPss, include Cursor,Ashmem, Other Dev, .so mmap, .jar mmap, .apk mmap, .ttf mmap, .dex mmap, Other mmap, Unkown统计信息都可以在process的smap文件看到。
Objects and SQL 信息都是从Android Debug信息中获得。
其他类型               smap 路径名称           描述
Cursor                   /dev/ashmem/Cursor   Cursor消耗的内存(KB)
Ashmem               /dev/ashmem             匿名共享内存用来提供共享内存通过分配一个多个进程
                                                          可以共享的带名称的内存块
Other dev             /dev/                         内部driver占用的在  “Other dev”                                                  
.so mmap             .so                             C 库代码占用的内存
.jar mmap             .jar                           java 文件代码占用的内存
.apk mmap           .apk                           apk代码占用的内存
.ttf mmap               .ttf                           ttf 文件代码占用的内存
.dex mmap             .dex                         Dex 文件代码占用的内存
Other mmap                                           其他文件占用的内存



But as to what the difference is between "Pss", "PrivateDirty", and "SharedDirty"...well now the fun begins.
A lot of memory in Android (and Linux systems in general) is actually shared across 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.
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 would probably end up with a number much greater than the actual total RAM.
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 of other processes also using that page. This way you can (in theory) add up the pss across all processes to see the total RAM they are using, and compare pss between processes to get a rough idea of their relative weight.
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).
That is pretty much the SDK APIs for this.However there is more you can do as a developer with your device.
Using adb, there is a lot of information you can get about the memory use of a running system.A common one is the command "adb shell dumpsys meminfo" which will spit out a bunch of information about the memory use of each Java process, containing the above info as well as a variety of other things.You can also tack on the name or pid of a single process to see, for example "adb shell dumpsys meminfo system" give me the system process:

** MEMINFO in pid 890 [system] ** nativedalvikothertotal size:109407047N/A17987 allocated:89435516N/A14459 free:3361531N/A1867 (Pss):458592821191625783 (shared dirty):218435969166696 (priv dirty):45045956745617916 Objects Views:149ViewRoots:4 AppContexts:13Activities:0 Assets:4AssetManagers:4 Local Binders:141Proxy Binders:158 Death Recipients:49 OpenSSL Sockets:0 SQL heap:205dbFiles:0 numPagers:0inactivePageKB:0 activePageKB:0

The top section is the main one, where "size" is the total size in address space of a particular heap, "allocated" is the kb of actual allocations that heap thinks it has, "free" is the remaining kb free the heap has for additional allocations, and "pss" and "priv dirty" are the same as discussed before specific to pages associated with each of the heaps.
If you just want to look at memory usage across all processes, you can use the command "adb shell procrank". Output of this on the same system looks like:
PIDVssRssPssUsscmdline 89084456K48668K25850K21284Ksystem_server 123150748K39088K17587K13792Kcom.android.launcher2 94734488K28528K10834K9308Kcom.android.wallpaper 98726964K26956K8751K7308Kcom.google.process.gapps 95424300K24296K6249K4824Kcom.android.phone 94823020K23016K5864K4748Kcom.android.inputmethod.latin 88825728K25724K5774K3668Kzygote 97724100K24096K5667K4340Kandroid.process.acore ... 59336K332K99K92K/system/bin/installd 60396K392K93K84K/system/bin/keystore 51280K276K74K68K/system/bin/servicemanager 54256K252K69K64K/system/bin/debuggerd

Here the Vss and Rss columns are basically noise (these are the straight-forward address space and RAM usage of a process, where if you add up the RAM usage across processes you get an ridiculously large number).
Pss is as we‘ve seen before, and Uss is Priv Dirty.
Interesting thing to note here: Pss and Uss are slightly (or more than slightly) different than what we saw in meminfo. Why is that? Well procrank uses a different kernel mechanism to collect its data than meminfo does, and they give slightly different results. Why is that? Honestly I haven‘t a clue. I believe procrank may be the more accurate one... but really, this just leave the point: "take any memory info you get with a grain of salt; often a very large grain."
Finally there is the command "adb shell cat /proc/meminfo" that gives a summary of the overall memory usage of the system. There is a lot of data here, only the first few numbers worth discussing (and the remaining ones understood by few people, and my questions of those few people about them often resulting in conflicting explanations):
MemTotal:395144 kB MemFree:184936 kB Buffers:880 kB Cached:84104 kB SwapCached:0 kB

MemTotal is the total amount of memory available to the kernel and user space (often less than the actual physical RAM of the device, since some of that RAM is needed for the radio, DMA buffers, etc).
MemFree is the amount of RAM that is not being used at all. The number you see here is very high; typically on an Android system this would be only a few MB, since we try to use available memory to keep processes running
【Android MemInfo 各项的意义(转)】Cached is the RAM being used for filesystem caches and other such things. Typical systems will need to have 20MB or so for this to avoid getting into bad paging states; the Android out of memory killer is tuned for a particular system to make sure that background processes are killed before the cached RAM is consumed too much by them to result in such paging.








    推荐阅读