1. Download our Official Android App: Forums for Android!
  2. We apologize for the disruption in email services! They are sending out now, but there is a backlog, so it may take a little time to catch up.

Apps Out of Memory Issue

Discussion in 'Android Development' started by pinkfloydim, Jan 13, 2013.

  1. pinkfloydim

    pinkfloydim Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Dec 28, 2012

    Dec 28, 2012
    9
    0
    5
    I am calling 'setBackgroundResource' on some png files, which are in the res/drawable-mdpi folder, but not in the other folders.

    But after a few back and forward loads the phone runs out of memory.
    Any way to clear the memory so I can load the new graphics.

    [HIGH]String map_up_theme_res = CoreFunctions.mapThemeRes("up");
    String map_down_theme_res = CoreFunctions.mapThemeRes("down");
    String service_up_theme_res = CoreFunctions.serviceThemeRes("up");
    String service_down_theme_res = CoreFunctions.serviceThemeRes("down");
    try {
    townUpMap.setBackgroundResource(CoreFunctions.getResId(
    map_up_theme_res, R.drawable.class));
    townDownMap.setBackgroundResource(CoreFunctions.getResId(
    map_down_theme_res, R.drawable.class));
    serviceUpTray.setBackgroundResource(CoreFunctions.getResId(
    service_up_theme_res, R.drawable.class));
    serviceDownTray.setBackgroundResource(CoreFunctions.getResId(
    service_down_theme_res, R.drawable.class));
    } catch (OutOfMemoryError E) {

    }[/HIGH]
     

    Advertisement

  2. jonbonazza

    jonbonazza Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
  3. pinkfloydim

    pinkfloydim Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Dec 28, 2012

    Dec 28, 2012
    9
    0
    5
    So how do I use this in respect to my code, as just passing in a resource id. How do I get the Bitmap resource in order to recycle it.
     
  4. jungajuice

    jungajuice Member
    Rank:
    None
    Points:
    16
    Posts:
    31
    Joined:
    Mar 30, 2010

    Mar 30, 2010
    31
    1
    16
    Male
    Lead Android Developer at TuneWiki
    Santa Monica, CA
    You really shouldn't, the system should take care of it. You should really only recycle bitmaps when you're iterating through a lot of them.

    Chances are you're leaking memory somewhere else that's adding up, and this is just where's its crashing. Make sure you're not holding onto static references of resource intensive objects like Activities.
     
  5. pinkfloydim

    pinkfloydim Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Dec 28, 2012

    Dec 28, 2012
    9
    0
    5
    Every Time I open an activity I am using this code:

    [HIGH]final Intent intent = new Intent(cnt, myClass);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    cnt.startActivity(intent);[/HIGH]I am assuming that this clears the activity stack, which should free up memory?
     
  6. CaptainConnect

    Rank:
    None
    Points:
    25
    Posts:
    2
    Joined:
    Jan 28, 2013

    Jan 28, 2013
    2
    0
    25
    You might also consider to optimize your images in order to make them take as little memory as possible when loaded by the app. Image dimensions matter, transparency matters. I blogged about this subject in this blog post
     

Share This Page

Loading...