關於記憶體溢出的一些想法(Android)

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   io   color   ar   os   

 

 

Android應用程式視窗(Activity)的運行上下文環境(Context)的建立過程分析

 

關於記憶體溢出的一些想法(Android)

http://m.oschina.net/blog/54936

 

1. 能用 Application  的 上下文 就用。 因為如果用 Activity 的 Context ,如果用太多的 Activity, 如果 有些資源 還在引用 Activity的context的資源,會導致 這個 Activity 沒有被回收,有可能導致 oom、

 

2. bitmap 回收。 在 Fragment 中 可以解除綁定,但不要 recycle()。 如果 要 recycle 要 確定 這個 bitmap 已經不再使用了,例如左右滑動的 Fragment 或者 Activity, bitmap 就不要 recycle。 但可以解除綁定。

 

3. Dialog 用完之後, dismiss 之後,設為 null。

 

4. 圖片不要做記憶體緩衝,可以做磁碟緩衝。 例如 Volley 的 下載圖片。 做磁碟緩衝

 

源碼分析:

    protected void onDestroy() {        if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);        mCalled = true;        // dismiss any dialogs we are managing.        if (mManagedDialogs != null) {            final int numDialogs = mManagedDialogs.size();            for (int i = 0; i < numDialogs; i++) {                final ManagedDialog md = mManagedDialogs.valueAt(i);                if (md.mDialog.isShowing()) {                    md.mDialog.dismiss();                }            }            mManagedDialogs = null;        }        // close any cursors we are managing.        synchronized (mManagedCursors) {            int numCursors = mManagedCursors.size();            for (int i = 0; i < numCursors; i++) {                ManagedCursor c = mManagedCursors.get(i);                if (c != null) {                    c.mCursor.close();                }            }            mManagedCursors.clear();        }        // Close any open search dialog        if (mSearchManager != null) {            mSearchManager.stopSearch();        }        getApplication().dispatchActivityDestroyed(this);    }

 

// 下面這些操作 要在 onDestroy 前執行

    @Override    protected void onDestroy() {          //停止線程        CommonUtils.stopAsyncTask(mHasMsgTask);          CommonUtils.stopAsyncTask(mGetExtraDataTask);        statTimer.cancel();        extraDataTimer.cancel();        hasSystemMsgTimer.cancel();        hasLetterTimer.cancel();        super.onDestroy();    }

 

 

圖片解除綁定:

com.xingjiabi.shengsheng.base.XjbBaseActivity

    protected static void unbindDrawable(Activity activity) {        View rootView = null;        try {            rootView = ((ViewGroup) activity.findViewById(android.R.id.content))                    .getChildAt(0);        } catch (Exception e) {            e.printStackTrace();        }        if (rootView != null) {            PictureUtil.unbindDrawables(rootView);        }    }

 

 

    /**     * final 在 引用執行完後,就會被銷毀。 Activity 銷毀的時候 就會被銷毀?     * static 在Application 結束 之後 才會被銷毀     */

 

方法1: 

 

以前 請求 網狀圖片 的 最佳化方法是:

在 ListView 中 把 postion 傳入到請求圖片的方法中,請求成功後, 在根據 postion 擷取到對於的View ,再設值。 這樣 子線程中 就不會佔有View,導致不能Context 沒有釋放。

 

方法2:

現在 開源的,例如 Volley 是把 imgView 引用傳入。

 

 

關於 Handler 的 static

Handler 寫成 static 有個好處是 不在依賴 Activity 的 Context,這樣 就算 Handler 被 子線程 引用的時候,也不會導致 Activity 結束的時候,由於 子線程 還在引用 Handler,導致 Activity 的 Context 其實 沒有被回收。 

 

 

 

 

 

 Android(Java)中常見的容易引起記憶體流失的不良代碼:

 

1. 查詢資料庫沒有關閉遊標

2. 構造Adapter時,沒有使用緩衝的 convertView 

3. Bitmap對象不在使用時調用recycle()沒有及時釋放 

4.沒有及時釋放對象的引用

    簡單舉個例子:比如兩個Activity之間傳遞的Context 或其它的自訂對象,使用完後必須立即釋放 即:Activity = null ;    Context = null ; Object = null;可以的話在這釋放對象之後通知系統來回收:System.gc();這樣最好了!

 

關於記憶體溢出的一些想法(Android)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.