Android:3240016字节分配的内存不足

时间:2014-01-21 01:15:53

标签: android

我总是面对这个问题。经历了很多相同的问题(即this pdflinklink等等。由于我在Android中仍然天真,任何人都可以帮助我吗?

logcat的:

01-20 22:41:50.644: D/dalvikvm(4838): GC_BEFORE_OOM freed <1K, 14% free 55713K/64583K, paused 20ms, total 20ms
01-20 22:41:50.644: E/dalvikvm-heap(4838): Out of memory on a 3240016-byte allocation.
01-20 22:41:50.644: I/dalvikvm(4838): "main" prio=5 tid=1 RUNNABLE
01-20 22:41:50.644: I/dalvikvm(4838):   | group="main" sCount=0 dsCount=0 obj=0x40e11508 self=0x40d2e9a0
01-20 22:41:50.644: I/dalvikvm(4838):   | sysTid=4838 nice=0 sched=0/0 cgrp=apps handle=1075126064
01-20 22:41:50.644: I/dalvikvm(4838):   | schedstat=( 3230609420 544631405 2986 ) utm=302 stm=21 core=0
01-20 22:41:50.644: I/dalvikvm(4838):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
01-20 22:41:50.649: I/dalvikvm(4838):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
01-20 22:41:50.649: I/dalvikvm(4838):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:476)
01-20 22:41:50.649: I/dalvikvm(4838):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
01-20 22:41:50.649: I/dalvikvm(4838):   at android.content.res.Resources.loadDrawable(Resources.java:1963)
01-20 22:41:50.649: I/dalvikvm(4838):   at android.content.res.Resources.getDrawable(Resources.java:672)
01-20 22:41:50.649: I/dalvikvm(4838):   at android.view.View.setBackgroundResource(View.java:14480)
01-20 22:41:50.649: I/dalvikvm(4838):   at axaart.collectors.adapter.CustomListAdapter.getView(CustomListAdapter.java:82)
01-20 22:41:50.649: I/dalvikvm(4838):   at android.widget.AbsListView.obtainView(AbsListView.java:2461)
01-20 22:41:50.649: I/dalvikvm(4838):   at android.widget.ListView.makeAndAddView(ListView.java:1775)
01-20 22:41:50.649: I/dalvikvm(4838):   at android.widget.ListView.fillDown(ListView.java:678)
01-20 22:41:50.649: I/dalvikvm(4838):   at android.widget.ListView.fillFromTop(ListView.java:739)
01-20 22:41:50.649: I/dalvikvm(4838):   at android.widget.ListView.layoutChildren(ListView.java:1628)
01-20 22:41:50.649: I/dalvikvm(4838):   at android.widget.AbsListView.onLayout(AbsListView.java:2296)
01-20 22:41:50.649: I/dalvikvm(4838):   at android.view.View.layout(View.java:14055)
01-20 22:41:50.649: I/dalvikvm(4838):   at android.view.ViewGroup.layout(ViewGroup.java:4604)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.view.View.layout(View.java:14055)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.view.ViewGroup.layout(ViewGroup.java:4604)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.view.View.layout(View.java:14055)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.view.ViewGroup.layout(ViewGroup.java:4604)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1585)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.view.View.layout(View.java:14055)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.view.ViewGroup.layout(ViewGroup.java:4604)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.view.View.layout(View.java:14055)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.view.ViewGroup.layout(ViewGroup.java:4604)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.view.View.layout(View.java:14055)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.view.ViewGroup.layout(ViewGroup.java:4604)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.view.View.layout(View.java:14055)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.view.ViewGroup.layout(ViewGroup.java:4604)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.view.View.layout(View.java:14055)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.view.ViewGroup.layout(ViewGroup.java:4604)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.view.View.layout(View.java:14055)
01-20 22:41:50.654: I/dalvikvm(4838):   at android.view.ViewGroup.layout(ViewGroup.java:4604)
01-20 22:41:50.659: I/dalvikvm(4838):   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-20 22:41:50.659: I/dalvikvm(4838):   at android.view.View.layout(View.java:14055)
01-20 22:41:50.659: I/dalvikvm(4838):   at android.view.ViewGroup.layout(ViewGroup.java:4604)
01-20 22:41:50.659: I/dalvikvm(4838):   at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1992)
01-20 22:41:50.659: I/dalvikvm(4838):   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1813)
01-20 22:41:50.659: I/dalvikvm(4838):   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1112)
01-20 22:41:50.659: I/dalvikvm(4838):   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4472)
01-20 22:41:50.659: I/dalvikvm(4838):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
01-20 22:41:50.659: I/dalvikvm(4838):   at android.view.Choreographer.doCallbacks(Choreographer.java:555)
01-20 22:41:50.659: I/dalvikvm(4838):   at android.view.Choreographer.doFrame(Choreographer.java:525)
01-20 22:41:50.659: I/dalvikvm(4838):   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
01-20 22:41:50.659: I/dalvikvm(4838):   at android.os.Handler.handleCallback(Handler.java:615)
01-20 22:41:50.659: I/dalvikvm(4838):   at android.os.Handler.dispatchMessage(Handler.java:92)
01-20 22:41:50.659: I/dalvikvm(4838):   at android.os.Looper.loop(Looper.java:137)
01-20 22:41:50.659: I/dalvikvm(4838):   at android.app.ActivityThread.main(ActivityThread.java:4898)
01-20 22:41:50.659: I/dalvikvm(4838):   at java.lang.reflect.Method.invokeNative(Native Method)
01-20 22:41:50.659: I/dalvikvm(4838):   at java.lang.reflect.Method.invoke(Method.java:511)
01-20 22:41:50.659: I/dalvikvm(4838):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-20 22:41:50.659: I/dalvikvm(4838):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-20 22:41:50.659: I/dalvikvm(4838):   at dalvik.system.NativeStart.main(Native Method)
01-20 22:41:50.664: A/libc(4838): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 4838 (aart.collectors)

CustomListAdapter.java

public class CustomListAdapter extends ArrayAdapter<ItemDetailTO> {

    private List<ItemDetailTO> aList;
    private List<ItemDetailTO> orignalList;
    private LayoutInflater inflater;
    private ItemDetailTO info = null;
    private byte[] decodeStringImage;
    Bitmap decodedByteImage;
//  private TextView txtvwLocation;
//  private Context context;

    public CustomListAdapter(Context context, int textViewResourceId, List<ItemDetailTO> list) {
        super(context, textViewResourceId, list);
        // TODO Auto-generated constructor stub
//      this.context = context;
        this.aList = list;
        this.orignalList = list;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//      inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.common_list_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(R.id.holder, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.holder);
        }
//      View rowView = inflater.inflate(R.layout.common_list_item, parent, false);
        ListColor listColorInstance = new ListColor();

//      if(view == null) {
//          view = inflater.inflate(R.layout.common_list_item, parent, false);          
//      }

        listColorInstance.listcolorsetter(convertView, position);
        info  = aList.get(position);

        if(info != null) {
            holder.txtvwTitle.setText(info.getsTitle());
            holder.txtvwCategory.setText(info.getsCategory());

            if (info.getsCcy() != null) {
                holder.txtvwValue.setText(String.format("%.2f", info.getdValue()) + " " + info.getsCcy());
            } else {
                holder.txtvwValue.setText(String.format("%.2f", info.getdValue()));
            }

            if(info.getImage() != null) {
                decodeStringImage = Base64.decode(info.getImage(), Base64.DEFAULT);
                decodedByteImage = BitmapFactory.decodeByteArray(decodeStringImage, 0, decodeStringImage.length);
                holder.imgvwItemImage.setImageBitmap(decodedByteImage);
            } else {
                holder.imgvwItemImage.setBackgroundResource(R.drawable.image_placeholder); 
            }
        }

        if (decodedByteImage != null) {
            decodedByteImage = null;
            System.gc();
        }

//      return rowView;
        return convertView;
    }

    @Override
    public int getCount() {
        return aList.size();
    }

    public void resetData() {
        // TODO Auto-generated method stub
        aList = orignalList;
    }

    private class ViewHolder {
        private ImageView imgvwItemImage;
        private TextView txtvwTitle;
        private TextView txtvwCategory;
        private TextView txtvwValue;

        public ViewHolder(View root) {
            imgvwItemImage = (ImageView) root.findViewById(R.id.imgvwItemImage);
            txtvwTitle = (TextView) root.findViewById(R.id.txtvwtitle);
            txtvwCategory = (TextView) root.findViewById(R.id.txtvwcategory);
            txtvwValue = (TextView) root.findViewById(R.id.txtvwvalue);
        }
    }
}

注意:

当我尝试在任何多个活动中填充listview时,会出现此问题。

4 个答案:

答案 0 :(得分:4)

您可以尝试将此行添加到清单文件中。它将为您的应用程序分配大内存。

android:largeHeap="true"

答案 1 :(得分:2)

您使用的每个位图在内存中的成本为4 MB。您可以通过以下方法使用调整大小的位图。

private Bitmap decodeBitmap(int targetWidth, int targetHeight) throws IOException {
    final BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(getAssets().open("frog.jpg"), null, opts);

    opts.inSampleSize = getBitmapSampleSize(opts, targetWidth, targetHeight);

    opts.inJustDecodeBounds = false;

    return BitmapFactory.decodeStream(getAssets().open("frog.jpg"), null, opts);
}

private int getBitmapSampleSize(BitmapFactory.Options opts, int targetWidth, int targetHeight) {
    final int width = opts.outWidth;
    final int height = opts.outHeight;
    Log.d("tag", "bitmap's size is " + width + ", " + height);
    int inSampleSize = 1;

    if (height > targetHeight || width > targetWidth) {
        if (width > height) {
            inSampleSize = (int) Math.floor((float)height / (float)targetHeight);
        } else {
            inSampleSize = (int) Math.floor((float)width / (float)targetWidth);
        }
    }
    Log.d("tag", "target size is " + targetWidth + ", " + targetHeight);
    Log.d("tag", "inSampleSize is " + inSampleSize);
    return inSampleSize;
}

答案 2 :(得分:0)

Android对单个应用程序为自己分配的内存有非常严格的限制。即使设备上有可用内存,但这并不意味着操作系统会将其提供给您。所以你几乎没有运气。您可以执行以下操作:

  1. 使用更高效的数据表示,不需要如此大的分配。也许您可以在设备外部的Web服务上压缩/预处理数据。

  2. 如果这不是一个选项,您可以构建自己的平台映像并更改每个应用程序的内存限制设置。 Look here for more information.

  3. 如果(2)不是您的选择。您可以编写一个本地库,它将为您保存数据。内存限制仅适用于Java / Dalvik分配。本机代码可以做任何想做的事。

答案 3 :(得分:0)

decodedByteImage = BitmapFactory.decodeByteArray(decodeStringImage, 0, decodeStringImage.length);
建议使用

imageloader。 https://github.com/nostra13/Android-Universal-Image-Loader