Android bitmap imageview memory leak

前端 未结 1 1131
渐次进展
渐次进展 2021-01-18 11:26

I put 4x4 imageView to an activity(BoardActivity), and user can change the images by clicking them. With HTC Desire (Android 2.2.2), I got OOM(Out Of Memory) in about 30 min

相关标签:
1条回答
  • 2021-01-18 11:52

    To reduce memory, you can try out these things :

    • After converting drawables into bitmaps, you can set the imageResourceMap to null. This will unload the 3 drawables.
    • Avoid storing a reference to the imageViews. You might be storing imageViews even after they are removed from the UI
    • Recycle the bitmaps more often. Instead of just onDestroy, as soon as you know that one bitmap is not used, you can recycle it.

    Edit : based on the conversation in the comments : The bitmap returned by BitmapFactory.decodeStream(fis, null, o) is not assigned to any variable and hence is not recycled. Android 2.2 and 2.3 will have leaks in this line.

    0 讨论(0)
提交回复
热议问题