Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7754361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:17:15+00:00 2026-06-01T12:17:15+00:00

Memory Issues So I am writing an app that should be able to page

  • 0

Memory Issues

So I am writing an app that should be able to page through detail views that have one large 640 x 480 image on top and 3 images that are part of a gallery that is being lazy loaded. Following Google design guidelines this is what they suggest doing. I can page through maybe 12 – 13 fragments before it crashes because of being out of memory. I think that there are a couple of culprits in this problem.

1.) I am using the FragmentStatePager. Shouldn’t this be destroying the fragments that are not being viewed when memory becomes an issue? This is not happening. I thought it was automatic. What do I have to do to make this happen? Could it have something to do with how I have my Fragment implemented? I do all of my Activity config in onCreateView. For the sake of thoroughness I’ve included the source for this. Plain Vanilla here:

public static class MyAdapter extends FragmentStatePagerAdapter {


        public MyAdapter(FragmentManager fm) {
            super(fm);              
        }

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

        @Override
        public Fragment getItem(int position) {
            return InventoryDetailFragment.newInstance(position);
        }
    }

2.) I have a method that is trying to figure out the size of the image that needs to be downloaded without placing it in memory. Then compresses the image while downloading it to the required size. This is not successfully implemented. But I’m not sure what is going wrong.

private Bitmap downloadBitmap(String url, int width, int height) {
    Bitmap bitmap = null;
    int scale = 1;
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;  
        bitmap = BitmapFactory.decodeStream((InputStream)new URL (url).getContent(), null, options);

        if (options.outHeight > height || options.outWidth > width) {
            scale = (int) Math.max(((options.outHeight)/ height), ((options.outWidth)/ width));             }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        bitmap = BitmapFactory.decodeStream((InputStream)new URL (url).getContent(), null, o2); 
        cache.put(url, new SoftReference<Bitmap>(bitmap));


    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Error e){
        Log.d("TEST", "Garbage Collector called!");
        System.gc();
    }
    return bitmap;
}

I have tried everything that I know how to do but it’s beyond my meager grasp of Android/Java. Please help! Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:17:17+00:00Added an answer on June 1, 2026 at 12:17 pm

    There are a few things that you need to change:

    1. This is a horrible idea: BitmapFactory.decodeStream((InputStream)new URL (url).getContent(), null, options); You’re getting the image from the web each time this is executed (so twice in the code you posted). Instead, you need to download the image and cache it locally.

    2. Add logic to your fragments to call recycle() on the bitmaps as soon as the fragment is detached. Add logic to always reload the image (from the cache) whenever the fragment is attached.

    3. Lastly, your inSampleSize calculation is wrong. inSampleSize should be a value that’s a power of two, e.g. 1,2,4,8. You can use logarithms or simple binary logic to get the right one, this is what I use, which will always downsample using at least 2 (only call this if you know that the image is too big):

    –

    int ratio =  (int) Math.max((height/options.outHeight), ( width/options.outWidth); //notice that they're flipped     
      for (int powerOfTwo = 64; powerOfTwo >=2; powerOfTwo = powerOfTwo >> 1 ) { //find the biggest power of two that represents the ratio
        if ((ratio & powerOfTwo) > 0) {
          return powerOfTwo;
        }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a huge web app that is having issues with memory leak in
I am writing an app that may have in its memory quite a bit
I have an other active question HERE regarding some hopeless memory issues that possibly
In the iOS app I'm writing, we have an add photo button that's pretty
I am combating some memory issues in my app and am finally managing to
I am trying to load an image in memory but might have memory issues
I have an app that processes images and use jQuery to display progress to
I'm writing an app that has a foreground service, content provider, and a Activity
I have a problem with the Google App Engine JDO implementation that I cannot
I'm writing an app (in iOS 5 using ARC!) that presents several hundred objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.