【android大杂烩】Debug.startMethodTracing文件大小为0

3,692 阅读1分钟

如果我的文章对您有所帮助,欢迎 👍 , 我的Github.


android中提供了android.os.Debug#startMethodTracing()方法协助应用分析CPU使用情况。 可以在被分析场景开始时调用android.os.Debug.startMethodTracing(),场景结束时调用android.os.Debug.stopMethodTracing()生成trace文件。 trace文件生成的默认地址在 Context#getExternalFilesDir(String) 也就是/sdcard/Android/$packagename/中。

最近在用这个分析性能时发现一个问题,当在Application oncreate 中调用android.os.Debug.startMethodTracing()时,trace文件的size一直是0。 但不在Application中调用是可以的。

查了官方文档,stackoverflow,都是说只要调用了 startMethodTracing 和 stopMethodTracing就能生成trace文件,但是为什么在application中不行呢。

分析可能原因:

  • application调用时,trace还不起作用
  • application被多次初始化

最终通过demo模拟发现是app有多个进程导致的,多个进程都调用了android.os.Debug#startMethodTracing(),导致文件被重复持有,在写入的时候无法写入成功。

~