Out of Memory : android.support.v7.app.AppCompatActivity.onCreate

330 Views Asked by At

we recently switched to using AppCompatActivity instead of FragmentActivity and now some devices are randomly crashing with the following error. No idea why this is happening.

Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 16008012 byte allocation with 4277152 free bytes and 4MB until OOM
   at dalvik.system.VMRuntime.newNonMovableArray(VMRuntime.java)
   at android.graphics.BitmapFactory.nativeDecodeAsset(BitmapFactory.java)
   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)
   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)
   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:703)
   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:733)
   at android.graphics.drawable.BitmapDrawable.updateStateFromTypedArray(BitmapDrawable.java:779)
   at android.graphics.drawable.BitmapDrawable.inflate(BitmapDrawable.java:741)
   at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:2549)
   at android.graphics.drawable.LayerDrawable.inflateLayers(LayerDrawable.java:254)
   at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:164)
   at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:2549)
   at android.graphics.drawable.Drawable.createFromXml(Drawable.java:2320)
   at android.content.res.Resources.loadDrawableForCookie(Resources.java:4210)
   at android.content.res.Resources.loadDrawable(Resources.java:4089)
   at android.content.res.Resources.getDrawable(Resources.java:2005)
   at android.content.res.Resources.getDrawable(Resources.java:1987)
   at android.content.Context.getDrawable(Context.java:464)
   at android.support.v4.content.ContextCompat.android.support.v4.content.ContextCompatApi21.getDrawable(SourceFile:3026)
   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(SourceFile:200)
   at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(SourceFile:81)
   at android.support.v7.app.AppCompatDelegateImplBase.(SourceFile:127)
   at android.support.v7.app.AppCompatDelegateImplV9.(SourceFile:147)
   at android.support.v7.app.AppCompatDelegateImplV11.(SourceFile:27)
   at android.support.v7.app.AppCompatDelegateImplV14.(SourceFile:53)
   at android.support.v7.app.AppCompatDelegateImplV23.(SourceFile:29)
   at android.support.v7.app.AppCompatDelegate.create(SourceFile:203)
   at android.support.v7.app.AppCompatDelegate.create(SourceFile:185)
   at android.support.v7.app.AppCompatActivity.getDelegate(SourceFile:525)
   at android.support.v7.app.AppCompatActivity.onCreate(SourceFile:74)
   at com.dubizzle.horizontal.activities.AbstractActivity.onCreate(SourceFile:98)
   at com.dubizzle.horizontal.activities.ItemPhotoViewer.onCreate(SourceFile:68)
   at android.app.Activity.performCreate(Activity.java:6876)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
   at android.app.ActivityThread.access$1100(ActivityThread.java:222)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:158)
   at android.app.ActivityThread.main(ActivityThread.java:7229)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
2

There are 2 best solutions below

1
On

You are trying to load an image using the BitmapFactory right?

I assume that your image is too big:

16008012 byte allocation with 4277152 free bytes and 4MB until OOM

Your image is 16008012 but you only have 4277152 bytes free. Try using a smaller image.

0
On

This happened to me when I set a rather large drawable as the windowBackground property of the activity's theme. Reducing the size of the image should fix it