片段没有从内存中释放 [英] Fragments are not being released from memory

查看:13
本文介绍了片段没有从内存中释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 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.

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

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