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

  • SEARCH
  • Home
  • 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 7893093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:02:14+00:00 2026-06-03T07:02:14+00:00

I have to believe there’s a way to clear image data from memory once

  • 0

I have to believe there’s a way to clear image data from memory once it’s no longer required, but despite exhaustive searching I can’t find a solution. Both stack and the google android dev list are full of questions regarding OOM errors, specifically “bitmap size exceeds VM budget”, but I still don’t see a clear answer.

I understand there are hard memory limits on devices, and I understand it’s not realistic to load up and display or cache large amounts of image data, but there should be away to discard data that’s no longer required.

For example, imagine this very basic hypothetical app, that emulates a lot of the behavior of the native gallery app:

  1. An image gallery that allows the user to peruse images from a remote server.
  2. There might be any number of images on that server.
  3. The app displays 1 image at a time, and allows a user to go back or forward 1 image at a time through button presses or swiping.
  4. There’d be a maximum of 3 images rendered at any one time (so the user can see the one immediately to the left or right of the current image when swiping). All other image data should be discarded.
  5. Images are loaded using URL.openStream and Drawable.createFromStream or BitmapFactory.decodeStream. Streams are closed appropriately.
  6. Images are sized appropriately on the server before being fetched.
  7. Loading happens in AsyncTasks. Tasks that are no longer needed (due to moving away from an image with an incomplete task) are cancelled. Any references in the AyncTask are WeaklyReferenced.
  8. When any image is no longer required, it’s “cleared” via:
    • getBackground().setCallback(null)
    • Listeners are set to null
    • setImageDrawable/Bitmap(null)
    • removeView

This simple construct, that takes into account all the suggest practices I’m aware of, will inevitably crash with an OOM error at some point. Using BitmapFactory.Options inSampleSize and inPreferredConfig will delay the inevitable, but not forever, and at the cost of image quality. In this example, I’ve used remote images, but the issue exists with images stored in /assets/ or in internal memory, etc.

My feeling is that if we can display X amount of image data at one point, and we take all steps to remove that image data from memory, we should be able to display that same amount of data later, without having to compensate for what has happened before.

With the sheer quantity of questions about this very issue, I’d hope to have a standard solution documented, but if there is one, I can’t find it. I’ve seen answers posted by Romain Guy, who otherwise seems very generous with his knowledge and active in the community, that say something like “Simple. Don’t use so much memory”. OK. Tell me how.

I should also mention that System.gc does nothing to help this. I’m also aware of bitmap.recycle, but unless I’m mistaken this can’t be used in this fashion.

Am I missing something fundamental? Is there a way to discard image data once it’s no longer being used? What is missing from the above to create a simple photo gallery? Assuming the built-in gallery app uses the framework and not the NDK, I imagine there has to be a way…

TYIA.

/this question has also been posted on the android developer google group list.

  • 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-03T07:02:15+00:00Added an answer on June 3, 2026 at 7:02 am

    Through my work with Prime I found a few tips, one of which you have not mentioned. When you decode your Bitmaps make sure to use the inPurgeable and inInputShareable flags in your BitmapFactory.Options. That will help a little bit but I would recommend you look at my implementation of image loading in Prime. I use it in all of my products without any memory issues. I have found that 95% of memory problems are from the incorrect usage of the Bitmap class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have ued cglib in th epast, but frankly I have to believe there
I believe there is a way to do this, but I'm not familiar with
I have come across code from someone who appears to believe there is a
I believe there is something weird when I have rewritten my urls. My website
I believe there are different flavors of this questions already asked, but I wanted
There gotta be an easy way to do this, I can't believe there's none.
I have previously asked two questions Question 1 and Question 2 but there is
Understandable this question is a little subjective, but I believe there is a concrete
I believe I have disabled the view state on all controls as well as
I believe I have a potential threading issue. I have a user control that

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.