以避免内存的最佳方式泄漏的Andr​​oid片段 [英] The best way to avoid memory leaks in android fragment

查看:128
本文介绍了以避免内存的最佳方式泄漏的Andr​​oid片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有很多的方式,以避免在Android的碎片内存泄漏,这是最好的办法吗?

1.设置视图为空时onDestroyView被称为

 公共类LeakyFragment扩展片段{    私人查看mLeak; //保留    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        setRetainInstance(真);
    }    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
        mLeak = inflater.inflate(R.layout.whatever,集装箱,FALSE);
        返回mLeak;
    }    @覆盖
    公共无效onDestroyView(){
        super.onDestroyView();
        mLeak = NULL; //现在清理!
    }
}

2.设置所有的子视图= null,并且移除视图

  @覆盖
    公共无效onDestroyView(){
        super.onDestroyView();
        unbindDrawables(mLeak);
    }    私人无效unbindDrawables(查看视图){
        如果(view.getBackground()!= NULL){
            view.getBackground()setCallback(空)。
        }
        如果(查看的instanceof的ViewGroup和放大器;&安培;!(查看的instanceof适配器视图)){
            的for(int i = 0;我≤((ViewGroup中)视图).getChildCount();我++){
                unbindDrawables(((ViewGroup中)视图).getChildAt(一));
            }
            ((ViewGroup中)视图).removeAllViews();
        }
    }


解决方案

设置为的变量,并不意味着它会得到GC'd。如果没有其他强引用到任何地方它只会GC'd。

设置 setRetainInstance(真)不会使你的片段泄漏本身,它只是preserves了在片段实例跨越配置更改。它可以被看作是自觉泄漏,因为你是在告诉框架要保留在片段对象过去电流活动的生命周期。

现在的片段会泄露你的活动如果它不是一个UI少片段。这是因为片段具有UI将举行UI组件的引用(即 TextViews EditTexts 等),而这些查看按住活动的参考上下文。为了避免这种情况,你需要所有这些引用设置为,你在做什么。

此外,你可能还需要从其父删除 mLeak 以及

I found many way to avoid memory leaks in android fragment, which is the best way?

1.Set the view to null when onDestroyView is called

public class LeakyFragment extends Fragment{

    private View mLeak; // retained

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mLeak = inflater.inflate(R.layout.whatever, container, false);
        return mLeak;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mLeak = null; // now cleaning up!
    }
}

2.Set all the child view = null and remove the view

    @Override
    public void onDestroyView(){
        super.onDestroyView();
        unbindDrawables(mLeak);
    }

    private void unbindDrawables(View view){
        if (view.getBackground() != null){
            view.getBackground().setCallback(null);
        }
        if (view instanceof ViewGroup && !(view instanceof AdapterView)){
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++){
                unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
            ((ViewGroup) view).removeAllViews();
        }
    }

解决方案

Setting a variable to null does not mean that it will get GC'd. It will only be GC'd if there are no other strong references to it anywhere.

Setting setRetainInstance(true) does not make your Fragment leak per se, it just preserves that instance of the Fragment across configuration changes. It may be considered a "conscious leak", since you are telling the framework you want to retain the Fragment object past the current Activity's lifecycle.

Now the Fragment will leak your Activity if it is not a UI-less Fragment. This happens because Fragments that have a UI will hold references to UI components(i.e. TextViews, EditTexts, etc), and these Views hold a reference of the Activity's Context. In order to avoid this, you need to set all of those references to null as you are doing.

Also, you probably also need to remove mLeak from its parent as well.

这篇关于以避免内存的最佳方式泄漏的Andr​​oid片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆