Causing OutOfMemoryError in Frame by Frame Animation in Android Causing OutOfMemoryError in Frame by Frame Animation in Android android android

Causing OutOfMemoryError in Frame by Frame Animation in Android


I had the same problem. Android loads all the drawables at once, so animation with many frames causes this error.

I ended up creating my own simple sequence animation:

public class AnimationsContainer {    public int FPS = 30;  // animation FPS    // single instance procedures    private static AnimationsContainer mInstance;    private AnimationsContainer() {    };    public static AnimationsContainer getInstance() {        if (mInstance == null)            mInstance = new AnimationsContainer();        return mInstance;    }    // animation progress dialog frames    private int[] mProgressAnimFrames = { R.drawable.logo_30001, R.drawable.logo_30002, R.drawable.logo_30003 };    // animation splash screen frames    private int[] mSplashAnimFrames = { R.drawable.logo_ding200480001, R.drawable.logo_ding200480002 };    /**     * @param imageView      * @return progress dialog animation     */    public FramesSequenceAnimation createProgressDialogAnim(ImageView imageView) {        return new FramesSequenceAnimation(imageView, mProgressAnimFrames);    }    /**     * @param imageView     * @return splash screen animation     */    public FramesSequenceAnimation createSplashAnim(ImageView imageView) {        return new FramesSequenceAnimation(imageView, mSplashAnimFrames);    }    /**     * AnimationPlayer. Plays animation frames sequence in loop     */public class FramesSequenceAnimation {    private int[] mFrames; // animation frames    private int mIndex; // current frame    private boolean mShouldRun; // true if the animation should continue running. Used to stop the animation    private boolean mIsRunning; // true if the animation currently running. prevents starting the animation twice    private SoftReference<ImageView> mSoftReferenceImageView; // Used to prevent holding ImageView when it should be dead.    private Handler mHandler;    private int mDelayMillis;    private OnAnimationStoppedListener mOnAnimationStoppedListener;    private Bitmap mBitmap = null;    private BitmapFactory.Options mBitmapOptions;    public FramesSequenceAnimation(ImageView imageView, int[] frames, int fps) {        mHandler = new Handler();        mFrames = frames;        mIndex = -1;        mSoftReferenceImageView = new SoftReference<ImageView>(imageView);        mShouldRun = false;        mIsRunning = false;        mDelayMillis = 1000 / fps;        imageView.setImageResource(mFrames[0]);        // use in place bitmap to save GC work (when animation images are the same size & type)        if (Build.VERSION.SDK_INT >= 11) {            Bitmap bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();            int width = bmp.getWidth();            int height = bmp.getHeight();            Bitmap.Config config = bmp.getConfig();            mBitmap = Bitmap.createBitmap(width, height, config);            mBitmapOptions = new BitmapFactory.Options();            // setup bitmap reuse options.             mBitmapOptions.inBitmap = mBitmap;            mBitmapOptions.inMutable = true;            mBitmapOptions.inSampleSize = 1;        }    }    private int getNext() {        mIndex++;        if (mIndex >= mFrames.length)            mIndex = 0;        return mFrames[mIndex];    }    /**     * Starts the animation     */    public synchronized void start() {        mShouldRun = true;        if (mIsRunning)            return;        Runnable runnable = new Runnable() {            @Override            public void run() {                ImageView imageView = mSoftReferenceImageView.get();                if (!mShouldRun || imageView == null) {                    mIsRunning = false;                    if (mOnAnimationStoppedListener != null) {                        mOnAnimationStoppedListener.AnimationStopped();                    }                    return;                }                mIsRunning = true;                mHandler.postDelayed(this, mDelayMillis);                if (imageView.isShown()) {                    int imageRes = getNext();                    if (mBitmap != null) { // so Build.VERSION.SDK_INT >= 11                        Bitmap bitmap = null;                        try {                            bitmap = BitmapFactory.decodeResource(imageView.getResources(), imageRes, mBitmapOptions);                        } catch (Exception e) {                            e.printStackTrace();                        }                        if (bitmap != null) {                            imageView.setImageBitmap(bitmap);                        } else {                            imageView.setImageResource(imageRes);                            mBitmap.recycle();                            mBitmap = null;                        }                    } else {                        imageView.setImageResource(imageRes);                    }                }            }        };        mHandler.post(runnable);    }        /**         * Stops the animation         */        public synchronized void stop() {            mShouldRun = false;        }    }}

Usage:

FramesSequenceAnimation anim = AnimationsContainer.getInstance().createSplashAnim(mSplashImageView);anim.start();
  • don't forget to stop it...


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.


I spent a lot of time on this and have two different solutions, both good..

First, the problem:1) Android loads all of the images into RAM, in uncompressed Bitmap format.2) Android uses resource scaling, so on a phone with an xxxhdpi display (such as LG G3), each frame takes up a TON of space, so you quickly run out of RAM.

Solution #1

1) Bypasses Android's resource scaling. 2) Stores the bytearrays of all files in memory (these are small, especially for JPEGs). 3) Generates Bitmaps frame-by-frame, so it is almost impossible to run out of RAM.

Disadvantages: It spams your logs as Android is allocating memory for new Bitmaps and recycling old ones. It also performs lousy on older devices (Galaxy S1), but performs nicely on current budget phones (read: $10 Alcatel C1 I picked up at BestBuy). Second solution below performs better on older devices, but could still run out of RAM in some circumstances.

public class MyAnimationDrawable {public static class MyFrame {    byte[] bytes;    int duration;    Drawable drawable;    boolean isReady = false;}public interface OnDrawableLoadedListener {    public void onDrawableLoaded(List<MyFrame> myFrames);}public static void loadRaw(final int resourceId, final Context context, final OnDrawableLoadedListener onDrawableLoadedListener) {    loadFromXml(resourceId, context, onDrawableLoadedListener);}private static void loadFromXml(final int resourceId, final Context context, final OnDrawableLoadedListener onDrawableLoadedListener) {    new Thread(new Runnable() {        @Override        public void run() {            final ArrayList<MyFrame> myFrames = new ArrayList<>();            XmlResourceParser parser = context.getResources().getXml(resourceId);            try {                int eventType = parser.getEventType();                while (eventType != XmlPullParser.END_DOCUMENT) {                    if (eventType == XmlPullParser.START_DOCUMENT) {                    } else if (eventType == XmlPullParser.START_TAG) {                        if (parser.getName().equals("item")) {                            byte[] bytes = null;                            int duration = 1000;                            for (int i=0; i<parser.getAttributeCount(); i++) {                                if (parser.getAttributeName(i).equals("drawable")) {                                    int resId = Integer.parseInt(parser.getAttributeValue(i).substring(1));                                    bytes = IOUtils.toByteArray(context.getResources().openRawResource(resId));                                }                                else if (parser.getAttributeName(i).equals("duration")) {                                    duration = parser.getAttributeIntValue(i, 1000);                                }                            }                            MyFrame myFrame = new MyFrame();                            myFrame.bytes = bytes;                            myFrame.duration = duration;                            myFrames.add(myFrame);                        }                    } else if (eventType == XmlPullParser.END_TAG) {                    } else if (eventType == XmlPullParser.TEXT) {                    }                    eventType = parser.next();                }            }            catch (IOException | XmlPullParserException e) {                e.printStackTrace();            }            // Run on UI Thread            new Handler(context.getMainLooper()).post(new Runnable() {                @Override                public void run() {                    if (onDrawableLoadedListener != null) {                        onDrawableLoadedListener.onDrawableLoaded(myFrames);                    }                }            });        }    }).run();}public static void animateRawManually(int resourceId, final ImageView imageView, final Runnable onStart, final Runnable onComplete) {    loadRaw(resourceId, imageView.getContext(), new OnDrawableLoadedListener() {        @Override        public void onDrawableLoaded(List<MyFrame> myFrames) {            if (onStart != null) {                onStart.run();            }            animateRawManually(myFrames, imageView, onComplete);        }    });}public static void animateRawManually(List<MyFrame> myFrames, ImageView imageView, Runnable onComplete) {    animateRawManually(myFrames, imageView, onComplete, 0);}private static void animateRawManually(final List<MyFrame> myFrames, final ImageView imageView, final Runnable onComplete, final int frameNumber) {    final MyFrame thisFrame = myFrames.get(frameNumber);    if (frameNumber == 0) {        thisFrame.drawable = new BitmapDrawable(imageView.getContext().getResources(), BitmapFactory.decodeByteArray(thisFrame.bytes, 0, thisFrame.bytes.length));    }    else {        MyFrame previousFrame = myFrames.get(frameNumber - 1);        ((BitmapDrawable) previousFrame.drawable).getBitmap().recycle();        previousFrame.drawable = null;        previousFrame.isReady = false;    }    imageView.setImageDrawable(thisFrame.drawable);    new Handler().postDelayed(new Runnable() {        @Override        public void run() {            // Make sure ImageView hasn't been changed to a different Image in this time            if (imageView.getDrawable() == thisFrame.drawable) {                if (frameNumber + 1 < myFrames.size()) {                    MyFrame nextFrame = myFrames.get(frameNumber+1);                    if (nextFrame.isReady) {                        // Animate next frame                        animateRawManually(myFrames, imageView, onComplete, frameNumber + 1);                    }                    else {                        nextFrame.isReady = true;                    }                }                else {                    if (onComplete != null) {                        onComplete.run();                    }                }            }        }    }, thisFrame.duration);    // Load next frame    if (frameNumber + 1 < myFrames.size()) {        new Thread(new Runnable() {            @Override            public void run() {                MyFrame nextFrame = myFrames.get(frameNumber+1);                nextFrame.drawable = new BitmapDrawable(imageView.getContext().getResources(), BitmapFactory.decodeByteArray(nextFrame.bytes, 0, nextFrame.bytes.length));                if (nextFrame.isReady) {                    // Animate next frame                    animateRawManually(myFrames, imageView, onComplete, frameNumber + 1);                }                else {                    nextFrame.isReady = true;                }            }        }).run();    }}}

** Solution #2 **

It loads the XML resource, parses it and loads the raw resources - thereby bypassing Android's resource scaling (which is responsible for most OutOfMemoryExceptions), and creates an AnimationDrawable.

Advantages: Performs better on older devices (eg. Galaxy S1)

Disadvantages: Can still run out of RAM as it's holding all of the uncompressed Bitmaps in memory (but they are smaller because they are not scaled the way Android normally scales images)

public static void animateManuallyFromRawResource(int animationDrawableResourceId, ImageView imageView, Runnable onStart, Runnable onComplete) {    AnimationDrawable animationDrawable = new AnimationDrawable();    XmlResourceParser parser = imageView.getContext().getResources().getXml(animationDrawableResourceId);    try {        int eventType = parser.getEventType();        while (eventType != XmlPullParser.END_DOCUMENT) {            if (eventType == XmlPullParser.START_DOCUMENT) {            } else if (eventType == XmlPullParser.START_TAG) {                if (parser.getName().equals("item")) {                    Drawable drawable = null;                    int duration = 1000;                    for (int i=0; i<parser.getAttributeCount(); i++) {                        if (parser.getAttributeName(i).equals("drawable")) {                            int resId = Integer.parseInt(parser.getAttributeValue(i).substring(1));                            byte[] bytes = IoUtils.readBytes(imageView.getContext().getResources().openRawResource(resId));                            drawable = new BitmapDrawable(imageView.getContext().getResources(), BitmapFactory.decodeByteArray(bytes, 0, bytes.length));                        }                        else if (parser.getAttributeName(i).equals("duration")) {                            duration = parser.getAttributeIntValue(i, 66);                        }                    }                    animationDrawable.addFrame(drawable, duration);                }            } else if (eventType == XmlPullParser.END_TAG) {            } else if (eventType == XmlPullParser.TEXT) {            }            eventType = parser.next();        }    }    catch (IOException | XmlPullParserException e) {        e.printStackTrace();    }    if (onStart != null) {        onStart.run();    }    animateDrawableManually(animationDrawable, imageView, onComplete, 0);}private static void animateDrawableManually(final AnimationDrawable animationDrawable, final ImageView imageView, final Runnable onComplete, final int frameNumber) {    final Drawable frame = animationDrawable.getFrame(frameNumber);    imageView.setImageDrawable(frame);    new Handler().postDelayed(new Runnable() {        @Override        public void run() {            // Make sure ImageView hasn't been changed to a different Image in this time            if (imageView.getDrawable() == frame) {                if (frameNumber + 1 < animationDrawable.getNumberOfFrames()) {                    // Animate next frame                    animateDrawableManually(animationDrawable, imageView, onComplete, frameNumber + 1);                }                else {                    // Animation complete                    if (onComplete != null) {                        onComplete.run();                    }                }            }        }    }, animationDrawable.getDuration(frameNumber));}

If you are still having memory issues, use smaller images... or store the resource name + duration, and generate the byte-array + Drawable on each frame. That would almost certainly cause too much chopping between frames, but uses almost zero RAM.