Skip to content

提交作业-分析内存相同图片 #8

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 2 commits into
base: master
Choose a base branch
from

Conversation

ShinyZeng
Copy link

极客账号:18718572230
先看下输出:
{
"hash":566918239,
"size":24964,
"width":79,
"height":79,
"duplicatedCount":2,
"stack":[

  • thread java.lang.Thread.!()! (named 'main')
  • ↳ android.app.ActivityThread.!(mActivities)!
  • ↳ android.util.ArrayMap.!(mArray)!
  • ↳ array java.lang.Object[].!([1])!
  • ↳ android.app.ActivityThread$ActivityClientRecord.!(activity)!
  • ↳ com.zengshaoyi.webwindowdemo.test1.Test1Activity.!(mImageView)!
  • ↳ android.widget.ImageView.!(mDrawable)!
  • ↳ android.graphics.drawable.BitmapDrawable.!(mBitmapState)!
  • ↳ android.graphics.drawable.BitmapDrawable$BitmapState.!(mBitmap)!
  • ↳ android.graphics.Bitmap
    ,
  • thread java.lang.Thread.!()! (named 'main')
  • ↳ android.app.ActivityThread.!(mBoundApplication)!
  • ↳ android.app.ActivityThread$AppBindData.!(info)!
  • ↳ android.app.LoadedApk.!(mResources)!
  • ↳ android.content.res.Resources.!(mResourcesImpl)!
  • ↳ android.content.res.ResourcesImpl.!(mDrawableCache)!
  • ↳ android.content.res.DrawableCache.!(mUnthemedEntries)!
  • ↳ android.util.LongSparseArray.!(mValues)!
  • ↳ array java.lang.Object[].!([17])!
  • ↳ java.lang.ref.WeakReference.!(referent)!
  • ↳ android.graphics.drawable.BitmapDrawable$BitmapState.!(mBitmap)!
  • ↳ android.graphics.Bitmap
    ]
    }
    实现思路:
    1.通过haha库中 MemoryMappedFileBuffer 和 HprofParser 解析 hprof 文件,生成 Snapshot
    2.snapshot 通过 findClass 查找 Bitmap ClassObj 对象
    3.通过 snapshot 获取 app 和 default 的 Heap 对象,通过 classObj 搜集Bitmap 的 Instance List
    4.遍历上面收集的List,存入一个key为 mBuffer hash 值,value 为 List 的Map中
    5.Map中去除没有重复的元素(value size 为1 就是没有重复的)并创建数据结构
    6.获取 Trace,这里感谢前面小哥们铺路,我两个方法都尝试了:
    1.getNextInstanceToGcRoot 这个方法前有说返回null,需要调用 snapshot.computeDominators() 才会返回
    2.通过使用LeakCanary的方式,调用 HeapAnalyzer.findLeakTrace 方式
    两者输出的形式有点不一样,后面采用的是 LeakCannary 的方式,因为这个方式可以提供的信息更多一点(作业中我将输出的信息修改输出类全路径)

疑问和体会:
相关搜索资料比较少,可能我找的方向不太对,作业提供的信息是 Haha 库 和 perflib ,google下来的信息也比较少,直接啃源码比较难,后面查看了 LeakCanary 源码,加上有同学 pr 了,研究同学作业,最后慢慢才摸索出来。

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.

1 participant