Android 内存泄露问题
来源:互联网 发布:增广贤文 知乎 编辑:程序博客网 时间:2024/04/29 20:24
关于Android中内存泄露问题,最近研究高通4.3联系人源码,分析一些心得。准备先写心得然后下一遍给出具体的代码分析,因为以前Android中Contacats是一个目录,现在分为了ContactsCommon、Contacts、Dialer三个模块。
具体代码分析请看下一遍文章,接下来先看总计。
android中的context可以做很多操作,但是最主要的功能是加载和访问资源。
在android中有两种context,一种是 application context,一种是activity context,通常我们在各种类和方法间传递的是activity context。
比如一个activity的onCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mGameView = new GameView(this);
setContentView(mGameView);
}
把activity context传递给view,意味着view拥有一个指向activity的引用,进而引用activity UI占有的资源:view , resource, SensorManager等。
但是这样如果context发生内存泄露的话,就会泄露很多内存,这里泄露的意思是gc没有办法回收activity的内存(当前Activity为活动或finish后还没来得及回收)。
Leaking an entire activity是很容易的一件事。
当屏幕旋转的时候,系统会销毁当前的activity,保存状态信息再创建一个新的。
比如我们写了一个应用程序,需要加载一个很大的图片,我们不希望每次旋转屏幕的时候都销毁这个图片重新加载。
实现这个要求的简单想法就是定义一个静态的Drawable,这样Activity 类创建销毁它始终保存在内存中,访问速度会很快。
实现类似:
public class myactivity extends Activity {
private static Drawable sBackground;
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);//drawable attached to a view
setContentView(label);
}
}
这段程序看起来很简单,但是却问题很大。当屏幕旋转的时候会有leak,即gc没法销毁activity
我们刚才说过,屏幕旋转的时候系统会销毁当前的activity。但是当drawable和view关联后,drawable保存了view的 reference,即sBackground保存了label的引用,而label保存了activity的引用。既然drawable不能销毁,它所引用和间接引用的都不能销毁,这样系统就没有办法销毁当前的activity,于是造成了内存泄露。gc对这种类型的内存泄露是无能为力的。
避免这种内存泄露的方法是避免activity中的任何对象的生命周期长过activity,避免由于对象对 activity的引用导致activity不能正常被销毁
同时,我们可以使用application context
application context伴随application的一生,与activity的生命周期无关。
application context可以通过Context.getApplicationContext或者Activity.getApplication方法获取。
使用Application,需要在 AndroidManifest.xml 文件注册,即android:name=".GApplication":
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:name=".GApplication">
<activity android:name=".WordSearch"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|keyboard|screenLayout">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
避免context相关的内存泄露,记住以下几点:
1. 不要让生命周期长的对象引用activity context,即保证引用activity的对象要与activity本身生命周期是一样的
2. 对于生命周期长的对象,可以使用application context (继承类:public class GApplication extends Application)
3. 尽量使用静态类(全局),避免非静态的内部类,避免生命周期问题,注意内部类对外部对象引用导致的生命周期变化
- Android 内存泄露问题
- android内存泄露问题
- android内存泄露的问题
- android内存泄露的问题
- android内存泄露的问题
- android内存泄露的问题
- android内存泄露的问题
- android内存泄露的问题
- android GC内存泄露问题
- android GC内存泄露问题
- android GC内存泄露问题
- android GC内存泄露问题
- android GC内存泄露问题
- android内存泄露的问题
- android内存泄露的问题
- android GC内存泄露问题
- android GC内存泄露问题
- android GC内存泄露问题
- PermGen space(内存溢出)
- jsoup
- 使用adb输入中文字符
- matlab编程入门学习(1)
- .NET动态设置和显示Titel、KeyWords、Description
- Android 内存泄露问题
- 屏蔽CTRL+ALT+DEL热键
- ArcGIS栅格重采样方法
- VC 使用msxml6.dll动态链接库中的函数读写XML文件
- SHELLEXECUTEINFO 和 ShellExecuteEx的使用
- mysql中时间日期的处理
- NDK不用cygwin,只用eclipse开发,照样能编译成 .so文件
- 文本框只能输入数字
- 清空vector容器的方法