Skip to content

查找内存中相同的 bitmap #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

xushengming
Copy link

极客时间账号: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 文件的神秘面纱,按照指定文件格式和协议解析即可。本代码还不完善,
提交有些匆忙,体现一下大致思路。

@diankuangliuxu
Copy link

你用的Bitmap是在AndroidP的版本吗,在这个版本中的Bitmap是没有mBuffer属性的,在23的版本中的bitmap中是有mBuffer的。hprof是通过Profile生成的还是Debug.dumpHprofData生成呢。

@xushengming
Copy link
Author

@diankuangliuxu 我试了两个 hprof 文件,Android P 上没有 mBuffer,pre Android P 上有。
我 hprof 文件是通过 AndroidStudio 的 Android Profiler dump 的。

@20020263
Copy link

2、构造引用链时,getNextInstanceToGcRoot 返回空,不确定此处是否使用有误,没有
看到其他合适的 api,接下来需要深入研究下,学习下 LeakCanary 的处理逻辑。

返回为空或者值等于Integer.MAX_VALUE是因为当前的对象是可回收但没被回收的,
我的理解是这样的

@20020263
Copy link

@diankuangliuxu 我试了两个 hprof 文件,Android P 上没有 mBuffer,pre Android P 上有。
我 hprof 文件是通过 AndroidStudio 的 Android Profiler dump 的。

系统8.0以后,bitmap数据放到natvie去了,所以应该使用系统3.0至7.0的版本实验

@AnluTong
Copy link

2、构造引用链时,getNextInstanceToGcRoot 返回空,不确定此处是否使用有误,没有
看到其他合适的 api,接下来需要深入研究下,学习下 LeakCanary 的处理逻辑。

返回为空或者值等于Integer.MAX_VALUE是因为当前的对象是可回收但没被回收的,
我的理解是这样的

是因为没有构造,需要调用snapshot.computeDominators()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants