您的位置:首页 > 移动开发

Android context(Application/Activity)与内存泄露

2012-02-02 09:54 405 查看
android中的context可以做很多操作,但是最主要的功能是加载和访问资源。

在android中有两种context,一种是 application context,一种是activity context,通常我们在各种类和方法间传递的是activity context。

比如一个activity的onCreate:

[java]
view plaincopyprint?

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);
}

[java]
view plaincopyprint?

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);
}
}

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":

[java]
view plaincopyprint?

<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>

<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. 尽量使用静态类(全局),避免非静态的内部类,避免生命周期问题,注意内部类对外部对象引用导致的生命周期变化
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: