android中context可以做很多操作,但是最主要的功能是加载和访问资源。

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

比如一个activity的onCreate:
[java] view plain copy print ?
  1. publicvoidonCreate(BundlesavedInstanceState){
  2. super.onCreate(savedInstanceState);
  3. requestWindowFeature(Window.FEATURE_NO_TITLE);
  4. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  5. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  6. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  7. mGameView=newGameView(this);
  8. setContentView(mGameView);
  9. }
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 类创建销毁它始终保存在内存中,访问速度会很快。

实现类似:
[java] view plain copy print ?
  1. publicclassmyactivityextendsActivity{
  2. privatestaticDrawablesBackground;
  3. protectedvoidonCreate(Bundlestate){
  4. super.onCreate(state);
  5. TextViewlabel=newTextView(this);
  6. label.setText("Leaksarebad");
  7. if(sBackground==null){
  8. sBackground=getDrawable(R.drawable.large_bitmap);
  9. }
  10. label.setBackgroundDrawable(sBackground);//drawableattachedtoaview
  11. setContentView(label);
  12. }
  13. }
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 plain copy print ?
  1. <applicationandroid:icon="@drawable/icon"
  2. android:label="@string/app_name"
  3. android:name=".GApplication">
  4. <activityandroid:name=".WordSearch"
  5. android:label="@string/app_name"
  6. android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
  7. android:screenOrientation="portrait"
  8. android:configChanges="keyboardHidden|orientation|keyboard|screenLayout">
  9. <intent-filter>
  10. <actionandroid:name="android.intent.action.MAIN"/>
  11. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  12. </intent-filter>
  13. </activity>
  14. </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. 尽量使用静态类(全局),避免非静态的内部类,避免生命周期问题,注意内部类对外部对象引用导致的生命周期变化

更多相关文章

  1. android中禁止横屏和竖屏切换
  2. Android使用Application总结
  3. 编译android文件系统4.0.4错误全部解析
  4. Android(安卓)application context/activity context与内存泄露
  5. Android中的?attr/和?android:attr/
  6. Android:activity context, application context和内存泄露
  7. Ubuntu环境下下载编译Android(安卓)kernel源码
  8. Android(安卓)application context/activity context与内存泄露
  9. Android(安卓)application context/activity context与内存泄露

随机推荐

  1. Android(安卓)4.4 Kitkat Phone工作流程
  2. Android(安卓)之 getSharedPreferences
  3. Android简单发送邮件(可带附件)
  4. Android进阶(六)文件读操作
  5. Android欢迎界面的创建方法
  6. (转载)Android平台,jni调用原始C运态库技
  7. AIDL使用详解(一)
  8. 【Android(安卓)UI】案例03滑动切换效果
  9. 【Android高级工程师】Android项目开发如
  10. Android(安卓)res/layout中布局文件管理