查找内存中相同的 bitmap #1
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
极客时间账号: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 文件的神秘面纱,按照指定文件格式和协议解析即可。本代码还不完善,
提交有些匆忙,体现一下大致思路。