<legend id='LEWbO'><style id='LEWbO'><dir id='LEWbO'><q id='LEWbO'></q></dir></style></legend>
  • <tfoot id='LEWbO'></tfoot>

      <bdo id='LEWbO'></bdo><ul id='LEWbO'></ul>

      <small id='LEWbO'></small><noframes id='LEWbO'>

    1. <i id='LEWbO'><tr id='LEWbO'><dt id='LEWbO'><q id='LEWbO'><span id='LEWbO'><b id='LEWbO'><form id='LEWbO'><ins id='LEWbO'></ins><ul id='LEWbO'></ul><sub id='LEWbO'></sub></form><legend id='LEWbO'></legend><bdo id='LEWbO'><pre id='LEWbO'><center id='LEWbO'></center></pre></bdo></b><th id='LEWbO'></th></span></q></dt></tr></i><div id='LEWbO'><tfoot id='LEWbO'></tfoot><dl id='LEWbO'><fieldset id='LEWbO'></fieldset></dl></div>

        片段没有从内存中释放

        时间:2023-10-04
        <tfoot id='uY4Pq'></tfoot>

        • <small id='uY4Pq'></small><noframes id='uY4Pq'>

          • <bdo id='uY4Pq'></bdo><ul id='uY4Pq'></ul>
              <legend id='uY4Pq'><style id='uY4Pq'><dir id='uY4Pq'><q id='uY4Pq'></q></dir></style></legend>
              <i id='uY4Pq'><tr id='uY4Pq'><dt id='uY4Pq'><q id='uY4Pq'><span id='uY4Pq'><b id='uY4Pq'><form id='uY4Pq'><ins id='uY4Pq'></ins><ul id='uY4Pq'></ul><sub id='uY4Pq'></sub></form><legend id='uY4Pq'></legend><bdo id='uY4Pq'><pre id='uY4Pq'><center id='uY4Pq'></center></pre></bdo></b><th id='uY4Pq'></th></span></q></dt></tr></i><div id='uY4Pq'><tfoot id='uY4Pq'></tfoot><dl id='uY4Pq'><fieldset id='uY4Pq'></fieldset></dl></div>

                    <tbody id='uY4Pq'></tbody>
                1. 本文介绍了片段没有从内存中释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个包含 View Pager 的活动,该 View Pager 有一个适配器 FragmentStatePagerAdapter.每次进入activity会占用200mb的内存,退出activity(finish())后重新进入它会追加并加倍手机使用的内存.

                  I have an activity that contains a View Pager that has an adapter FragmentStatePagerAdapter. each time enter the activity it will take up 200mb of memory, after going back out of the activity(finish()) and then re entering it it will append and double the memory used on the phone.

                  在对问题进行故障排除后,片段管理器似乎没有释放片段,尽管我试图删除它们,但它只是不起作用.

                  After troubleshooting the problem it seems as if the fragment manager is not releasing the fragments although im trying to remove them but its just not working.

                  我尝试清空正在添加的片段,以确保它不是问题仍然存在的片段内部的东西.

                  I tried emptying the fragment that is being added to make sure its not something internal inside the fragment the the problem remains.

                  我的适配器代码是

                     private class ChildrenPagerAdapter extends FragmentStatePagerAdapter
                     {
                        private List<ChildBean> childrenBean;
                  
                        public ChildrenPagerAdapter(FragmentManager fm, List<ChildBean> bean)
                        {
                           super(fm);
                           this.childrenBean = bean;
                        }
                  
                        @Override
                        public int getItemPosition(Object object)
                        {
                           return PagerAdapter.POSITION_NONE;
                        }
                  
                        @Override
                        public Fragment getItem(int position)
                        {
                  
                           ReportFragment reportFragment = new ReportFragment();
                           reportFragment.childBean = childrenBean.get(position);
                           reportFragment.position = position;
                           reportFragment.mPager = mPager;
                           if(position == 0)
                           {
                              reportFragment.mostLeft = true;
                           }
                           if(position == childrenNumber - 1)
                           {
                              reportFragment.mostRight = true;
                           }
                  
                           return reportFragment;
                        }
                  
                        @Override
                        public int getCount()
                        {
                           return childrenNumber;
                        }
                  
                        @Override
                        public void destroyItem(ViewGroup container, int position, Object object)
                        {
                           // TODO Auto-generated method stub
                           super.destroyItem(container, position, object);
                        }
                     }
                  

                  我的活动代码是

                      public class ReportActivity extends CustomActivity
                  {
                     public ImageLoader imageLoader;
                     private ViewPager mPager;
                     private PagerAdapter mPagerAdapter;
                     private int childrenNumber;
                     private int currentChild;
                  
                     @Override
                     protected void onDestroy()
                     {
                        mPager.removeAllViews();
                        mPager.removeAllViewsInLayout();
                        mPager.destroyDrawingCache();
                        mPagerAdapter = null;
                        mPager = null;
                        System.gc();
                        super.onDestroy();
                     }
                  
                     @Override
                     protected void onCreate(Bundle savedInstanceState)
                     {
                  
                        super.onCreate(savedInstanceState);
                        setCustomTitle(string.title_activity_reports);
                        this.currentChild = getIntent().getIntExtra("itemselected", -1);
                  
                        getSupportFragmentManager().
                     }
                  
                     @Override
                     protected void onResume()
                     {
                        super.onResume();
                        mPager = (ViewPager) findViewById(R.id.vpchildren);
                        mPager.setOffscreenPageLimit(6);
                        childrenNumber = MainActivity.bean.size();
                        mPagerAdapter = new ChildrenPagerAdapter(getSupportFragmentManager(), MainActivity.bean);
                        mPager.setAdapter(mPagerAdapter);
                        mPager.setCurrentItem(currentChild);
                     }
                  }
                  

                  片段代码:

                  public class ReportFragment extends Fragment
                  {
                  
                     public ChildBean childBean;
                     public int position;
                     public ImageView img;
                     public ImageLoader imageLoader;
                     public DisplayImageOptions options;
                     private int pee = 0;
                     private int poop = 0;
                     private double sleep = 0.0;
                     public ViewPager mPager;
                     public boolean mostLeft = false;
                     public boolean mostRight = false;
                  
                     public ReportFragment()
                     {
                  
                     }
                  
                     @Override
                     public void onDestroyView()
                     {
                        super.onDestroyView();
                     }
                  
                     @Override
                     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
                     {
                        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.report_fragment, container, false);
                  
                        if(mostLeft)
                        {
                           rootView.findViewById(id.btnleft).setVisibility(View.GONE);
                        }
                        if(mostRight)
                        {
                           rootView.findViewById(id.btnright).setVisibility(View.GONE);
                        }
                  
                        rootView.findViewById(id.btnleft).setOnClickListener(new OnClickListener()
                        {
                  
                           @Override
                           public void onClick(View v)
                           {
                              mPager.setCurrentItem(mPager.getCurrentItem() - 1);
                  
                           }
                        });
                  
                        rootView.findViewById(id.btnright).setOnClickListener(new OnClickListener()
                        {
                  
                           @Override
                           public void onClick(View v)
                           {
                              mPager.setCurrentItem(mPager.getCurrentItem() + 1);
                  
                           }
                        });
                  
                        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
                        Date dobchild = new Date();
                  
                        ((TextView) rootView.findViewById(id.tvday)).setText(sdf.format(dobchild));
                  
                        ImageView childimg = (ImageView) rootView.findViewById(id.img_child);
                        ((TextView) rootView.findViewById(id.tvchildname)).setText(childBean.childname);
                        ((TextView) rootView.findViewById(id.tvclassname)).setText(((CustomApplication) getActivity().getApplication()).preferenceAccess.getCurrentClassName());
                  
                        Date dob = null;
                        String age = "";
                        try
                        {
                           dob = sdf.parse(childBean.childdob);
                           age = GeneralUtils.getAge(dob.getTime(), getString(string.tv_day), getString(string.tv_month), getString(string.tv_year));
                        }
                        catch(ParseException e)
                        {
                           // TODO:
                        }
                        ((CustomTextView) rootView.findViewById(id.tvchildage)).setText(age);
                  
                        DisplayImageOptions options =
                           new DisplayImageOptions.Builder().showImageForEmptyUri(drawable.noimage).showImageOnFail(drawable.noimage).showStubImage(drawable.noimage).cacheInMemory()
                              .imageScaleType(ImageScaleType.NONE).build();
                  
                        imageLoader = ImageLoader.getInstance();
                        imageLoader.displayImage(childBean.childphoto, childimg, options);
                        final TextView tvpee = (TextView) rootView.findViewById(id.tvpeetime);
                        final TextView tvpoop = (TextView) rootView.findViewById(id.tvpootimes);
                        final TextView tvsleep = (TextView) rootView.findViewById(id.tvsleeptime);
                  
                        rootView.findViewById(id.btnaddpee).setOnClickListener(new OnClickListener()
                        {
                           @Override
                           public void onClick(View v)
                           {
                              pee = pee + 1;
                              if(pee > 9)
                              {
                                 Toast.makeText(getActivity(), getString(string.tvareyousurepee), Toast.LENGTH_LONG).show();
                              }
                              tvpee.setText(String.format(getString(string.tvtimes), pee));
                           }
                        });
                  
                        rootView.findViewById(id.btnminuspee).setOnClickListener(new OnClickListener()
                        {
                           @Override
                           public void onClick(View v)
                           {
                              if(pee > 0)
                              {
                                 pee = pee - 1;
                                 tvpee.setText(String.format(getString(string.tvtimes), pee));
                              }
                           }
                        });
                  
                        rootView.findViewById(id.btnpluspoo).setOnClickListener(new OnClickListener()
                        {
                           @Override
                           public void onClick(View v)
                           {
                              poop = poop + 1;
                              if(poop > 9)
                              {
                                 Toast.makeText(getActivity(), getString(string.tvareyousurepoop), Toast.LENGTH_LONG).show();
                              }
                              tvpoop.setText(String.format(getString(string.tvtimes), poop));
                           }
                        });
                  
                        rootView.findViewById(id.btnminuspoo).setOnClickListener(new OnClickListener()
                        {
                           @Override
                           public void onClick(View v)
                           {
                              if(poop > 0)
                              {
                                 poop = poop - 1;
                                 tvpoop.setText(String.format(getString(string.tvtimes), poop));
                              }
                           }
                        });
                  
                        rootView.findViewById(id.btnaddsleep).setOnClickListener(new OnClickListener()
                        {
                           @Override
                           public void onClick(View v)
                           {
                              sleep = sleep + 0.25;
                              tvsleep.setText(String.format(getString(string.tvhours), sleep));
                           }
                        });
                  
                        rootView.findViewById(id.btnminussleep).setOnClickListener(new OnClickListener()
                        {
                           @Override
                           public void onClick(View v)
                           {
                              if(sleep > 0)
                              {
                                 sleep = sleep - 0.25;
                                 tvsleep.setText(String.format(getString(string.tvhours), sleep));
                              }
                           }
                        });
                  
                        rootView.findViewById(id.btnsave).setOnClickListener(new OnClickListener()
                        {
                           @Override
                           public void onClick(View v)
                           {
                              Toast.makeText(getActivity(), "Report Saved.", Toast.LENGTH_LONG).show();
                              getActivity().finish();
                           }
                        });
                  
                        return rootView;
                     }
                  }
                  

                  请指教...谢谢

                  推荐答案

                  ViewPager 本身有一个方法 setOffscreenPageLimit 允许您指定适配器保留的页数.所以你远处的碎片会被销毁.

                  ViewPager itself has a method setOffscreenPageLimit which allows you to specify number of pages kept by the adapter. So your fragments that are far away will be destroyed.

                  首先查看您的代码,我没有看到您在 onDestroy() 片段中执行任何内存释放措施.片段本身被销毁和 gc'ed 的事实并不意味着您分配的所有资源也被删除.

                  First of all looking at your code I don't see you doing any memory releasing measures in your fragments onDestroy(). The fact that fragment itself is destroyed and gc'ed does not mean all resources you allocated were removed too.

                  例如,我最关心的是:

                  imageLoader = ImageLoader.getInstance();
                  imageLoader.displayImage(childBean.childphoto, childimg, options);
                  

                  从我在这里看到的情况来看,似乎每次出现新片段时都会戳到 ImageLoader 的一个静态实例,但我看不到一个垂死的片段会在哪里要求 ImageLoader 卸载它的东西.我觉得这很可疑.

                  From what I see here it seems that there is a static instance of ImageLoader that gets poked every time a new fragment appears, but I can't see where a dying fragment would ask ImageLoader to unload its stuff. That looks suspicious to me.

                  如果我是你,我会在活动重新启动后花费额外的 200mb(如你所称)并通过 MAT(内存分析器工具)分析引用时转储我的应用程序的 HPROF 文件.您显然有内存泄漏问题,我非常怀疑问题在于片段本身没有被破坏.

                  If I were you I would dump an HPROF file of my application the moment it took extra 200mb (as you claim) after activity restart and analyze references via MAT (memory analyzer tool). You are clearly having memory leaks issue and I highly doubt the problem is in Fragments themselves not being destroyed.

                  如果你不知道如何分析内存堆,这里有一个很好的视频.我数不清有多少次它帮助我识别并消除了我的应用程序中的内存泄漏.

                  In case you don't know how to analyze memory heap, here is a good video. I can't count how many times it helped me identifying and getting rid of memory leaks in my apps.

                  这篇关于片段没有从内存中释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何实现 PagerAdapter 的视图回收机制? 下一篇:如何从活动(viewpager)更新片段内容?

                  相关文章

                2. <i id='fEjoE'><tr id='fEjoE'><dt id='fEjoE'><q id='fEjoE'><span id='fEjoE'><b id='fEjoE'><form id='fEjoE'><ins id='fEjoE'></ins><ul id='fEjoE'></ul><sub id='fEjoE'></sub></form><legend id='fEjoE'></legend><bdo id='fEjoE'><pre id='fEjoE'><center id='fEjoE'></center></pre></bdo></b><th id='fEjoE'></th></span></q></dt></tr></i><div id='fEjoE'><tfoot id='fEjoE'></tfoot><dl id='fEjoE'><fieldset id='fEjoE'></fieldset></dl></div>
                    • <bdo id='fEjoE'></bdo><ul id='fEjoE'></ul>

                    <legend id='fEjoE'><style id='fEjoE'><dir id='fEjoE'><q id='fEjoE'></q></dir></style></legend>
                  1. <small id='fEjoE'></small><noframes id='fEjoE'>

                  2. <tfoot id='fEjoE'></tfoot>