0

My application uses many large images. Instead of requesting the use of native memory, I want to clear the bitmaps on back pressed. Android Manifest already states the required use of largeHeap so that's not an issue. The problem is the sheer amount of bitmaps. Can this be done using the back button?

I am getting the java out of memory error. The bitmap combined size exceeds the memory allocated even when largeHeap is enabled.

Update: Each activity has it's own bitmap. When the application reaches around 10 activities, 10 bitmaps, that's when the out of memory error is show.

Google
  • 80
  • 13
  • Did you tried bitmap.recycle();?? – WISHY Apr 01 '15 at 05:28
  • @WISHY there are so many bitmaps the device won't even load all of them, let alone recycle. – Google Apr 01 '15 at 05:38
  • Try to store the bitmap in cache and load from cache. And you can also try to reduce the size of bitmap....http://stackoverflow.com/questions/15759195/reduce-size-of-bitmap-to-some-specified-pixel-in-android – WISHY Apr 01 '15 at 05:46

2 Answers2

0

Try bitmap.recycle() for all images you used before you start new activity .

dharmendra
  • 7,835
  • 5
  • 38
  • 71
  • Will this work even if there are around 28 activities each with their own bitmap? – Google Apr 01 '15 at 05:34
  • If you single activity is able to render the image successfully then yes recycle will works for you – dharmendra Apr 01 '15 at 05:35
  • That's the problem. The device can't render more than 10 and I need around 28 for recycle to be an effective solution. Can I clear some bitmaps using the back button after say 10 bitmaps and _then_ use bitmap.recycle( ); – Google Apr 01 '15 at 05:38
  • Single Activity contains single image , and you have 10 activity which contains the same , right ? – dharmendra Apr 01 '15 at 05:39
  • No, there are 28 activities with 28 bitmaps and the device cannot render more than around 10 even with largeHeap enabled. – Google Apr 01 '15 at 05:43
  • Ok then , Do bitmap.recycle() for current activity while you about to start new activity . – dharmendra Apr 01 '15 at 05:48
  • Could this be implemented using the back button as well? – Google Apr 01 '15 at 06:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74230/discussion-between-dhams-and-goooooogle). – dharmendra Apr 01 '15 at 06:51
0

Use SoftReference to save the Bitmap. When the memory is not enough , GC will clear it or you can try to use the bitmap.recycle().

dharmendra
  • 7,835
  • 5
  • 38
  • 71
梁航斌
  • 83
  • 1
  • 7