1

我有一个 main fragment,它创建 7 个页面,each pages包含一个navigation drawer,每个导航抽屉内都包含一个gridview显示从Universal-image-loader处理的 sd 卡加载的大图像。我FragmentStatePagerAdapter用来处理很多页面。

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

当我查看 MAT(内存分析工具)和分配内存中的Heap选项卡时,在我的导航过程中DDMS,它的大小和大小都在不断上升heap(这没有方向改变)。我尝试System.gc()在导航时放置,它会导致延迟,所以我将其删除。我已经通过处理(弱和软)引用View Holder 模式泄漏context来优化我的代码。即使我点击cause system gc了,分配的内存也没有降低。

下面的代码有可能导致OOM吗?我的意思是我在我的四个页面中实例化了相同的片段。MainGroupFragment- 处理抽屉布局并调用另一个处理“gridview 显示”的片段。是否有任何有效的实例化方法不会导致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;
    }
4

0 回答 0