1

I am using AnimationDrawable in my all five Activities. I am getting outofMemory error after some time.

problem is related to Virtual Heap Memory and I am finding a way to remove all the earlier/pervious animation when I tap on the new Activity.

I tried some way to :

 1) Runtime.getRuntime().gc();

 2) activity_name.finish();

 3) startGirlBlinking.stop();
    iView_cow.setBackgroundDrawable(null);

Logcat :

E/AndroidRuntime(11449): java.lang.OutOfMemoryError
E/AndroidRuntime(11449):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
E/AndroidRuntime(11449):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:577)
E/AndroidRuntime(11449):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)

Got help to reslove the Application crash but not for long. I need a valueable solution for this.

How to resove this thing. Plase give me the way to do this.

  • Could you please post the logcat of the crash? Also, are you using Bitmaps too? – fasteque Nov 17 '13 at 20:16
  • Can you post the stack trace for the out of memory exception and the code that you think is causing the problem. It's generally not a good idea to force garbage collection. – Ben Pearson Nov 17 '13 at 21:19
  • Logcat is related to exceedingheap memory .... out of memory .... i am picking imagres from xml for animation list –  Nov 18 '13 at 02:09
  • @BenPearson : I edit the code with Logcat . please have a look on it –  Nov 18 '13 at 02:30
  • @fasteque : I edit the question with the logcat and I am using images from the xmlfile using animation_list. –  Nov 18 '13 at 02:47
  • did you got any other error ? – Akarsh M Nov 18 '13 at 05:18

4 Answers4

4

Extends your AnimationDrawable with this:

public void Recycle() {
        for (int i = 0; i < getNumberOfFrames(); ++i){
            Drawable frame = getFrame(i);
            if (frame instanceof BitmapDrawable) {
                ((BitmapDrawable)frame).getBitmap().recycle();
            }
            frame.setCallback(null);
        }
        setCallback(null);
    }
L.Grillo
  • 960
  • 3
  • 12
  • 26
0

Can you Please Try Like This:

((AnimationDrawable)(someButton.getBackground())).stop();
someButton.setBackgroundDrawable(null);
someButton.setBackgroundResource(R.drawable.animation);
  • but I am not able relase all the memory which is notuseable ... after sometime the crash will definatily occur –  Nov 18 '13 at 02:46
0

I suppose the problem is that the size of resource you decoded is too big. You should use public static Bitmap decodeResourceStream (Resources res, TypedValue value, InputStream is, Rect pad, BitmapFactory.Options opts) to scale down the resource.

yushulx
  • 11,695
  • 8
  • 37
  • 64
  • yes, you are right if the image size is too big but he need to release the memory which is used by the application when he change the Activity. – Akarsh M Nov 18 '13 at 05:17
  • according to the crash log, it's decoding issue. If he can control the bitmap size, even if switch to new Activity, the memory will not be run out. Of course, for saving resource, a good habit is really important that release big resource as Activity is paused. – yushulx Nov 18 '13 at 05:49
0

It looks like the bitmap is too big for the device you're running it on. I would suggest doing the following:

  1. Read the documentation here on good practice for handling bitmap - specifically loading large bitmaps efficiently and managing bitmap memory - http://developer.android.com/training/displaying-bitmaps/index.html
  2. Use a graphics program to scale down the bitmap and place it in the according folders - xhdpi/hdpi/mdpi, etc
  3. Look at changing the Bitmap.Config (options.inPreferredConfig) value when decoding the bitmap - http://developer.android.com/reference/android/graphics/Bitmap.Config.html
Ben Pearson
  • 7,532
  • 4
  • 30
  • 50