0

I think I am getting memory leaks when I use fragments for my android app because when I add more fragments the app crashes and each new fragment I open the memory peaks. How can I resolve this? I am fairly new to fragments and I have used many resources to use the appropriately... or so I thought. I am getting memory leaks as my heap continues to grow as I change from fragment to fragment.

This is my MainActivity.java

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {
    ViewPager viewpager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewpager = (ViewPager) findViewById(R.id.pager);
    PagerAdapter padapter = new PagerAdapter(getSupportFragmentManager());
    viewpager.setAdapter(padapter);
}
}

This is my PagerAdapter.java

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class PagerAdapter extends FragmentPagerAdapter {

    public PagerAdapter(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
}

@Override
public Fragment getItem(int arg0) {
    // TODO Auto-generated method stub
    switch (arg0) {
        case 0:

            return new FragmentOne();
        case 1:
            return new FragmentTwo();
        case 2:
            return new FragmentThree();
        case 3:
            return new FragmentFour();
        case 4:
            return new FragmentFive();


        default:
            break;
    }
    return null;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 5;
}


}

Given the code above, when I switch to Fragment 4 or 5 I retain the previous memory that was allocated for the previous fragments and so the app memory levels peak uncontrollably and the app begins to lag. Fragment 5 doesn't even load so I don't know whether that is because of there not being enough memory or due to the code being wrong. Please help as I'm lost.

Jonathan Chappell
  • 197
  • 2
  • 2
  • 10
  • To retain the memory form the previous fragments you need to be making some sort of allocation that is keeping them from being garbage collected. Without their code it's impossible to know why their not being cleared. – Pedro Oliveira Mar 23 '15 at 10:15
  • There are no memory leaks in this code. You're most likely registering the fragments in some eventBus or in some way keeping a reference to the fragment, even after the view pager destroys them. –  Mar 23 '15 at 10:16
  • Also, you're creating new fragments whenever you need to show them. Why don't just check if they exist in your FragmentManager, and if so, return them? – Pedro Oliveira Mar 23 '15 at 10:17
  • Did you try FragmentStatePagerAdapter?? – Sjd Mar 23 '15 at 10:22
  • @JonathanChappell: so, any progress with this ? – Yash Sampat Apr 25 '15 at 11:21

2 Answers2

0

do this in adapter..

    @Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Fragment fragment = null;
switch (arg0) {
    case 0:
        fragment = new FragmentOne();
    case 1:
        fragment = new FragmentTwo();
    case 2:
        fragment = new FragmentThree();
    case 3:
        fragment = new FragmentFour();
    case 4:
        fragment = new FragmentFive();
    default:
        break;
    }
return fragment;
}
Sjd
  • 1,261
  • 1
  • 12
  • 11
0

Just declaring a local fragment variable as null as in solution provided by Sjd will not help in memory optimization.

If you have many fragments that will use up lot of memory, you should use FragmentStatePagerAdapter, which means a fragment will be destroyed once you move to another fragment. If you don't have that many fragments, you could simply use FragmentPagerAdapter, which keeps your fragments in memory.

So your PagerAdapter.java implementation will have to be modified as below:

public static class MyAdapter extends FragmentStatePagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
            return 5;
    }

    @Override
    public Fragment getItem(int position) {
        switch (arg0) {
                         case 0:
                             return new FragmentOne();
                         case 1:
                             return new FragmentTwo();
                         case 2:
                             return new FragmentThree();
                         case 3:
                             return new FragmentFour();
                         case 4:
                             return new FragmentFive();
                         default:
                         break;
          }
     }
}
Ian Pinto
  • 2,199
  • 1
  • 21
  • 24