Causing Outofmemoryerror in Frame by Frame Animation in Android

Causing OutOfMemoryError in Frame by Frame Animation in Android

I assume that your animation frame images are compressed (PNG or JPG). The compressed size is not useful for calculating how much memory is needed to display them. For that, you need to think about the uncompressed size. This will be the number of pixels (320x480) multiplied by the number of bytes per pixel, which is typically 4 (32 bits). For your images, then, each one will be 614,400 bytes. For the 26-frame animation example you provided, that will require a total of 15,974,400 bytes to hold the raw bitmap data for all the frames, not counting the object overhead.

Looking at the source code for AnimationDrawable, it appears to load all of the frames into memory at once, which it would basically have to do for good performance.

Whether you can allocate this much memory or not is very system dependent. I would at least recommend trying this on a real device instead of the emulator. You can also try tweaking the emulator's available RAM size, but this is just guessing.

There are ways to use BitmapFactory.inPreferredConfig to load bitmaps in a more memory-efficient format like RGB 565 (rather than ARGB 8888). This would save some space, but it still might not be enough.

If you can't allocate that much memory at once, you have to consider other options. Most high performance graphics applications (e.g. games) draw their graphics from combinations of smaller graphics (sprites) or 2D or 3D primitives (rectangles, triangles). Drawing a full-screen bitmap for every frame is effectively the same as rendering video; not necessarily the most efficient.

Does the entire content of your animation change with each frame? Another optimization could be to animate only the portion that actually changes, and chop up your bitmaps to account for that.

To summarize, you need to find a way to draw your animation using less memory. There are many options, but it depends a lot on how your animation needs to look.

Animation Drawable causing OutOfMemoryError on second run in Android

You need to free memory used by bitmaps, when you're exiting this screen. Unfortunately, for earlier versions of Android (afaik this was "fixed" since 3.0, but I can be wrong) memory for bitmaps is allocated through native code and, what is sad, memory should be freed explicitly.

So, I suppose you to try this approach:

  1. Move code for AnimationDrawable to onResume() method.
  2. Add following code for releasing memory to onPause().

    ad.stop();
    for (int i = 0; i < ad.getNumberOfFrames(); ++i){
    Drawable frame = ad.getFrame(i);
    if (frame instanceof BitmapDrawable) {
    ((BitmapDrawable)frame).getBitmap().recycle();
    }
    frame.setCallback(null);
    }
    ad.setCallback(null);

I hope it'll help you.

OutOfMemoryError when trying to load multiple frame-by-frame animations one after the other in Android

I finally got it to work by adding mImageView.getDrawable().setVisible(false, true) before starting a new animation.

Memory Error during frame animation in Android application

The images you are using might be too large for an animation. How large are they? In an animation, Android loads all of the images into memory and uncompresses them meaning that every pixel will take 4 bytes. So 50k would mean that your image is 111px x 111px. It seems from the error each frame is about 480 x 640, which is really large. Try using smaller images.



Related Topics



Leave a reply



Submit