Uh oh!
There was an error while loading. Please reload this page.
查找内存中相同的 bitmap - #1
Conversation
diankuangliuxu
commented
Dec 12, 2018
你用的Bitmap是在AndroidP的版本吗,在这个版本中的Bitmap是没有mBuffer属性的,在23的版本中的bitmap中是有mBuffer的。hprof是通过Profile生成的还是Debug.dumpHprofData生成呢。 |
xushengming
commented
Dec 12, 2018
@diankuangliuxu 我试了两个 hprof 文件,Android P 上没有 mBuffer,pre Android P 上有。 |
20020263
commented
Dec 13, 2018
2、构造引用链时,getNextInstanceToGcRoot 返回空,不确定此处是否使用有误,没有 返回为空或者值等于Integer.MAX_VALUE是因为当前的对象是可回收但没被回收的, |
20020263
commented
Dec 26, 2018
系统8.0以后,bitmap数据放到natvie去了,所以应该使用系统3.0至7.0的版本实验 |
AnluTong
commented
Mar 18, 2019
是因为没有构造,需要调用snapshot.computeDominators() |
极客时间账号:15101611795
原理:主要是使用了 HAHA 库对 hprof 文件解析,HAHA 内部也是用 perflib 来解析的。
1、使用 MemoryMappedFileBuffer 和 HprofParser 分别来读取解析生成快照。
2、从快照中可以读取想要的 heap,比如 default heap 和 app heap。
3、从快照中调用 findClass 找到属于 Bitmap 的对象
4、找到属于指定堆的 Bitmap 们,得到一个 Instance 列表。
5、接着对 Instance 操作即可,能够在其中找到 mBuffer、mWidth、mHeight 等
属性。通过 getNextInstanceToGcRoot 可以构建引用链。
6、根据 mBuffer 的 MD5 来归类 Instance。
7、最后构造输出即可。
疑问和体会:
1、调试时我的 hprof 文件中没有看到 mBuffer 属性,可能和 Android 系统版本有关。
2、构造引用链时,getNextInstanceToGcRoot 返回空,不确定此处是否使用有误,没有
看到其他合适的 api,接下来需要深入研究下,学习下 LeakCanary 的处理逻辑。
3、揭开了解析 hprof 文件的神秘面纱,按照指定文件格式和协议解析即可。本代码还不完善,
提交有些匆忙,体现一下大致思路。