0

I have 5 FrameLayout, In all screen i am using AnimationDrawable class for some animation and when Activity goes to another activity or further.

Edit 1 for code snippet what I am Using :

iView_cow.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            try {
                startCowBlinking.stop();
                iView_cow.setBackgroundResource(R.drawable.cow_turn_movement);
                startCowAnimation = (AnimationDrawable) iView_cow.getBackground();
                startCowAnimation.start();
                mHandler.postDelayed(r, 4000);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    });

     mHandler = new Handler();
     r = new Runnable() {

        @Override
        public void run() {
            try {
                if(startCowAnimation.isRunning()){
                    startCowAnimation.stop();
                    iView_cow.setBackgroundResource(R.drawable.cow_blink);

                    startCowBlinking = (AnimationDrawable) iView_cow.getBackground();
                    startCowBlinking.start();
                }
            } catch (Exception e) {
                // TODO: handle exception
            }


        }
    };

Like this animation, I am using 3-4 animation on each activity. Application run fine but After some time I got this error:

dalvikvm-heap:  Out of memory on a 4840016-byte allocation.

And

10-26 00:57:58.601: E/AndroidRuntime(12952): FATAL EXCEPTION: main
10-26 00:57:58.601: E/AndroidRuntime(12952): java.lang.OutOfMemoryError
10-26 00:57:58.601: E/AndroidRuntime(12952):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
10-26 00:57:58.601: E/AndroidRuntime(12952):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:577)
10-26 00:57:58.601: E/AndroidRuntime(12952):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
10-26 00:57:58.601: E/AndroidRuntime(12952):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:775)
10-26 00:57:58.601: E/AndroidRuntime(12952):    at android.content.res.Resources.loadDrawable(Resources.java:1998)
10-26 00:57:58.601: E/AndroidRuntime(12952):    at android.content.res.Resources.getDrawable(Resources.java:707)
10-26 00:57:58.601: E/AndroidRuntime(12952):    at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:280)
10-26 00:57:58.601: E/AndroidRuntime(12952):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:869)
10-26 00:57:58.601: E/AndroidRuntime(12952):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:806)
10-26 00:57:58.601: E/AndroidRuntime(12952):    at android.content.res.Resources.loadDrawable(Resources.java:1983)
10-26 00:57:58.601: E/AndroidRuntime(12952):    at android.content.res.Resources.getDrawable(Resources.java:707)

For solution I did two things when Activity Switch

  1. Finish the currrent activity by finish();
  2. Finish all the AnimationDrawable by using stop();

It helped me to recover this error but not longer. So someone suggest me to what shold I do ?

  • How many drawables are there on this AnimationDrawable? – NOlivNeto Oct 25 '13 at 19:50
  • Avarage 5-6 ... @NOlivNeto –  Oct 25 '13 at 19:52
  • That's strange... It should not be that way. I have an AnimationDrawable with 23 images running sweet! It could be the size and quality of the images, have you checked that? – NOlivNeto Oct 25 '13 at 19:54
  • size less than 100 k and png format... one more thing some dimension of the images are bigger than screen and I just put into ImageView in a fixedsize @NOlivNeto –  Oct 25 '13 at 19:57
  • can u tell me the way to free the memory or release it for AnimationDrawable @NOlivNeto –  Oct 25 '13 at 19:59
  • Well... Think about it. 100K/image, about 30 images running on memory... That's a lot. Try to reduce then. – NOlivNeto Oct 25 '13 at 20:00
  • In every animation Drawable some also running till the activity is alive @NOlivNeto –  Oct 25 '13 at 20:01
  • I posted an answer of how to release memory with bitmaps, but I think you could downsize your images. – NOlivNeto Oct 25 '13 at 20:02
  • @NOlivNeto: tell me where you posted the answer. i'll check this And hmm... I try to downsize of the image dimensions –  Oct 25 '13 at 20:05
  • On this very question heheh. For reducing the quality of the images, check [that](http://www.raymond.cc/blog/4-free-tools-to-optimize-and-compress-png-images-without-loosing-quality/) – NOlivNeto Oct 25 '13 at 20:11

2 Answers2

0

At the end of every activity, release the bitmaps and large objects by calling bitmap.recycle() on Bitmaps (get them from with component.getDrawable().getBitmap()) and setting null to the objects.

NOlivNeto
  • 703
  • 1
  • 5
  • 10
  • All Images what I am using , is in the xml file. So , how can I use the bitmap.recycle() on it –  Oct 26 '13 at 07:12
  • No problem at all, use component.getDrawable().getBitmap(), assuming is a ImageView or something :) – NOlivNeto Oct 28 '13 at 23:25
  • when I tried null to the object , next time When I came back That object whic I assigned null is showing blank ... what is this ? –  Oct 29 '13 at 03:37
  • You should null large objects, not the component image bitmap. For the bitmap you must call recycle() – NOlivNeto Oct 29 '13 at 20:37
0

I Think your drawable images are too large. Better you should reduce the image sizes. It'll give the best result if images size should be drawable-128*128.

Silambarasan Poonguti
  • 9,386
  • 4
  • 45
  • 38