Note that there are some explanatory texts on larger screens.

plurals
  1. POremoving image data from memory
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>For example, imagine this very basic hypothetical app, that emulates a lot of the behavior of the native gallery app:</p> <ol> <li>An image gallery that allows the user to peruse images from a remote server.</li> <li>There might be any number of images on that server.</li> <li>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.</li> <li>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.</li> <li>Images are loaded using URL.openStream and Drawable.createFromStream or BitmapFactory.decodeStream. Streams are closed appropriately.</li> <li>Images are sized appropriately <em>on the server</em> before being fetched.</li> <li>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.</li> <li>When any image is no longer required, it's "cleared" via: <ul> <li>getBackground().setCallback(null)</li> <li>Listeners are set to null </li> <li>setImageDrawable/Bitmap(null) </li> <li>removeView </li> </ul></li> </ol> <p>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.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>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...</p> <p>TYIA.</p> <p>/this question has also been posted on the android developer google group list.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload