Daniel Viglione
Daniel Viglione

Reputation: 9407

java.lang.OutOfMemoryError: Failed to allocate a 6553612 byte allocation with 3696144 free bytes and 3MB until OOM

When I load my EmojiActivity, it loads the corresponding layout:

setContentView(R.layout.activity_emoji);

That layout has a GridLayout with 25 ImageView's.

<GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:columnCount="5"
        android:rowCount="5">

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="0"
            android:layout_column="0"
            android:src="@drawable/grinning_emoji_with_smiling_eyes" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="0"
            android:layout_column="1"
            android:src="@drawable/hugging_face_emoji" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="0"
            android:layout_column="2"
            android:src="@drawable/hungry_face_emoji" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="0"
            android:layout_column="3"
            android:src="@drawable/kiss_emoji_with_closed_eyes" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="0"
            android:layout_column="4"
            android:src="@drawable/kissing_face_emoji" />


    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="1"
            android:layout_column="0"
            android:src="@drawable/kissing_face_with_smiling_eyes_emoji" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="1"
            android:layout_column="1"
            android:src="@drawable/money_face_emoji" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="1"
            android:layout_column="2"
            android:src="@drawable/nerd_with_glasses_emoji" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="1"
            android:layout_column="3"
            android:src="@drawable/relieved_emoji" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="1"
            android:layout_column="4"
            android:src="@drawable/sad_face_emoji" />


    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="2"
            android:layout_column="0"
            android:src="@drawable/slightly_smiling_face_emoji" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="2"
            android:layout_column="1"
            android:src="@drawable/smiling_emoji_with_eyes_opened" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="2"
            android:layout_column="2"
            android:src="@drawable/smiling_emoji_with_smiling_eyes" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="2"
            android:layout_column="3"
            android:src="@drawable/smiling_face_emoji" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="2"
            android:layout_column="4"
            android:src="@drawable/smiling_face_emoji_with_blushed_cheeks" />


    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="3"
            android:layout_column="0"
            android:src="@drawable/smiling_face_with_closed_eyes" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="3"
            android:layout_column="1"
            android:src="@drawable/smiling_with_sweat_emoji" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="3"
            android:layout_column="2"
            android:src="@drawable/smirk_face_emoji" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="3"
            android:layout_column="3"
            android:src="@drawable/sunglasses_emoji" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="3"
            android:layout_column="4"
            android:src="@drawable/tears_of_joy_emoji" />


    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="4"
            android:layout_column="0"
            android:src="@drawable/tongue_out_emoji" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="4"
            android:layout_column="1"
            android:src="@drawable/tongue_out_emoji_with_closed_eyes" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="4"
            android:layout_column="2"
            android:src="@drawable/tongue_out_emoji_with_winking_eye" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="4"
            android:layout_column="3"
            android:src="@drawable/unamused_face_emoji" />
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="4"
            android:layout_column="4"
            android:src="@drawable/upside_down_face_emoji" />
</GridLayout>

The graphics are located in @drawable and their total size is 2.4 megabytes on disk. Each of them is around 100 kilobytes. So I have no idea why a out of memory error would occur with such a light load:

I/art: Starting a blocking GC Alloc
I/art: Clamp target GC heap from 140MB to 128MB
I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 3% free, 124MB/128MB, paused 668us total 18.401ms
W/art: Throwing OutOfMemoryError "Failed to allocate a 6553612 byte allocation with 4109776 free bytes and 3MB until OOM"
D/skia: --- allocation failed for scaled bitmap
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: mobapptut.com.camera2videoimage, PID: 30354
                  java.lang.OutOfMemoryError: Failed to allocate a 6553612 byte allocation with 4109776 free bytes and 3MB until OOM
                      at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                      at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                      at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
                      at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
                      at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
                      at android.content.res.Resources.loadDrawableForCookie(Resources.java:2738)
                      at android.content.res.Resources.loadDrawable(Resources.java:2643)
                      at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
                      at android.widget.ImageView.<init>(ImageView.java:152)
                      at android.widget.ImageView.<init>(ImageView.java:140)
                      at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
                      at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
                      at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                      at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1013)
                      at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1072)
                      at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                      at mobapptut.com.camera2videoimage.EmojiActivity.onCreate(EmojiActivity.java:11)
                      at android.app.Activity.performCreate(Activity.java:6272)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                      at android.app.ActivityThread.access$900(ActivityThread.java:157)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5551)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
I/art: Clamp target GC heap from 132MB to 128MB
I/Process: Sending signal. PID: 30354 SIG: 9
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'

I am attempting to create snapchat clone and in snapchat they are loading 25 emoji's on the screen. I am using images of small sizes. So what is going on?

Upvotes: 0

Views: 2882

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

Each of them is around 100 kilobytes

Images stored on disk are compressed, at least for the relevant formats for Android resources. The size in memory is based on the uncompressed size (width x height x 4 bytes/pixel)... at best.

The graphics are located in @drawable

If by this you mean res/drawable/, that is a synonym for res/drawable-mdpi/. Putting images there tells Android to resample those images when running on other-density devices. So, for example, if you are running on an -xhdpi device, the bitmap will consume four times the heap space that it would on an -mdpi device.

That layout has a GridLayout with 25 ImageView's.

100KB of a compressed image is a rather large image. A PNG, for example, would be somewhere around 800x600 pixels. There is no way that you are going to fit 25 of those on one screen at a time.

So, to fix this:

  • Use lower-resolution images
  • Put them in a proper directory, based on the actual density, not res/drawable/

Upvotes: 1

Related Questions