Android---性能优化方案分享,kotlin随机数

本文分享了Android应用性能优化中的插桩技术,通过Debug类记录.trace文件,并探讨了使用Android Studio和traceview查看及分析这些文件的方法。此外,还介绍了CPU Profiler的使用,以无侵入的方式进行函数跟踪。
摘要由CSDN通过智能技术生成
一、插桩

插桩需要使用到 Debug 类,并且会在 sdcard 中生成 .trace 文件,所以你必须首先保证你的应用具有写外部存储( WRITE_EXTERNAL_STORAGE )的权限。

在想要跟踪的代码逻辑开头和结尾处分别插桩:

// Starts recording a trace log with the name you provide. For example, the

// following code tells the system to start recording a .trace file to the

// device with the name “sample.trace”.

Debug.startMethodTracing(“sample”);

// The system begins buffering the generated trace data, until your

// application calls stopMethodTracing(), at which time it writes

// the buffered data to the output file.

Debug.stopMethodTracing();

生成的 .trace 文件会被保存在固定目录下,与 getExternalFilesDir() 返回的目录相同,即 /sdcard/Android/data/[YOUR_PACKAGE_NAME]/files 下。
请注意,如果您的应用在未更改跟踪日志名称的情况下再次调用 startMethodTracing(),则会覆盖已保存至设备的现有日志。如果希望每次运行都保存至不同的日志文件,可以使用如下代码:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值