【问题标题】:OutOfMemory Error at ListView, I try to garbage Collect but failedListView 出现 OutOfMemory 错误,我尝试垃圾收集但失败
【发布时间】:2014-05-12 20:35:19
【问题描述】:

从我的 ListView 创建新 Activity 时出现 OutOfMemory 错误。我在 Picasso Library 的 listview 中加载图像。

这是由很多位图图像造成的 OutOfMemory 错误吗?

我尝试垃圾收集图像,但我认为它不起作用。

在 Question.java

// this java file return view to adapter at adapter's getView()

public View getView(LayoutInflater inflater, View convertView, int mPosition, NewsFeedListViewAdapter newsFeedListViewAdapter, final Context mContext) 
{



    try
    {
                 /**get view in here**/

    }
    catch (OutOfMemoryError e) 
    {

        newsFeedListViewAdapter.recycleHalf();
        System.gc();
        return getView( inflater, convertView,  mPosition,  newsFeedListViewAdapter,  mContext) ;
    }

    newsFeedListViewAdapter.addRecycleList(
            new WeakReference<View>(
                    newsFeedQuestionViewHolder.imageview_profile_image));







    return convertView;
}

在 NewsFeedListViewAdapter.java

public void recycleHalf() 
{
    int halfSize = mRecycleList.size() / 2;
    List<WeakReference<View>> recycleHalfList = mRecycleList.subList(0, halfSize);
    RecycleUtils.recursiveRecycle(recycleHalfList);

    for (int i = 0; i < halfSize; i++)
        mRecycleList.remove(0);
}


public void recycle() 
{
    RecycleUtils.recursiveRecycle(mRecycleList);
}

public void addRecycleList(WeakReference<View> view)
{
    mRecycleList.add(view);
}

LogCat

05-13 05:15:41.431: D/dalvikvm(19437): GC_FOR_ALLOC freed 3421K, 37% free 29306K/46496K, paused 28ms, total 29ms
05-13 05:15:41.431: I/dalvikvm-heap(19437): Grow heap (frag case) to 31.300MB for 810016-byte allocation
05-13 05:15:41.461: D/dalvikvm(19437): GC_FOR_ALLOC freed 5K, 37% free 30091K/47288K, paused 32ms, total 32ms
05-13 05:15:41.511: D/dalvikvm(19437): GC_FOR_ALLOC freed 6K, 37% free 30085K/47288K, paused 20ms, total 20ms
05-13 05:15:41.511: I/dalvikvm-heap(19437): Grow heap (frag case) to 34.378MB for 3240016-byte allocation
05-13 05:15:41.536: D/dalvikvm(19437): GC_FOR_ALLOC freed 0K, 35% free 33249K/50456K, paused 24ms, total 24ms
05-13 05:15:41.571: D/dalvikvm(19437): GC_FOR_ALLOC freed 801K, 36% free 32494K/50456K, paused 22ms, total 22ms
05-13 05:15:41.576: I/dalvikvm-heap(19437): Grow heap (frag case) to 36.630MB for 3134352-byte allocation
05-13 05:15:41.606: D/dalvikvm(19437): GC_FOR_ALLOC freed 3K, 34% free 35551K/53520K, paused 28ms, total 28ms
05-13 05:15:41.626: D/dalvikvm(19437): GC_FOR_ALLOC freed <1K, 34% free 35551K/53520K, paused 21ms, total 21ms
05-13 05:15:41.631: I/dalvikvm-heap(19437): Forcing collection of SoftReferences for 12537360-byte allocation
05-13 05:15:41.661: D/dalvikvm(19437): GC_BEFORE_OOM freed 70K, 34% free 35480K/53520K, paused 30ms, total 30ms
05-13 05:15:41.661: E/dalvikvm-heap(19437): Out of memory on a 12537360-byte allocation.
05-13 05:15:41.661: I/dalvikvm(19437): "main" prio=5 tid=1 RUNNABLE
05-13 05:15:41.661: I/dalvikvm(19437):   | group="main" sCount=0 dsCount=0 obj=0x422b9578 self=0x41d6fb58
05-13 05:15:41.661: I/dalvikvm(19437):   | sysTid=19437 nice=-8 sched=0/0 cgrp=apps handle=1075113980
05-13 05:15:41.661: I/dalvikvm(19437):   | state=R schedstat=( 35374459365 4630864455 68775 ) utm=3061 stm=476 core=3
05-13 05:15:41.661: I/dalvikvm(19437):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-13 05:15:41.661: I/dalvikvm(19437):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
05-13 05:15:41.661: I/dalvikvm(19437):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
05-13 05:15:41.661: I/dalvikvm(19437):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
05-13 05:15:41.661: I/dalvikvm(19437):   at android.content.res.Resources.loadDrawable(Resources.java:2988)
05-13 05:15:41.661: I/dalvikvm(19437):   at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.widget.ImageView.<init>(ImageView.java:131)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.widget.ImageView.<init>(ImageView.java:121)
05-13 05:15:41.666: I/dalvikvm(19437):   at java.lang.reflect.Constructor.constructNative(Native Method)
05-13 05:15:41.666: I/dalvikvm(19437):   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.view.LayoutInflater.createView(LayoutInflater.java:600)
05-13 05:15:41.666: I/dalvikvm(19437):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
05-13 05:15:41.666: I/dalvikvm(19437):   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.app.Activity.setContentView(Activity.java:1956)
05-13 05:15:41.666: I/dalvikvm(19437):   at com.zeallat.free.WriteFreeActivity.onCreate(WriteFreeActivity.java:45)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.app.Activity.performCreate(Activity.java:5372)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.app.ActivityThread.access$700(ActivityThread.java:168)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.os.Looper.loop(Looper.java:137)
05-13 05:15:41.666: I/dalvikvm(19437):   at android.app.ActivityThread.main(ActivityThread.java:5493)
05-13 05:15:41.666: I/dalvikvm(19437):   at java.lang.reflect.Method.invokeNative(Native Method)
05-13 05:15:41.666: I/dalvikvm(19437):   at java.lang.reflect.Method.invoke(Method.java:525)
05-13 05:15:41.666: I/dalvikvm(19437):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
05-13 05:15:41.666: I/dalvikvm(19437):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
05-13 05:15:41.666: I/dalvikvm(19437):   at dalvik.system.NativeStart.main(Native Method)
05-13 05:15:41.666: D/skia(19437): --- allocation failed for scaled bitmap
05-13 05:15:41.666: D/AndroidRuntime(19437): Shutting down VM
05-13 05:15:41.666: W/dalvikvm(19437): threadid=1: thread exiting with uncaught exception (group=0x422b8700)
05-13 05:15:41.676: E/AndroidRuntime(19437): FATAL EXCEPTION: main
05-13 05:15:41.676: E/AndroidRuntime(19437): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test20140320/com.zeallat.free.WriteFreeActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class <unknown>
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.app.ActivityThread.access$700(ActivityThread.java:168)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.os.Looper.loop(Looper.java:137)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.app.ActivityThread.main(ActivityThread.java:5493)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at java.lang.reflect.Method.invokeNative(Native Method)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at java.lang.reflect.Method.invoke(Method.java:525)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at dalvik.system.NativeStart.main(Native Method)
05-13 05:15:41.676: E/AndroidRuntime(19437): Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class <unknown>
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.view.LayoutInflater.createView(LayoutInflater.java:626)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.app.Activity.setContentView(Activity.java:1956)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at com.zeallat.free.WriteFreeActivity.onCreate(WriteFreeActivity.java:45)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.app.Activity.performCreate(Activity.java:5372)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
05-13 05:15:41.676: E/AndroidRuntime(19437):    ... 11 more
05-13 05:15:41.676: E/AndroidRuntime(19437): Caused by: java.lang.reflect.InvocationTargetException
05-13 05:15:41.676: E/AndroidRuntime(19437):    at java.lang.reflect.Constructor.constructNative(Native Method)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.view.LayoutInflater.createView(LayoutInflater.java:600)
05-13 05:15:41.676: E/AndroidRuntime(19437):    ... 26 more
05-13 05:15:41.676: E/AndroidRuntime(19437): Caused by: java.lang.OutOfMemoryError
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.content.res.Resources.loadDrawable(Resources.java:2988)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.widget.ImageView.<init>(ImageView.java:131)
05-13 05:15:41.676: E/AndroidRuntime(19437):    at android.widget.ImageView.<init>(ImageView.java:121)
05-13 05:15:41.676: E/AndroidRuntime(19437):    ... 29 more

【问题讨论】:

  • 也许您可以按照此处的建议通过解码位图来减少所需的内存量:developer.android.com/training/displaying-bitmaps/load-bitmap.html
  • @MarkW 我只是为了便于查看而删除了它...
  • 如果出现 OOM 错误,System.gc() 不是解决方案。

标签: java android garbage-collection out-of-memory


【解决方案1】:

尝试减小图像的大小,我认为这是第一个问题。

05-13 05:15:41.666: D/skia(19437): --- allocation failed for scaled bitmap

【讨论】:

    【解决方案2】:

    您正在尝试加载 12Mb 的图像,这可能太大而无法放入内存。

    如果您不需要图像的完整定义,您可以使用BitmapFactoryBitmapFactory.Options inSampleSize 来缩放位图加载到内存之前。

    您可以在官方文档中获取更多信息:Loading Large Bitmaps Efficiently

    【讨论】:

    • 我用毕加索图书馆的时候可以用吗??
    • 如果我没记错的话,Picasso 库提供了一个 resize() 方法,它应该为您处理大小调整。您只需要计算尺寸(例如屏幕的宽度,以及相应的高度)
    猜你喜欢
    • 2018-01-08
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2023-02-07
    • 2017-08-22
    • 2019-02-01
    • 1970-01-01
    相关资源
    最近更新 更多