歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android context(Application/Activity)與內存洩露

Android context(Application/Activity)與內存洩露

日期:2017/3/1 11:10:14   编辑:Linux編程

Android中的context可以做很多操作,但是最主要的功能是加載和訪問資源。

在android中有兩種context,一種是 application context,一種是activity context,通常我們在各種類和方法間傳遞的是activity context。

比如一個activity的onCreate:
  1. public void onCreate(Bundle savedInstanceState) {
  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 = new GameView(this);
  8. setContentView(mGameView);
  9. }
把activity context傳遞給view,意味著view擁有一個指向activity的引用,進而引用activity UI占有的資源:view , resource, SensorManager等。
但是這樣如果context發生內存洩露的話,就會洩露很多內存,這裡洩露的意思是gc沒有辦法回收activity的內存(當前Activity為活動或finish後還沒來得及回收)。

Leaking an entire activity是很容易的一件事。
當屏幕旋轉的時候,系統會銷毀當前的activity,保存狀態信息再創建一個新的。

比如我們寫了一個應用程序,需要加載一個很大的圖片,我們不希望每次旋轉屏幕的時候都銷毀這個圖片重新加載。

實現這個要求的簡單想法就是定義一個靜態的Drawable,這樣Activity 類創建銷毀它始終保存在內存中,訪問速度會很快。

實現類似:
  1. public class myactivity extends Activity {
  2. private static Drawable sBackground;
  3. protected void onCreate(Bundle state) {
  4. super.onCreate(state);
  5. TextView label = new TextView(this);
  6. label.setText("Leaks are bad");
  7. if (sBackground == null) {
  8. sBackground = getDrawable(R.drawable.large_bitmap);
  9. }
  10. label.setBackgroundDrawable(sBackground);//drawable attached to a view
  11. setContentView(label);
  12. }
  13. }
這段程序看起來很簡單,但是卻問題很大。當屏幕旋轉的時候會有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":

  1. <application android:icon="@drawable/icon"
  2. android:label="@string/app_name"
  3. android:name=".GApplication">
  4. <activity android: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. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. </application>

避免context相關的內存洩露,記住以下幾點:
1. 不要讓生命周期長的對象引用activity context,即保證引用activity的對象要與activity本身生命周期是一樣的
2. 對於生命周期長的對象,可以使用application context (繼承類:public class GApplication extends Application)
3. 盡量使用靜態類(全局),避免非靜態的內部類,避免生命周期問題,注意內部類對外部對象引用導致的生命周期變化
Copyright © Linux教程網 All Rights Reserved