当前位置: 首页 > news >正文

毕节市网站建设定制wordpress主题多少钱

毕节市网站建设,定制wordpress主题多少钱,网站平台建设需要多少钱,安康市网页设计培训内存泄漏是指无用对象(不再使用的对象)持续占有内存或无用对象的内存得不到及时释放,从而造成内存空间的浪费称为内存泄漏。 平时我们在使用app时,少量的内存泄漏我们是发现不了的,但是当内存泄漏达到一定数量时&…

内存泄漏是指无用对象(不再使用的对象)持续占有内存或无用对象的内存得不到及时释放,从而造成内存空间的浪费称为内存泄漏。

平时我们在使用app时,少量的内存泄漏我们是发现不了的,但是当内存泄漏达到一定数量时,可能会引起OOM(Out of memory)

我们可以借助工具LeakCanary来检测内测泄漏

首先在项目build.gradle中引入LeakCanary库

  //https://github.com/square/leakcanary 内存泄漏分析工具(debugImplementation 来配置依赖,只在开发环境中依赖)debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.12'

当有内存泄漏时,会在通知栏中有通知,所以我们可以使用debugImplementation 来配置依赖,只在开发环境中依赖,这样正式包里不会引用LeakCanary库。

使用LeakCanary 2.0以上版本,无需做任何配置,引入库后就会自动检测内存泄漏

单例模式导致的内存泄漏

创建一个单例

public class Singleton {private static Singleton sInstance;private Context mContext;private Singleton(Context context) {this.mContext = context;}public static Singleton getInstance(Context context) {if (sInstance == null) {sInstance = new Singleton(context);}return sInstance;}public void test(){mContext.getContentResolver();}
}

在Activity中执行

public class TestLeakActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_test_leak);Singleton.getInstance(this).test();}
}

执行完单例之后看一下leakcanary

LeakCanary: 1 APPLICATION LEAKS
LeakCanary: ┬───
LeakCanary: │ GC Root: Local variable in native code
LeakCanary: │ ...            
LeakCanary: ├─ com.example.myapplication.Singleton instance
LeakCanary: │    Leaking: UNKNOWN
LeakCanary: │    Retaining 112915 bytes in 1674 objects
LeakCanary: │    mContext instance of com.example.myapplication.TestLeakActivity with mDestroyed = true
LeakCanary: │    ↓ Singleton.mContext
LeakCanary: │                ~~~~~~~~
LeakCanary: ╰→ com.example.myapplication.TestLeakActivity instance
LeakCanary: ​     Leaking: YES (ObjectWatcher was watching this because com.example.myapplication.TestLeakActivity received
LeakCanary: ​     Activity#onDestroy() callback and Activity#mDestroyed is true)
LeakCanary: ​     Retaining 112903 bytes in 1673 objects
LeakCanary: ​     key = 297d72a4-5e9d-41bf-baba-6856105c73f0
LeakCanary: ​     watchDurationMillis = 5176
LeakCanary: ​     retainedDurationMillis = 139
LeakCanary: ​     mApplication instance of com.example.myapplication.MyApplication
LeakCanary: ​     mBase instance of android.app.ContextImpl, not wrapping known Android context
LeakCanary: ====================================
LeakCanary: 0 LIBRARY LEAKS

发现UNKNOW 出现地方为Singleton中的mContext,说明当前的mContext可能没有释放掉,但是后续又看到YES说明当前确实没有释放掉

解决方案
将context变成ApplicationContext,当应用关掉之后,会自动回收ApplicationContext

 private Singleton(Context context) {this.mContext = context.getApplicationContext();}
http://www.yayakq.cn/news/213203/

相关文章:

  • 沈阳网站建站wordpress ping optimizer
  • 网站搭建就来徐州百度网络非常好crm排名
  • 商贸公司网站建设五百人建站
  • 济南做网站软件做网站网页的公司
  • 长春网站建设团队四合一网站建设
  • 燕郊个人做网站临沂做商城网站的公司
  • 专做hip hop音乐的网站百度公司做网站优化多少钱
  • 移动网站二级域名m开头怎么做广州网站建设定制多少钱
  • onedrive做网站下载盘wordpress 文章 作者
  • 网站开发前端兼职无锡网站设计无锡网站建设
  • h5网站动画怎么做开发公司清除地上树木侵犯了谁的权利
  • 政务网站平台建设 招标住建厅官网证件查询
  • 建设银行网站链接怎么做网络推广营销
  • 网站开发专业培训学校辅料企业网站建设费用
  • 学校网站建设调查表随州做网站
  • 东莞品牌网站建设费用如何做网站水晶头
  • 石家庄桥西区网站建设有什么手机网站
  • 企业网站的建立多少钱购物网站开发可行性
  • 在linux系统上用什么做网站h5网站开发软件下载
  • 网站建设域名申请wordpress插件放在主题的哪里
  • 成都网站制作费用交互型网站
  • 谷歌优化 网站建设济南做网站的好公司
  • 建设银行深圳天健世纪支行网站梭子手做鱼网站
  • 网站获取访客qq号品牌策划与推广实训报告
  • 帮做试卷的网站南昌网站维护制作
  • 技术支持 上海做网站域名怎么创建网站吗
  • 梅州哪里做网站wordpress 添加文章
  • 专门做相声的网站电子商务有限公司怎么注册
  • 做文明人网站专题wordpress导出数据库结构
  • 网站正在建设中 html5直播电商玩法介绍