java.lang.OutOfMemoryError:无法分配6553612字节分配3696144个空闲字节和3MB直到OOM

时间:2017-07-13 21:55:04

标签: android

当我加载我的EmojiActivity时,它会加载相应的布局:

setContentView(R.layout.activity_emoji);

该布局的GridLayout为25 ImageView

<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>

图形位于@drawable中,磁盘上的总大小为2.4 MB。它们每个大约100千字节。所以我不知道为什么这么轻的负载会出现内存不足的错误:

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'

我正在尝试创建Snapchat克隆,并且在Snapchat中他们正在屏幕上加载25个表情符号。我正在使用小尺寸的图像。那么发生了什么?

1 个答案:

答案 0 :(得分:1)

  

每个都是大约100千字节

存储在磁盘上的图像会被压缩,至少是针对Android资源的相关格式。内存中的大小基于未压缩大小(宽x高x 4字节/像素)......充其量。

  

图形位于@drawable

如果你的意思是res/drawable/,那就是res/drawable-mdpi/的同义词。将图像放在那里告诉Android在其他密度设备上运行时重新采样这些图像。因此,例如,如果您在-xhdpi设备上运行,则位图将占用-mdpi设备上的堆空间的四倍。

  

该布局有一个带有25个ImageView的GridLayout。

100KB的压缩图像是相当大的图像。例如,PNG大约为800x600像素。你无法一次在一个屏幕上安装其中的25个。

所以,解决这个问题:

  • 使用低分辨率图像
  • 根据实际密度将它们放在正确的目录中,而不是res/drawable/