Exception : OutOfMemoryError

前端 未结 7 800
北海茫月
北海茫月 2020-11-29 13:49

I have published my app in play store. Now In Crashes and ANRs I am getting following errors on 2 devices (Galaxy Note3 and Galaxy Note II). I dont know how to solve these e

相关标签:
7条回答
  • 2020-11-29 14:26

    You have to scale down your image resource when loading it.

    If you are loading the image, using a Bitmap variable, you can use the inSample option, using BitmapFactory.Options. Don't use the Bitmap.createScaledBitmap as it will create another bitmap besides the original one!

    Try the following code to load the bitmap:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true; //This will just get the size of your image
    BitmapFactory.decodeFile(pictureFile.getAbsolutePath(), options);
    //Get the downscale ratio
    options.inJustDecodeBounds = false;
    options.inSampleSize = calculateInSampleSize(options, widthOfTheImageView, heightOfTheImageView);
    //Decode the bitmap using the downsampled image
    Bitmap finalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image_name options);
    //Finally, set the bitmap
    imageView.setImageBitmap(finalBitmap);
    

    Where widthOfTheImageView and heightOfTheImageView represent the size of your ImageView.

    The calculateInSampleSize method (I got it from the Android developers tutorial):

    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) {
    
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
    
            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }
    
    0 讨论(0)
  • 2020-11-29 14:28

    Remove all the views when you exit from an activity. Make it as null in OnDestroy().

    if you are using Bitmaps

    use

     bitmap.recycle();
    

    when you used it.

    you may use

     android:largeHeap="true"
    

    in you manifest.xml to increase the heap size.

    0 讨论(0)
  • 2020-11-29 14:29

    Do not load Bitmap directly if you are not sure about the size of the image. Sample it when the size is too large. At most time, the length of an image loaded should not exceed the length of your phone screen. Use code like this to sample image.

    public static Bitmap loadImage(Context cx, Uri uri, int maxLength) {
        InputStream is = cx.getContentResolver().openInputStream(uri);
        Options opts = new Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, opts);
        int length = Math.max(opts.outWidth, opts.outHeight);
        int n = 1;
        while (length > maxLength) {
            maxLength /= 2;
            n ++;
        }
        is = cx.getContentResolver().openInputStream(uri);
        opts.inJustDecodeBounds = false;
        opts.inPurgeable = true;
        opts.inDither = true;
        opts.inPurgeable = true;
        opts.inSampleSize = sample;
        Bitmap bm = BitmapFactory.decodeStream(is, null, opts);
        return bm;
    }
    
    0 讨论(0)
  • 2020-11-29 14:30

    One problem I've had a couple of times is having a lot of MDPI drawable resources but no HDPI / XHDPI versions. When loading these on an HDPI or XHDPI device, Android will scale them up by loading the original, then making a a scaled up copy in memory, meaning you've got two copies in memory at once, the original and a scaled up version. If the drawable is being stretched by the renderer (e.g. If it's a background for a view), then there's no need for Android to scale it up when it's loaded, as the renderer does that anyway. So if you only have one MDPI copy of the image and don't want Android to scale it up, you should put it in the drawable-nodpi folder instead of the drawable or drawable-mdpi folders.

    EDIT:

    You can use the render time scaling available on an image view by setting the android:scaleType attribute. E.g. you could set the image view's width and height to match_parent, then set the scale type to centerCrop or centerInside to have the image scale up when it's drawn while maintaining its aspect ratio. centerCrop will totally fill the image view but potentially cut off some of the image, whereas centerInside ensures that the whole image is visible, but may leave some blank space. The scaling will be done at render time, so won't use any extra memory.

    It can all be done in the layout XML like so:

    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:scaleType="centerCrop"
    

    To have the image displayed fully inside the image view without cropping, change the last line to:

    android:scaleType="centerInside"
    

    Finally, if you don't want to maintain the aspect ratio, you can use the following scale type:

    android:scaleType="fitXY"
    
    0 讨论(0)
  • 2020-11-29 14:33

    Sometimes when we use so many images & backgrounds in applications, it takes lot of space on android RAM. This leads to force close your application by “Out of memory Bound Exception”.

    It seems from your crash logs that you have drawable which is quite large, so to avoid the OutOfMemory you have keep drawables that would not take much memory. You can scale down the drawable either as some guys suggested in the answers.

    If you don't want go through that and you have not much large size drawable then you can opt to increase your application heap size to large that would increase the heap memory.

    How to increase heap size

    You can use android:largeHeap="true" in application tag of Android manifest(Reference here) to request a larger heap size, but this will not work on any pre Honeycomb devices.

    On pre 2.3 devices, you can use the VMRuntime class, but this will not work on Gingerbread and above See below how to do it.

    VMRuntime.getRuntime().setMinimumHeapSize(BIGGER_SIZE);
    

    Before Setting HeapSize make sure that you have entered the appropriate size which will not affect other application or OS functionality.

    Before settings just check how much size your app takes & then set the size just to fulfill your job. Dont use so much of memory otherwise other apps might affect.

    Update

    Note : It is always best practice to free the allocated memory of the bitmaps when these wont longer required, you can this bitmap.recycle()

    0 讨论(0)
  • 2020-11-29 14:37

    Try to create scaled bitmap using following line of the code:

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, width,
                    height, true);
    

    Check the following links:

    android - out of memory exception when creating bitmap

    Android out of memory exception with bitmaps

    Can I catch out of memory exception in Android in Bitmap allocation for decoding a picture file?

    0 讨论(0)
提交回复
热议问题