-2

I know this is a common question but I did not find an appropriate answer to my problem in other threads.

I use bitmaps in my application for displaying profile pictures of users.
In addition, the app is like Instagram, so there is a page that is showing pictures from a database.
The problem is when I redirect between activities, i get often - "unfortunately application has stopped".

In the log I found the "out of memory" error.
I think it is because I should clean the memory by recycling bitmaps.

I don't know why its not working.
Please help me how to use bitmaps in way that would not make errors.

Here is part of the page where I print the bitmaps. I have rows in database.
I use while and run all the rows.
I make new ImageViews, TextViews and I print them all like Instagram does.

Here you see how I print the picture every row:

ImageView v = new ImageView(this);
b2 = BitmapFactory.decodeByteArray(res4.getBlob(1), 0, res4.getBlob(1).length);
v.setImageBitmap(b2);
v.setId(count + 1);
p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
p.width = width;
p.setMargins(0, 0, 0, 20);
p.addRule(RelativeLayout.BELOW, count);
rl.addView(v, p);

where should I recycle the bitmap? Is that the solution? here is a part of the log that shows "oom error":

10-05 18:53:31.184 16093-16093/com.example.user.instamath2 W/CursorWindow﹕ Window is full: requested allocation 1423987 bytes, free space 1241748 bytes, window size 2097152 bytes
10-05 18:53:31.201 16093-16093/com.example.user.instamath2 D/dalvikvm﹕ between the previous GC alloc 4461K
10-05 18:53:31.227 16093-16093/com.example.user.instamath2 I/dalvikvm-heap﹕ Clamp target GC heap from 132.368MB to 128.000MB
10-05 18:53:31.227 16093-16093/com.example.user.instamath2 D/dalvikvm﹕ GC_FOR_ALLOC freed 1229K (1749), 3% free 126908K/130816K, paused 25ms, total 25ms
10-05 18:53:31.227 16093-16093/com.example.user.instamath2 I/dalvikvm-heap﹕ Forcing collection of SoftReferences for 3149840-byte allocation
10-05 18:53:31.227 16093-16093/com.example.user.instamath2 D/dalvikvm﹕ between the previous GC alloc 0K
10-05 18:53:31.257 16093-16093/com.example.user.instamath2 I/dalvikvm-heap﹕ Clamp target GC heap from 132.360MB to 128.000MB
10-05 18:53:31.257 16093-16093/com.example.user.instamath2 D/dalvikvm﹕ GC_BEFORE_OOM freed 9K (44), 3% free 126899K/130816K, paused 31ms, total 31ms
10-05 18:53:31.257 16093-16093/com.example.user.instamath2 E/dalvikvm-heap﹕ Out of memory on a 3149840-byte allocation.
10-05 18:53:31.258 16093-16093/com.example.user.instamath2 I/dalvikvm﹕ "main" prio=5 tid=1 RUNNABLE
10-05 18:53:31.258 16093-16093/com.example.user.instamath2 I/dalvikvm﹕ | group="main" sCount=0 dsCount=0 obj=0x41d00de0 self=0x41cee638
10-05 18:53:31.258 16093-16093/com.example.user.instamath2 I/dalvikvm﹕ | sysTid=16093 nice=0 sched=0/0 cgrp=default handle=1074549128
10-05 18:53:31.258 16093-16093/com.example.user.instamath2 I/dalvikvm﹕ | state=R schedstat=( 2032092377 14250700 588 ) utm=164 stm=39 core=2
10-05 18:53:31.258 16093-16093/com.example.user.instamath2 I/dalvikvm﹕ at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
10-05 18:53:31.258 16093-16093/com.example.user.instamath2 I/dalvikvm﹕ at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:529)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Yoni4949
  • 15
  • 3
  • I belive thats because your res4 is way too big to allocate, check this page http://developer.android.com/training/displaying-bitmaps/load-bitmap.html might help ;) – Luis Pereira Oct 26 '15 at 13:51
  • http://developer.android.com/training/displaying-bitmaps/load-bitmap.html – thumbmunkeys Oct 26 '15 at 13:52
  • 1
    did you try looking for an existing answer before posting this question? there are many pre-existing questions which address this issue, and many sites (including official android sites) which address this too. – Gil Moshayof Oct 26 '15 at 13:55
  • 1
    Possible duplicate of [Strange out of memory issue while loading an image to a Bitmap object](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – Gil Moshayof Oct 26 '15 at 13:56

1 Answers1

1

So according to the docs, recycle() does not need to be called normally because if your reference goes out of scope the JVM will recycle it anyway.

Free the native object associated with this bitmap, and clear the reference to the pixel data. This will not free the pixel data synchronously; it simply allows it to be garbage collected if there are no other references. The bitmap is marked as "dead", meaning it will throw an exception if getPixels() or setPixels() is called, and will draw nothing. This operation cannot be reversed, so it should only be called if you are sure there are no further uses for the bitmap. This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this bitmap.

But in the code snippet you shared with us it's unclear if b2 ever goes out of scope, so the JVM probably never garbage collects it. Where in the activity lifecycle are you allocating the bitmap? What do you do with b2?

If you would like to explicitly recycle the bitmap, I would do it as part of the stop() or destroy() lifecycle event. Just make sure that you're allocate in the corresponding lifecycle event (i.e. start() or create() respectively).

Samuel
  • 16,923
  • 6
  • 62
  • 75
  • It probably just stays in-memory and leaks slowly, a WeakReference to it should help further pinpoint the problem – Shark Oct 26 '15 at 13:55
  • 1
    or, the loaded bitmap is too large, or there are already many bitmaps in memory. this problem has been addressed many times here in SO, and the general approach is to know how to use bitmaps in android. this includes sampling the bitmap's size with options, then using sample size when loading. – Gil Moshayof Oct 26 '15 at 13:59