0

I have tried this : OutOfMemoryError

And read many other such posts , but none of them worked

This is the error :

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.lud.root.jetfighter, PID: 19789
                  java.lang.OutOfMemoryError: Failed to allocate a 4940652 byte allocation with 2168368 free bytes and 2MB until OOM
                      at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                      at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                      at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:651)
                      at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:486)
                      at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1085)
                      at android.content.res.Resources.loadDrawableForCookie(Resources.java:2879)
                      at android.content.res.Resources.loadDrawable(Resources.java:2768)
                      at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
                      at android.graphics.drawable.AnimationDrawable.inflateChildElements(AnimationDrawable.java:324)
                      at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:294)
                      at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1220)
                      at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1129)
                      at android.content.res.Resources.loadDrawableForCookie(Resources.java:2874)
                      at android.content.res.Resources.loadDrawable(Resources.java:2768)
                      at android.content.res.Resources.getDrawable(Resources.java:932)
                      at android.support.v7.widget.ResourcesWrapper.getDrawable(ResourcesWrapper.java:133)
                      at android.content.Context.getDrawable(Context.java:465)
                      at android.view.View.setBackgroundResource(View.java:17566)
                      at android.support.v7.widget.AppCompatImageView.setBackgroundResource(AppCompatImageView.java:76)
                      at com.lud.root.jetfighter.Instruction.onCreate(Instruction.java:32)
                      at android.app.Activity.performCreate(Activity.java:6583)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2531)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
                      at android.os.Handler.dispatchMessage(Handler.java:111)
                      at android.os.Looper.loop(Looper.java:207)
                      at android.app.ActivityThread.main(ActivityThread.java:5769)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
I/System: FinalizerDaemon: finalize objects = 381

Reading some other posts I came to know of large bitmaps exceeding the memory limit, but the files are too small to I think exceed the limit ( they are 8 to 12kB max .png files)

I have read many of the posts referring to the bitmap problem but none could help me.

I tried this : android:largeHeap="true"

But since it didn't work , I read another article and it suggested to remove it , so I did it .

My Instuction.java file which runs into the error:

setContentView(R.layout.activity_instruction);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ll = (LinearLayout)findViewById(R.id.instruction);
ll.setOnClickListener(this);
imv1 = (ImageView) findViewById(R.id.instruction_imv1);
imv3 = (ImageView) findViewById(R.id.instruction_imv3);
imv1.setBackgroundResource(R.drawable.inst);   // **This is where error occurs**
imv3.setBackgroundResource(R.drawable.collision); 

tap = (AnimationDrawable) imv1.getBackground();
tap.start();

collision = (AnimationDrawable) imv3.getBackground();
collision.start();

I loaded 22 files for the inst animation.

This is my inst.xml file:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/selected" android:oneshot="false">
    <item android:drawable="@drawable/enlarge001" android:duration="100" />
    <item android:drawable="@drawable/enlarge002" android:duration="100" />
    <item android:drawable="@drawable/enlarge003" android:duration="100" />
    <item android:drawable="@drawable/enlarge005" android:duration="100" />
    <item android:drawable="@drawable/enlarge006" android:duration="100" />
    <item android:drawable="@drawable/enlarge007" android:duration="100" />
    <item android:drawable="@drawable/enlarge008" android:duration="100" />
    <item android:drawable="@drawable/enlarge009" android:duration="100" />
    <item android:drawable="@drawable/enlarge010" android:duration="100" />
    <item android:drawable="@drawable/enlarge011" android:duration="100" />
    <item android:drawable="@drawable/enlarge012" android:duration="100" />
    <item android:drawable="@drawable/enlarge013" android:duration="100" />
    <item android:drawable="@drawable/enlarge014" android:duration="100" />
    <item android:drawable="@drawable/enlarge015" android:duration="100" />
    <item android:drawable="@drawable/enlarge016" android:duration="100" />
    <item android:drawable="@drawable/enlarge017" android:duration="100" />
    <item android:drawable="@drawable/enlarge018" android:duration="100" />
    <item android:drawable="@drawable/enlarge020" android:duration="100" />
    <item android:drawable="@drawable/enlarge021" android:duration="100" />
    <item android:drawable="@drawable/enlarge022" android:duration="100" />

</animation-list>
Community
  • 1
  • 1
JDFuzyll
  • 77
  • 9
  • `Failed to allocate a 4.940.652 byte allocation with 2.168.368 free bytes and 2MB until OOM` How many files did you load ? – AxelH Nov 23 '16 at 09:18
  • Potentially that line could just be the tip of the iceberg. The high memory consumption could be in some other part of the code. – malmling Nov 23 '16 at 09:19
  • You can use the memory monitor in Android Studio to track your allocations. Maybe you have some huge objects that cannot be garbage collected due to a memory leak. The most common leaked objects are contexts and bitmaps. You can also try library LeakCanary that warns you about the memory leaks. – Marcin Jedynak Nov 23 '16 at 09:22
  • May be this ` setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);` could be the issue you are trying to invoke config change and setting image on it immediately.. Try `Android monitor` to debug the exact root cause of the issue. Note: try to recycle bitmap images using yourBitmap.recycle(); – SaravInfern Nov 23 '16 at 09:22
  • set `setImageResource` instead of `setBackgroundResource`. or `setImageDrawable(getResources().getDrawable(R.drawable.inst));` – IntelliJ Amiya Nov 23 '16 at 09:29
  • at first reduce image size and resolutions – IntelliJ Amiya Nov 23 '16 at 09:38
  • BTW you are missing frame 4 and 19. – Marcin Jedynak Nov 23 '16 at 09:52

2 Answers2

1

An animation list loads all images before starting the animation. If that causes an OOM, you can reduce the number of images, or reduce the size of the images (pixel size, not file size), or both. Or you can create an image loader that loads the images to your ImageView on the fly. Still another solution is to create a video of your animation and show that video.

There's no easy solution I think. I have been working on an app with multiple simultaneous animations, and we were constantly struggling with getting everything to work smoothly. We ended up making compromises in resolution and image sizes, and leaving out some animations.

Christine
  • 5,617
  • 4
  • 38
  • 61
  • I saw my Android Monitor logs and Running the game in my **Lenovo k3 Note** shows memory allocation as high as over **500MB** but running the same game on **Coolpad Note3** allocates only **198MB** and that is the reason why the game crashes on my phone I don't understand why such a difference..... – JDFuzyll Nov 23 '16 at 11:56
  • Thanks.... In the end I had to use VideoView as a replacement , even though my previous app did work on a different phone – JDFuzyll Nov 23 '16 at 12:48
0

How large are the images in terms of pixels. The reason you get an outOfMemroy exception is not to do with the byte sizes of your images, but the pixel sizes. I'd recommend you use 10 of those 22 images as they only animate for 100ms and thus the user will not know the difference

Niza Siwale
  • 2,390
  • 1
  • 18
  • 20