View overall memory allocations

几个月之前看过一遍,结果过一阵子在查bitmap造成内存泄露时又感觉忘得差不多了,不晓得android os RAM 到底怎么分配的了。干脆,再翻翻,翻译翻译。(粗翻,不对请指出)

https://developer.android.com/studio/profile/investigate-ram.html?

观察所有的内存分配

为了更深入的研究,你需要下面这个指令来观察,你app的内存在不同类型的RAM分配之下是如何分开使用的:

adb shell dumpsys meminfo[-d]

加入-d 标识可以打印更多关于Dalvik 和 ART的内存使用信息。

这条命令的输出展示了app所有当前的内存分配,kb为单位。

当查看这些信息时,你需要知道下面几种分配类型:(之所以把英文也贴到文章结尾,是因为貌似这一块貌似更新过,几个月之前我看的分配类型好像不是这些,贴出英文,怕后期google有更新,好有一个对比)

私有RAM(分为Clean和Dirty)

这一部分内存是被你的进程独占的。当你的app进程销毁,这部分内存绝大多数会被系统收回。通常,私有RAM里面最重要的部分是私有dirty RAM,因为他不仅由你的进程独占,同时其所有内容也只能在RAM里面,不能被分页存入外存(因为android没有虚拟内存?),所以他尤其珍贵。所有Dalvik和native heap使用的内存都是私有dirty RAM 。你和Zygote 进程共享的Dalvik与native内存属于 共享dirty RAM。

个人理解:内存分

Private RAM (Dirty :上面已讲 Clean:不明?)

Shared RAM(Dirty:上面已讲 Clean:不明?)

PSS-实际使用的物理内存(比例分配共享库占用的内存)

这是一种把你的app所有有共享部分的进程内存也计算在内的测量手段。所有你的进程独占的RAM内存首先会直接计入PSS,然后与其他进程共享的内存会只计算共享的那一部分。例如,一块内存被2个进程共享,就会把这块内存的一半分别算入两个进程的PSS 里面去。

PSS这一测量规则的一个好处是你可以把所有的内存的PSS加在一起,就可以看到所有进程的实际使用总内存。这意味着PSS是一个很好的,测量一个进程实际使用内存的方法,同时也很方便与其他进程的内存使用量进行对比,而且方便计算总的可用内存。 例如:... (见链接图表)

通常,只需要关心 Pss Total和 Private Dirty 这两列,在一些例子中,Private Clean和Heap Alloc 列也有一些有趣的数据。为了观察更多的内存分配信息,你需要关注下面的数据:

Dalvik Heap:

Dalvik虚拟机所使用的那一部分RAM. PSS Total包含所有Zygote分配的内存(按所有共享进程的占比计入,上文对PSS的定义已经说过)The Private Dirty number is the actual RAM committed to only your app’s heap, composed of your own allocations and any Zygote allocation pages that have been modified since forking your app’s process from Zygote.(有些没看懂,Private Dirty是 真实的RAM内存,只属于app堆中,当app进程从zygote进程fork出来后,app所分配的和zygote所分配的内存就会发生改变?)



.so mmapand.dex mmap:


.oat mmap:


... Please stay tuned



View overall memory allocations

For further analysis, you might want to observe how your app's memory is divided between different types of RAM allocation with the followingadbcommand:

adb shell dumpsys meminfo [-d]

The -d flag prints more info related to Dalvik and ART memory usage.

The output lists all of your app's current allocations, measured in kilobytes.

When inspecting this information, you should be familiar with the following types of allocation:

Private (Clean and Dirty) RAM

This is memory that is being used by only your process. This is the bulk of the RAM that the system can reclaim when your app’s process is destroyed. Generally, the most important portion of this isprivate dirtyRAM, which is the most expensive because it is used by only your process and its contents exist only in RAM so can’t be paged to storage (because Android does not use swap). All Dalvik and native heap allocations you make will be private dirty RAM; Dalvik and native allocations you share with the Zygote process are shared dirty RAM.

Proportional Set Size (PSS)

This is a measurement of your app’s RAM use that takes into account sharing pages across processes. Any RAM pages that are unique to your process directly contribute to its PSS value, while pages that are shared with other processes contribute to the PSS value only in proportion to the amount of sharing. For example, a page that is shared between two processes will contribute half of its size to the PSS of each process.

A nice characteristic of the PSS measurement is that you can add up the PSS across all processes to determine the actual memory being used by all processes. This means PSS is a good measure for the actual RAM weight of a process and for comparison against the RAM use of other processes and the total available RAM.

For example, below is the the output for Map’s process on a Nexus 5 device. There is a lot of information here, but key points for discussion are listed below.

adb shell dumpsys meminfo com.google.android.apps.maps -d

Note:The information you see might vary slightly from what is shown here, because some details of the output differ across platform versions.

** MEMINFO in pid 18227 [com.google.android.apps.maps] **

Pss  Private  Private  Swapped    Heap    Heap    Heap

Total    Dirty    Clean    Dirty    Size    Alloc    Free

------  ------  ------  ------  ------  ------  ------

Native Heap    10468    10408        0        0    20480    14462    6017

Dalvik Heap    34340    33816        0        0    62436    53883    8553

Dalvik Other      972      972        0        0

Stack    1144    1144        0        0

Gfx dev    35300    35300        0        0

Other dev        5        0        4        0

.so mmap    1943      504      188        0

.apk mmap      598        0      136        0

.ttf mmap      134        0      68        0

.dex mmap    3908        0    3904        0

.oat mmap    1344        0      56        0

.art mmap    2037    1784      28        0

Other mmap      30        4        0        0

EGL mtrack    73072    73072        0        0

GL mtrack    51044    51044        0        0

Unknown      185      184        0        0

TOTAL  216524  208232    4384        0    82916    68345    14570

Dalvik Details

.Heap    6568    6568        0        0

.LOS    24771    24404        0        0

.GC      500      500        0        0

.JITCache      428      428        0        0

.Zygote    1093      936        0        0

.NonMoving    1908    1908        0        0

.IndirectRef      44      44        0        0

Objects

Views:      90        ViewRootImpl:        1

AppContexts:        4          Activities:        1

Assets:        2        AssetManagers:        2

Local Binders:      21        Proxy Binders:      28

Parcel memory:      18        Parcel count:      74

Death Recipients:        2      OpenSSL Sockets:        2

Here is an older dumpsys on Dalvik of the gmail app:

** MEMINFO in pid 9953 [com.google.android.gm] **

Pss    Pss  Shared Private  Shared Private    Heap    Heap    Heap

Total  Clean  Dirty  Dirty  Clean  Clean    Size  Alloc    Free

------  ------  ------  ------  ------  ------  ------  ------  ------

Native Heap      0      0      0      0      0      0    7800    7637(6)  126

Dalvik Heap  5110(3)    0    4136    4988(3)    0      0    9168    8958(6)  210

Dalvik Other  2850      0    2684    2772      0      0

Stack    36      0      8      36      0      0

Cursor    136      0      0    136      0      0

Ashmem    12      0      28      0      0      0

Other dev    380      0      24    376      0      4

.so mmap  5443(5) 1996    2584    2664(5) 5788    1996(5)

.apk mmap    235      32      0      0    1252      32

.ttf mmap    36      12      0      0      88      12

.dex mmap  3019(5) 2148      0      0    8936    2148(5)

Other mmap    107      0      8      8    324      68

Unknown  6994(4)    0    252    6992(4)    0      0

TOTAL  24358(1) 4188    9724  17972(2)16388    4260(2)16968  16595    336

Objects

Views:    426        ViewRootImpl:        3(8)

AppContexts:      6(7)        Activities:        2(7)

Assets:      2        AssetManagers:        2

Local Binders:    64        Proxy Binders:      34

Death Recipients:      0

OpenSSL Sockets:      1

SQL

MEMORY_USED:  1739

PAGECACHE_OVERFLOW:  1164          MALLOC_SIZE:      62

In general, be concerned with only thePss TotalandPrivate Dirtycolumns. In some cases, thePrivate CleanandHeap Alloccolumns also offer interesting data. More information about the different memory allocations (the rows) you should observe follows:

Dalvik Heap

The RAM used by Dalvik allocations in your app. ThePss Totalincludes all Zygote allocations (weighted by their sharing across processes, as described in the PSS definition above). ThePrivate Dirtynumber is the actual RAM committed to only your app’s heap, composed of your own allocations and any Zygote allocation pages that have been modified since forking your app’s process from Zygote.

Note:On newer platform versions that have theDalvik Othersection, thePss TotalandPrivate Dirtynumbers for Dalvik Heap do not include Dalvik overhead such as the just-in-time compilation (JIT) and GC bookkeeping, whereas older versions list it all combined underDalvik.

TheHeap Allocis the amount of memory that the Dalvik and native heap allocators keep track of for your app. This value is larger thanPss TotalandPrivate Dirtybecause your process was forked from Zygote and it includes allocations that your process shares with all the others.

.so mmapand.dex mmap

The RAM being used for mapped.so(native) and.dex(Dalvik or ART) code. ThePss Totalnumber includes platform code shared across apps; thePrivate Cleanis your app’s own code. Generally, the actual mapped size will be much larger—the RAM here is only what currently needs to be in RAM for code that has been executed by the app. However, the .so mmap has a large private dirty, which is due to fix-ups to the native code when it was loaded into its final address.

.oat mmap

This is the amount of RAM used by the code image which is based off of the preloaded classes which are commonly used by multiple apps. This image is shared across all apps and is unaffected by particular apps.

.art mmap

This is the amount of RAM used by the heap image which is based off of the preloaded classes which are commonly used by multiple apps. This image is shared across all apps and is unaffected by particular apps. Even though the ART image containsObjectinstances, it does not count towards your heap size.

.Heap(only with -d flag)

This is the amount of heap memory for your app. This excludes objects in the image and large object spaces, but includes the zygote space and non-moving space.

.LOS(only with -d flag)

This is the amount of RAM used by the ART large object space. This includes zygote large objects. Large objects are all primitive array allocations larger than 12KB.

.GC(only with -d flag)

This is the amount of internal GC accounting overhead for your app. There is not really any way to reduce this overhead.

.JITCache(only with -d flag)

This is the amount of memory used by the JIT data and code caches. Typically, this is zero since all of the apps will be compiled at installed time.

.Zygote(only with -d flag)

This is the amount of memory used by the zygote space. The zygote space is created during device startup and is never allocated into.

.NonMoving(only with -d flag)

This is the amount of RAM used by the ART non-moving space. The non-moving space contains special non-movable objects such as fields and methods. You can reduce this section by using fewer fields and methods in your app.

.IndirectRef(only with -d flag)

This is the amount of RAM used by the ART indirect reference tables. Usually this amount is small, but if it is too high, it might be possible to reduce it by reducing the number of local and global JNI references used.

Unknown

Any RAM pages that the system could not classify into one of the other more specific items. Currently, this contains mostly native allocations, which cannot be identified by the tool when collecting this data due to Address Space Layout Randomization (ASLR). Like the Dalvik heap, thePss Totalfor Unknown takes into account sharing with Zygote, andPrivate Dirtyis unknown RAM dedicated to only your app.

TOTAL

The total Proportional Set Size (PSS) RAM used by your process. This is the sum of all PSS fields above it. It indicates the overall memory weight of your process, which can be directly compared with other processes and the total available RAM.

ThePrivate DirtyandPrivate Cleanare the total allocations within your process, which are not shared with other processes. Together (especiallyPrivate Dirty), this is the amount of RAM that will be released back to the system when your process is destroyed. Dirty RAM is pages that have been modified and so must stay committed to RAM (because there is no swap); clean RAM is pages that have been mapped from a persistent file (such as code being executed) and so can be paged out if not used for a while.

ViewRootImpl

The number of root views that are active in your process. Each root view is associated with a window, so this can help you identify memory leaks involving dialogs or other windows.

AppContextsandActivities

The number of appContextandActivityobjects that currently live in your process. This can help you to quickly identify leakedActivityobjects that can’t be garbage collected due to static references on them, which is common. These objects often have many other allocations associated with them, which makes them a good way to track large memory leaks.

Note:AVieworDrawableobject also holds a reference to theActivitythat it's from, so holding aVieworDrawableobject can also lead to your app leaking anActivity.

Trigger memory leaks

While using the tools described above, you should aggressively stress your app code and try forcing memory leaks. One way to provoke memory leaks in your app is to let it run for a while before inspecting the heap. Leaks will trickle up to the top of the allocations in the heap. However, the smaller the leak, the longer you need to run the app in order to see it.

You can also trigger a memory leak in one of the following ways:

Rotate the device from portrait to landscape and back again multiple times while in different activity states. Rotating the device can often cause an app to leak anActivity,Context, orViewobject because the system recreates theActivityand if your app holds a reference to one of those objects somewhere else, the system can't garbage collect it.

Switch between your app and another app while in different activity states (navigate to the Home screen, then return to your app).

Tip:You can also perform the above steps by using themonkeytest framework. For more information on running the monkey test framework, read themonkeyrunnerdocumentation.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,684评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,143评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,214评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,788评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,796评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,665评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,027评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,679评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,346评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,664评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,766评论 1 331
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,412评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,015评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,974评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,073评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,501评论 2 343

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,369评论 0 23
  • 6000下的跳绳结束,已经大汗滴滴,脸上因为运动的加剧,跟红屁股一样潮红哈!每次坚持不下来的时候,就鼓励自己再坚持...
    圆圆的湘气阅读 265评论 3 2
  • 文 艾米 金秋九月,时值秋高气爽,瓜果飘香。 北方的城市里应是枫叶火红,银杏飘黄一片浪漫的景象,而南方的城市里还...
    月影清韵阅读 2,204评论 100 175
  • 我们中国人打招呼的方式很奇怪,都喜欢问别人“吃了吗”。这时无论你吃没吃,都必须说吃了。 你要说你没吃,那你就是品格...
    强强说阅读 391评论 0 2