Android生成heap dump文件(.hprof) 使用koom实现(快手)

Add a fast dumper with HeapDumper API. #2387

Fast dump

@pyricau As we discussed in #2047 & #2121, I add a fast dumper with HeapDumper API #2237 in KOOM v2.2.0.

How to use it

  1. Add a dependency in your build.gradle
dependencies {
    implementation "com.kuaishou.koom:koom-fast-dump:2.2.0"
}
  1. Initialize koom fast dump and set HeapDumper, then enjoy it.
    DefaultInitTask.init(applicationContext as Application)
    LeakCanary.config = LeakCanary.config.copy(
      heapDumper = HeapDumper {
        ForkJvmHeapDumper.getInstance().dump(it.absolutePath)
      })

Additional information

  1. Compatibility
  • Support Android L and above(API level >= 21)
  • Support armeabi-v7a arm64-v8a x86 x86-64
  1. Min Sdk
    The minSdkVersion of fast dump is 18. If the minSdkVersion of your app is lower than that, it needs to be compatible with overrideLibrary in the manifest.
  <uses-sdk tools:overrideLibrary="com.kwai.koom.fastdump, com.kwai.android.base, com.kwai.koom.base" />
  1. STL Support
    Fast dump support both shared and static libc++, if you are worried about STL conflicts, please use the static version, or you will prefer to the shared version for package size.
dependencies {
  // In shared mode, multiple modules share the same libc++_shared.so (STL), and the package 
  // size is small, but when multiple modules depend on different STL versions, the final 
  // compilation will conflict.
  implementation "com.kuaishou.koom:koom-fast-dump:2.2.0"
  // Or in static mode, each module has its own STL, the package size is large, and there are no 
  // compilation and runtime problems.
  implementation "com.kuaishou.koom:koom-fast-dump-static:2.2.0"
}
  1. Key logcat
    Please check logcat prefix with OOMMonitor to see if fast dump works.
16743 16766 I OOMMonitor_ForkJvmHeapDumper: dump xxx.hprof. 16743 16766 I OOMMonitor_ForkJvmHeapDumper: before suspend and fork. // The pid changes from 16743 to 16807, and the child process starts to dump 16807 16807 I mple.leakcanar: hprof: heap dump "xxx.hprof" starting... // The child process dump finished, it takes 6.4s 16807 16807 I mple.leakcanar: hprof: heap dump completed (24MB) in 6.411s objects 330914 objects with stack traces 0 16807 16807 I JNIBridge: process 16807 will exit! // The main process is notified by the completion of the child process dump 16743 16766 I OOMMonitor_ForkJvmHeapDumper: dump true, notify from pid 16807

转自:https://github.com/square/leakcanary/issues/2387

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将一个大的 .hprof 文件分割为多个小文件,可以避免在使用 jvisualvm 或其他工具分析 .hprof 文件时出现内存不足或程序崩溃的情况。 以下是一种将一个大的 .hprof 文件分割为多个小文件的方法: 1. 使用 jmap 命令生成一个 .hprof 文件,例如: ``` jmap -dump:file=heap.hprof <pid> ``` 其中,<pid> 是目标 Java 进程的进程 ID。 2. 使用 jhat 命令将 .hprof 文件转换为 HTML 文件,例如: ``` jhat -J-Xmx4g heap.hprof ``` 其中,-J-Xmx4g 选项指定了 jhat 工具使用 4GB 的内存来分析 .hprof 文件,你可以根据实际情况进行调整。 3. 在浏览器中打开 jhat 工具生成的 HTML 文件,并查找你要分割的对象。 4. 在浏览器中打开开发者工具(通常是按 F12 键),并切换到控制台面板。 5. 在控制台中输入以下命令: ``` var blob = new Blob([new Uint8Array(<object data>)]); var url = URL.createObjectURL(blob); ``` 其中,<object data> 是你要分割的对象在浏览器中的地址,可以在浏览器的地址栏中找到。 6. 将生成的 blob 和 url 打印出来,例如: ``` console.log(blob); console.log(url); ``` 7. 将输出的 blob 内容保存为一个二进制文件,例如: ``` echo <blob> > heap.part1.bin ``` 其中,<blob> 是上一步输出的 blob 内容。 8. 将输出的 url 保存为一个文本文件,例如: ``` echo <url> > heap.part1.url ``` 其中,<url> 是上一步输出的 url。 9. 重复步骤 4 到 8,直到将整个 .hprof 文件分割为多个小文件。 10. 在分析 .hprof 文件时,你需要将多个小文件合并为一个 .hprof 文件,例如: ``` cat heap.part*.bin > heap.bin ``` 然后使用 jvisualvm 或其他工具分析合并后的 .hprof 文件。 注意:在分割 .hprof 文件时,你需要确保每个小文件都包含完整的对象信息,否则可能会导致分析结果不准确。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值