1. Download our Official Android App: Forums for Android!

Fragments and memory

Discussion in 'Android Development' started by Fl_Op, Aug 24, 2017.

  1. Fl_Op

    Fl_Op Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Aug 24, 2017

    Aug 24, 2017
    2
    0
    5
    Hy,

    I am developing an application with Fragments. Here is my (simplified) code.

    Code (Java):
    1. public class MainActivity extends AppCompatActivity{
    2.     private FragmentManager fm = null;
    3.     private Fragment fragment = null;
    4.  
    5.     @Override
    6.     protected void onCreate(Bundle savedInstanceState) {
    7.         super.onCreate(savedInstanceState);
    8.         // ...
    9.  
    10.         FragmentManager fm = getSupportFragmentManager();
    11.         fragment = Fragment1.newInstance();
    12.         fm.beginTransaction().replace(R.id.content_to_replace,fragment).commit();
    13.     }
    14.  
    15.     @SuppressWarnings("StatementWithEmptyBody")
    16.     @Override
    17.     public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    18.         int id = item.getItemId();
    19.  
    20.         if (id == R.id.fragment_1) {
    21.             fragment = Fragment1.newInstance();
    22.             idFragment = 0;
    23.         } else if (id == R.id.fragment_2) {
    24.             fragment = Fragment2.newInstance();
    25.             idFragment = 1;
    26.         }
    27.  
    28.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    29.         drawer.closeDrawer(GravityCompat.START);
    30.  
    31.         fm.beginTransaction().replace(R.id.content_to_replace,fragment).commit();
    32.         return true;
    33.     }
    34. }
    I thought that in "onNavigationItemSelected", the replaced fragment will free the memory that it took. But I see that if I switch fragments many times, the free memory will always decrease.

    RAM_android.png


    So, I would like to know why, and if I can prevent this from happening

    Thanks in advance for your answers.
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,869
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,869
    11,495
    1,988
    Male
    Software developer
    South West of England
    Are you asking why the consumed memory doesn't decrease?
    The garbage collector makes its own decisions on when to free up memory. It may not behave as you expect. Each time you call Fragment.newInstance() an object is created on the heap. Periodically the GC decides which objects are unreferenced and cleans them up. Or it may decide to defer this action. With GC, you cannot rely on predictable behaviour. As the memory consumed by your program increases, the GC may decide to do more rigorous cleaning up.
     
  3. Fl_Op

    Fl_Op Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Aug 24, 2017

    Aug 24, 2017
    2
    0
    5
    Thanks, I made the test with a loop which create many Fragments, and you're right.
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,869
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,869
    11,495
    1,988
    Male
    Software developer
    South West of England
    No problem. The GC works in mysterious ways, but you can be sure it knows what it's doing :)
     
    sweetndreemy73 likes this.

Share This Page

Loading...