android application被限制到16M堆内存。 越多的application保存在内存中,那么在他们之间切换的时候,速度就会更快。 a? 在一个规则的application中,你通常用俩中 当屏幕方向转变时,默认摧毁当前的activity 创建一个新的,android将重新加载application的ui。 如果你有一个大的bitmap你不想每次转变屏幕时都加载,如果你用一个static去总是保持它 private?static?Drawable?sBackground?; @Override protected?void?onCreate?(?Bundle?state?) {???super?.?onCreate?(?state?);?? TextView?label?=?new?TextView?(?this?);?? label?.?setText?(?"Leaks are bad"?);?? if?(?sBackground?==?null?)?{?? ? sBackground?=?getDrawable?(?R?.?drawable?.?large_bitmap?); ??}?? label?.?setBackgroundDrawable?(?sBackground?);?? setContentView?(?label?);?} 代码很快,但是也很错。他泄露了这第一个activity,当一个drawable被?attached to a view,这个VIEW被设置作为一个callback在这个drawable,在上述代码中,这意味着这个drawable有一个引用在 textview,而这个textview本身有一个引用在这个activity上,而这个activity依次有许多他自身的引用。你可以参看home screen源代码通过设置drawble的callback为空当activity被摧毁时。 /** * Remove the callback for the cached drawables or we leak the previous* Home screen on orientation change. */ FAQ: 1.If Drawable just a member field and not static - it’s fine right? An instance variable will not cause any problem indeed. 2.public void setBackgroundDrawable(Drawable d) { 从这个代码看出,上述例子里the setBackgroundDrawable reset the drawable’s callback after the activity reloaded. when the setBackgroundDrawable() is called, the Drawable has setCallback() invoked, which will overwrite the current callback, therefore no memory leak?Context?能被在很多方面应用,但是最多的是加载访问资源,这就是为啥widget接受一个a?Context?参数在构造函数里。Context,activity和application。 private void unbindDrawables(ArrayList<ItemInfo> desktopItems) { if (desktopItems != null) { final int count = desktopItems.size(); for (int i = 0; i < count; i++) { ItemInfo item = desktopItems.get(i); switch (item.itemType) { case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: ((ApplicationInfo)item).icon.setCallback(null); break; } } } }俩中避免与context相关联的内存泄露。The most obvious one is to avoid escaping the context outside of its own scope.上述例子展示了一个静态引用的例子。但是内部类和他们的隐士的外部引用能是同样危险的。这第二个解答是用application context。这个context将活着和你的application一样长,并不依赖这activity的生命周期。如果你计划保持长引用的对象,他需要一个context,记者用application的。通过Context.getApplicationContext()or Activity.getApplication()
…
if (d != null) {
…
d.setCallback(this);
…
}
…
}
详细解决方案
Android 内存储器泄露
热度:59 发布时间:2016-05-01 16:44:55.0
相关解决方案
- android 读取byte[]中的元素解决方案
- android 标题栏兑现方式
- android 中Activity向BroadcastReceiver发送数据,该怎么解决
- Android 4.0 为什么模拟器老是提示小弟我谷歌拼音输入法已停止
- android:getSharedPreferences() 这是哪个类的方法解决思路
- android 怎么判断一个程序是否联网
- android RadioButton如何设置默认选中
- android 怎么重新设置锚点
- android 图片对象获取的有关问题
- android 关于服务连接的疑义
- android 怎么实现对view的放大和缩小
- android ID,该如何处理
- 准备复习2-3个月,看java+android,请问有经验者,怎么看效果最好》
- android UI线程与AsyncTask的有关问题
- android(java) 中文乱码的有关问题
- android 动态设立控件高度
- Android test project 编译方法
- android 4.03启动出错,显示"Encryption Unsuccessful"该如何解决
- Android 下面的listView的动态效果怎么实现的?求源代码,多谢
- Android?Palm?Symbian?Windows Mobile?学哪个有“钱图”?解决方案
- Android 开发语言,该怎么解决
- 单个人去做手机开发选什么平台好?android,iphone?解决办法
- Android 虚拟机崩溃的有关问题
- 想学学移动平台的开发 android 还是windows phone7好啊该怎么处理
- Android 上的如何读写设备文件
- Android 2.2 API 汉语文档系列(3) —— AccessibilityService
- 【先睹为快】ArcGIS Android SDK上一版本新特性
- ArcGIS for Android app v2.0.4公布
- android.hardware.USB种介绍
- android linux开发 在线电子书解决方案