Java 内存不足问题

Java 内存不足问题,java,android,memory-management,Java,Android,Memory Management,当我有相当多的结果回来时(大约30个),我的内存问题就出来了。实际误差为: 08-20 10:55:19.820: E/dalvikvm-heap(13483): Out of memory on a 499408-byte allocation. 08-20 10:55:19.835: E/AndroidRuntime(13483): FATAL EXCEPTION: main 08-20 10:55:19.835: E/AndroidRuntime(13483): java.lang.Out

当我有相当多的结果回来时(大约30个),我的内存问题就出来了。实际误差为:

08-20 10:55:19.820: E/dalvikvm-heap(13483): Out of memory on a 499408-byte allocation.
08-20 10:55:19.835: E/AndroidRuntime(13483): FATAL EXCEPTION: main
08-20 10:55:19.835: E/AndroidRuntime(13483): java.lang.OutOfMemoryError
08-20 10:55:19.835: E/AndroidRuntime(13483):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at com.directenquiries.assessment.tool.Globals.Functions.decodeSampledBitmapFromFile(Functions.java:94)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at com.directenquiries.assessment.tool.db.assets.AssetRow.image(AssetRow.java:119)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at com.directenquiries.assessment.tool.ViewAssetsList.loadTable(ViewAssetsList.java:75)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at com.directenquiries.assessment.tool.ViewAssetsList.onResume(ViewAssetsList.java:60)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1188)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at android.app.Activity.performResume(Activity.java:5280)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2606)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at android.os.Looper.loop(Looper.java:137)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at android.app.ActivityThread.main(ActivityThread.java:4898)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at java.lang.reflect.Method.invokeNative(Native Method)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at java.lang.reflect.Method.invoke(Method.java:511)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
08-20 10:55:19.835: E/AndroidRuntime(13483):    at dalvik.system.NativeStart.main(Native Method)
缩小图像的方式是:

public static Bitmap decodeSampledBitmapFromFile(String imagePath, int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imagePath, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(imagePath, options);
    }
图像来自DB,设置如下:

   @Override
        public String imagePath() { 
                if (firstissuePhoto == null) {
                    firstissuePhoto = StationPhotoDB.getFirstPhotoWithStationObjectID(this.stationObjectID);    
                        if(!firstissuePhoto.endsWith("jpg")){
                            firstissuePhoto = "notset";
                        }
                }
                return firstissuePhoto; 

        }



    @Override
    public Bitmap image() {
        if (firstissuePhotoThumb == null) {
            firstissuePhotoThumb =  Functions.decodeSampledBitmapFromFile(imagePath(),200, 200);
        }   
        return firstissuePhotoThumb;
    }
然后,当我实际使用图像并设置它时,它就像:

String picturePath = dbRow.imagePath();
if (picturePath.equals("notset")) {
            holder.ivPicture.setImageResource(R.drawable.photonotset);      
         } else {
            holder.ivPicture.setImageBitmap(dbRow.image());             
          }
编辑:这是请求的例程:

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float)height / (float)reqHeight);
            } else {
                inSampleSize = Math.round((float)width / (float)reqWidth);
            }
        }
        return inSampleSize;
    }

您需要回收未使用的位图。有一个recycle()api。使用它

代码看起来正确,并遵循Android处理大型位图文件的最佳实践

查看堆栈跟踪,内存不足发生在
BitmapFactory.nativeDecodeseam
中。对于JPEG图像,应用程序必须解码(解压缩)图像数据。这种解压缩可能需要在缩放图像之前将其解压缩到内存中。如果图像较大(例如照片),则很容易需要大量内存

由于JPEG图像的块大小可能为8或16,因此您可能需要尝试强制采样大小为8的倍数。这可能有助于图像缩放逻辑不使用太多的内存来解码JPEG


此外,记录
宽度
高度
采样
值应该很有用。

当内存中包含太多位图对象时,可能会出现后计算不简单的内存错误。.我已经添加了calucluteinsamplesize,谢谢您能再详细说明一下吗?我应该在什么阶段进行回收?
recycle
是在位图对象上调用的,当您不再需要它时。因此,您需要将位图与holder关联,然后在(holder.image!=null)holder.image.recycle()时执行
设置图像之前。这只会确保图像被垃圾收集,并且不会真正帮助OOM,除非您正在创建大量位图。添加
Log
跟踪应有助于实现这一点。请参阅。您也可以参考此链接,希望对您有所帮助