简书链接:检测内存泄漏、优化的常用手法笔记
文章字数:251,阅读全文大约需要1分钟

  • 可视化自动内存泄漏检测
    1
    2
    //    debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.3' ///屏蔽提升操作速度,开启后不需要任何代码的。 原理是内容提供者

  • 开启自带的检测
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if(BuildConfig.DEBUG){
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
// .detectLeakedSqlLiteObjects()
.detectFileUriExposure()
.detectActivityLeaks()
.detectLeakedClosableObjects()
.detectLeakedRegistrationObjects()
.detectLeakedSqlLiteObjects()
// .detectLeakedClosableObjects()
// .detectAll()
.penaltyLog()
.penaltyDropBox()
.build());
}
  • Android Studio Profiler检测

一个APP通常只分配256 或者512大小的内存,查看自己应用使用了多少,

比较麻烦的还有很多,官网可查,目前这个是比较方便的。

  • 测试
    1
    2
    3
    4
    5

    adb shell monkey -p com.sotrun.app --throttle 380 -v -s 3500 300000 >C:\monkey_log.txt
    停止方法
    ps -A|grep monkey
    kill -9
    更高级的需要写代码测试,比较麻烦。