Android内存泄露改进
来源:互联网 发布:java 时间戳转换成时间 编辑:程序博客网 时间:2024/05/22 07:01
Android开发最重要的就是保持系统运行的流畅性,为了减少内存使用、优化代码编程风格,本文将对于一些常见的错误进行介绍以及给出解决办法,希望能够通过本文介绍,能够对大家有所帮助。
本文主要通过编程常见错误以及sdk选择两个方面进行介绍
编程常见错误
Handle导致的内存泄露
描述:
This Handler class should be static or leaks might occur (anonymous android.os.Handler)
less… (⌘F1)
解决方法:
Since this Handler is declared as an inner class, it may prevent the outer class from being garbage collected. If the Handler is using a Looper or MessageQueue for a thread other than the main thread, then there is no issue. If the Handler is using the Looper or MessageQueue of the main thread, you need to fix your Handler declaration, as follows: Declare the Handler as a static class; In the outer class, instantiate a WeakReference to the outer class and pass this object to your Handler when you instantiate the Handler; Make all references to members of the outer class using the WeakReference object.
解读:
示例:
public class MyActivity extends Activity { private MyHandler mHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHandler = new MyHandler(this); } @Override protected void onDestroy() { // Remove all Runnable and Message. mHandler.removeCallbacksAndMessages(null); super.onDestroy(); } static class MyHandler extends Handler { // WeakReference to the outer class's instance. private WeakReference<MyActivity> mOuter; public MyHandler(MyActivity activity) { mOuter = new WeakReference<MyActivity>(activity); } @Override public void handleMessage(Message msg) { MyActivity outer = mOuter.get(); if (outer != null) { // Do something with outer as your wish. } } }}
静态变量导致的内存泄露
描述:
Static member ‘com.h2y.android.shop.activity.receiver.SMSBroadcastReceiver.mMessageListener’ accessed via instance reference less… (⌘F1)
解决方法:
Shows references to static methods and fields via class instance rather than a class itself.
Activity管理类导致的内存泄露
描述:
使用ActivityManager管理Activity时,一定不要忘记弹出finish掉的Activity实例,要不然存在引用,也会导致Activity在内存中无法释放而导致的内存泄露。
常见SDK内存泄露情况
百度地图
百度地图SDK本身存在内存泄露的情况。。。
- Android内存泄露改进
- 使用Handler引发内存泄露的改进
- Android 内存泄露
- android内存泄露 mat
- 预防Android内存泄露
- 预防Android内存泄露
- 预防Android内存泄露
- Android 避免内存泄露
- Android内存泄露分析
- Android内存泄露
- 避免Android内存泄露
- android 内存泄露
- Android 内存泄露测试
- 避免Android内存泄露
- android 内存泄露调试
- 避免Android内存泄露
- android内存泄露优化
- android 内存泄露 小结
- App启动加载广告页面思路
- solr5.5安装配置过程-本人测试通过
- gethostbyname
- 【1】mac下面iTerm配置oh-my-zsh教程
- RN-flexbox(弹性布局)
- Android内存泄露改进
- Linux网络配置
- Construct Binary Tree from Preorder and Inorder Traversal 根据前序遍历和中序遍历构造二叉树
- 一些元素的css样式重置
- 今天视图添加东西
- Android matrix.postScale的用法
- unity3d内置着色器
- boostrap双重tab嵌套结构
- ViewPager实现广告轮播