Bitmap is recycled but I didn't do it?

by William » Sat, 07 Mar 2009 10:53:20 GMT

Sponsored Links
 I am drawing bitmaps left and right and I hit this issue where I
create a bitmap in one section of my code and when I later try to draw
on it using canvas, i get bitmap recycled.  but I did not null if out,
or call its recycle method.


I have a main Class that extends View that when first loads, initiates
the main screen which loads/draws on bitmaps and no problem.  I then
added a key event that causes this screen to go away and load the next
screen.  The screen logic is encapsulated in a class that I already
instantiated in the Main Class constructor and in my second screen its
constructor is this code:

staticBg = new BitmapDrawable(Bitmap.createBitmap(320, 240,
staticGr = new Canvas( staticBg.getBitmap() );

at a later time, from my first screen when I push a button, i call my
init() function that trys load screen two which does a draw to this
bitmap and I get the following error:

03-06 08:52:56.301: ERROR/AndroidRuntime(846):
03-06 08:52:56.301: ERROR/AndroidRuntime(846):     at
03-06 08:52:56.301: ERROR/AndroidRuntime(846):     at

It is saying my bitmap is recycled BUT from what I understand that
does not happened unless you explicitly call it or the garbage
collector does when there are no more references to it BUT i obviously
still have a reference to it.  What am I doing wrong?


Bitmap is recycled but I didn't do it?

by Marco Nelissen » Sun, 08 Mar 2009 00:37:11 GMT


Are you sure it's your bitmaps it's complaining about, and not some
other bitmap?
Are you using the drawing cache in any way?


Sponsored Links

Bitmap is recycled but I didn't do it?

by Stoyan Damov » Sun, 08 Mar 2009 00:49:19 GMT

 BTW, the Android engineer who wrote the throwIfRecycled method should
be lynched. I understand that it saves an "if", as in:

if (bitmap.isRecycled()) throw ...

but s/he didn't even implement it defensively enough and the method
crashes if passed a null pointer.

Now, you either make the method super safe so it checks for both a
null pointer and whether the bitmap is recycled, or you let the user's
code crash miserably (preferred).
I will *very* much appreciate a Canvas::drawBitmapFast() or
drawBitmapUnsafe() method which doesn't check anything, because the
current implementation of drawBitmap(Bitmap, Paint) spends as much as
9%!!!!! in throwIfRecycled, which IMVHO is unacceptable.



Bitmap is recycled but I didn't do it?

by Jean-Baptiste Queru » Sun, 08 Mar 2009 01:27:26 GMT

 e'll gladly accept high-quality contributions to optimize this.


On Sat, Mar 7, 2009 at 8:49 AM, Stoyan Damov <> wrote:

Jean-Baptiste M. "JBQ" Queru
Android Engineer, Google.

Questions sent directly to me that have no reason for being private
will likely get ignored or forwarded to a public forum with no further


Bitmap is recycled but I didn't do it?

by clark » Sun, 08 Mar 2009 01:49:32 GMT

 gt; BTW, the Android engineer who wrote the throwIfRecycled method should

Lynched??? Really. Wow, that would be a little excessive, don't you
think? I understand your frustration with the implementation of the
throwIfRecylced method, but to say the engineer should be lynched is
just ridiculous. Perhaps stopping to read your post prior to hitting
"send" would not be a bad idea. I think there are more tactful ways
of addressing issues than to attack the google developers. An OS and
the accompanying Framework are no small project, and I think most of
us realized there would be issues along the way, especially with the
hardware being released with a 1.0 SDK.

Long story short, be careful not to bite off the hand that feeds you.
Okay google isn't feeding you, but they have produced something that
has great potential, and as a community we need to come together and
help out in any which way we can.

And as JBQ said, if you have a better implementation send it on up to
the google guys to review and possibly implement.


On Mar 7, 8:49am, Stoyan Damov <> wrote:

Bitmap is recycled but I didn't do it?

by William » Sun, 08 Mar 2009 02:14:50 GMT

 well it happens either way: if I call or.  I used a breakpoint right
at the line and it bombs when the line gets executed so I know its
that bitmap.

    private BitmapDrawable staticBg;
    private Canvas staticGr;

both throw the error

 g.drawBitmap(staticBg.getBitmap(),0, 0, null);

anymore ideas?

Its really annoying. what makes it be recycled.  even if it was
another bitmap, i do not call recycle on any of my bitmaps so that
means the GC has to get to them which makes no sense since I still
have references to the object it should not get them.


Bitmap is recycled but I didn't do it?

by Romain Guy » Sun, 08 Mar 2009 03:38:22 GMT

 gt; but s/he didn't even implement it defensively enough and the method

It would crash without the test to see if it's recycled. Passing a
null will Bitmap will crash, period. The reason why there's a check
for recycled and not for null is that a crash due to a recycled Bitmap
would be much more difficult to debug/understand without the "recycled

Romain Guy
Android framework engineer

Note: please don't send private questions to me, as I don't have time
to provide private support. All such questions should be posted on
public forums, where I and others can see and answer them


Bitmap is recycled but I didn't do it?

by Stoyan Damov » Sun, 08 Mar 2009 06:52:43 GMT

 n Sat, Mar 7, 2009 at 7:49 PM, clark <> wrote:

You can't possibly think that I *meant* that the guy should be
lynched, right? It's an expression of frustration. If an engineer were
lynched for 1 bug there wouldn't be any of us :)

It's not that I don't understand what an enormous effort an OS and a
framework is, my frustration comes from the fact that Google have
published performance guidelines for app developers but in this case
(which btw is quite a hot spot if you're developing a game and most of
the app's time is spent in drawing) they've chosen a shortcut and
instead of doing a direct check are calling a method
(throwIfRecycled), which in turns calls another method (isRecycled)
and could easily spare both calls (or at least one):

1. throwIfRecycled is absolutely not needed - they can call isRecycled
directly in an if statement in drawBitmap

2. isRecycled is not needed as well. To answer Romain's post -
drawBitmap_native or whatever the JNI method is called could check
whether the bitmap is recycled or not, and surely faster than Java
would (i.e. checking a bit or comparing against an invalid handle
value or whatever the native implementation uses).

So I can't criticize if they've done something wrong, huh? I know I'm
probably the grumpiest ranting dev on the list but I just can help it
when I see something as obviously wrong as that.

Well, it's not that I'm suggesting they implement drawBitmap in some
fundamentally different and optimized way, is it? It's just replacing
2 method calls with 1 "if" and a method call.


Bitmap is recycled but I didn't do it?

by Marco Nelissen » Sun, 08 Mar 2009 09:45:14 GMT


That sounds like it shouldn't take much time to implement and test at
all, so we'll be expecting your patch by Monday :)


Other Threads

1. what are 9-patch bad patches?

I've created some button .pngs in CS4 Fireworks and have loaded them
into draw9patch. The shadow effects I'm using to create a 3D-ish look-
and-feel cause the draw9patch tool to mark all the stretchable areas
as 'bad'. What artifacts are created when 'bad patches' are stretched?
How do I steer clear of graphics that won't stretch properly as 9-
patch pngs?


2. Is it possible to use DDMS to send mock locations to a physical device?

I am debugging an application on a Droid 2 physical device. When I
invoke DDMS I can see the physical device. I can see Logcat entries,
threads etc etc., so I know I am communicating with the physical
device. When I look at the emulator control tab to send a location
everything is grayed out (both telephony and location controls)

In the manifest I have the following permissions:

android:name="android.permission.ACCESS_COARSE_LOCATION" />
android:name="android.permission.ACCESS_FINE_LOCATION" />
android:name="android.permission.ACCESS_MOCK_LOCATION" />

On my Droid 2 physical device under application --- > development   I
have checked:

USB debugging:
Allow mock locations:

 If I use an AVD I have no problems with location control. In field
testing the location listener is processing information properly but I
would like to test more thoroughly with DDMS.

Is there any way to send mock locations to using DDMS?
Thanks in advance for any help

3. How do I initiate/terminate xml-defined SurfaceView upon a button push?

4. MediaPlayer to play Multicast (igmp) medias?

5. Testing in app billing, all purchases end up cancelled

6. Flickr api connection with android

7. HelloAndroid] Unknown error: Unable to build: the file dx.jar was not loaded from the SDK folder!