ViewPager +导航抽屉+ GridView上的OutOfMemoryError

时间:2013-11-11 06:42:39

标签: java android memory-management out-of-memory universal-image-loader

我有一个主fragment创建了7个页面,each pages包含navigation drawer,每个导航抽屉里面都包含一个gridview,显示从处理的SD卡加载的大图像通过通用图像加载器。我正在使用FragmentStatePagerAdapter来处理很多页面。

gPager.get().setOffscreenPageLimit(gABar.get().getTabCount());

当我查看MAT(内存分析工具)和Heap内的DDMS标签时,在导航期间分配的内存和heap大小一直在上升(这是没有方向的)更改)。我尝试在导航时放置System.gc(),这会导致延迟,所以我将其删除。我已经通过处理(弱和软)引用查看持有者模式泄漏context 来优化我的代码。即使我点击cause system gc仍然没有降低分配的内存。

以下代码是否可能导致 OOM ?我的意思是我在我的四个页面中实例化了相同的片段。 MainGroupFragment - 处理抽屉布局并调用另一个处理'gridview display'的片段。是否有任何有效的实例化方法,以便它不会导致OOM或保存内存?

@Override public Parcelable saveState() { return null; }

@Override
public void destroyItem(View collection, int position, Object o) {
    Log.i("destroyItem", "MyFragmentPagerAdapter destroyItem");
    View view = (View)o;
    ((ViewPager) collection).removeView(view);
    view = null;

}

 @Override
    public Fragment getItem(int arg0) {
        SavePref saveState = new SavePref(mContext.get());

        switch(arg0){
            /** Android tab is selected */
            case 0:
                //Bundle b = new Bundle();
                MainGroupFragment ItemID1 = new MainGroupFragment();
                ItemID1.variableAssigned("ItemID", mContext.get());
                ItemID1.saveSelecteditem("ItemID", 
                        ItemID1.selectedMainGroup.toString());
                //banquet.setRetainInstance(true);
                Log.i("#Inside MyFragmentPagerAdapter", "ItemID Value: " + 
                        ItemID1.selectedMainGroup.toString());
                //b.putCharSequence("mainGroup", "ItemID");
                //banquet.setArguments(b);
                return ItemID1;

            case 1:

                MainGroupFragment ItemID2 = new MainGroupFragment();
                ItemID2.variableAssigned("ItemID", mContext.get());
                saveState.saveSelecteditem("ItemID", 
                        ItemID2.selectedMainGroup.toString());
                //beverages.setRetainInstance(true);
                Log.i("#Inside MyFragmentPagerAdapter", "ItemID Value: " + 
                        ItemID2.selectedMainGroup.toString());

                return ItemID2;

            case 2:

                MainGroupFragment ItemID3 = new MainGroupFragment();
                ItemID3.variableAssigned("ItemID",  mContext.get());
                saveState.saveSelecteditem("ItemID", 
                        ItemID3.selectedMainGroup.toString());
                //food.setRetainInstance(true);
                Log.i("#Inside MyFragmentPagerAdapter", "ItemID Value: " + 
                        ItemID3.selectedMainGroup.toString());

                return ItemID3;

            case 3:

                MainGroupFragment ItemID4 = new MainGroupFragment();
                ItemID4.variableAssigned("OTHERS", mContext.get());
                saveState.saveSelecteditem("OTHERS", 
                        ItemID4.selectedMainGroup.toString());
                //others.setRetainInstance(true);
                Log.i("#Inside MyFragmentPagerAdapter", "OTHERS Value: " + 
                        ItemID4.selectedMainGroup.toString());

                return ItemID4;

            case 4: 
                HistoryFragment ItemID5 = new HistoryFragment();
                return ItemID5;

            case 5:
                OrdersFragment ItemID6 = new OrdersFragment();
                return ItemID6;

            case 6:
                FavoritesFragment ItemID7 = new FavoritesFragment();
                return ItemID7;


            default:
                //Bundle b2 = new Bundle();
                MainGroupFragment ItemID8 = new MainGroupFragment();
                ItemID8.variableAssigned("ItemID", mContext.get());
                saveState.saveSelecteditem("ItemID", 
                        ItemID8.selectedMainGroup.toString());

                return ItemID8;

        }

    }

根据MAT只有怀疑1;

One instance of "android.support.v4.app.FragmentManagerImpl" loaded by 

"dalvik.system.PathClassLoader @ 0x4123f038" occupies 32,500,328 (70.23%) bytes. The memory is 

accumulated in one instance of "android.support.v4.app.FragmentManagerImpl" loaded by 

"dalvik.system.PathClassLoader @ 0x4123f038".

Keywords
android.support.v4.app.FragmentManagerImpl
dalvik.system.PathClassLoader @ 0x4123f038

这是什么意思:

The FragmentManager seems to keep every Fragment forever. Make sure you destroy Fragments when you're done with them, simply detaching doesn't remove them from the manager.

更新

这有帮助吗?

  @Override
    public int getItemPosition(Object object){
        return PagerAdapter.POSITION_NONE;
    }

0 个答案:

没有答案