码迷,mamicode.com
首页 > 移动开发 > 详细

关于内存溢出的一些想法(Android)

时间:2014-11-03 19:21:13      阅读:357      评论:0      收藏:0      [点我收藏+]

标签: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)

标签:des   android   style   blog   http   io   color   ar   os   

原文地址:http://www.cnblogs.com/hdolc/p/4071891.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!