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. 尽量使用静态类(全局),避免非静态的内部类,避免生命周期问题,注意内部类对外部对象引用导致的生命周期变化



更多相关文章

  1. Android(安卓)模块化编程之引用本地的aar
  2. Android(安卓)官方博客 - Android应用程序的内存分析
  3. Android引用ttf图标字体库
  4. Android(安卓)application context/activity context与内存泄露
  5. Android(安卓)application context/activity context与内存泄露
  6. Android(安卓)application context/activity context与内存泄露
  7. Android(安卓)application context/activity context与内存泄露
  8. Android源码阅读分析:ActivityManagerService分析(二)——Activity
  9. Android(安卓)Context 使用时注意内存泄漏问题

随机推荐

  1. Project Volta 让 Android 续航提升了多
  2. android:id="@+id/title"、android:id="@
  3. android 管理手机短信
  4. Android 鲜为人知的 8 个小秘密
  5. android企业级商城源码、360°全景图VR源
  6. Android必备:Android UI控件的了解与学习
  7. Android渗透测试Android渗透测试入门教程
  8. 独立的android开发者开发app如何盈利
  9. Android知识图谱:我们到底需要学习哪些And
  10. Android最佳实践(六)之扫描二维码模块