Exception : OutOfMemoryError

2019-01-07 01:03发布

问题:

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 errors and what type of error is this? So please help me to fix these errors. On other devices I am not getting any report there.

Errors-

java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872)
at android.content.res.Resources.loadDrawable(Resources.java:3022)
at android.content.res.Resources.getDrawable(Resources.java:1586)
at android.view.View.setBackgroundResource(View.java:16120)
at com.info.laughingbuddha.Buddha4.onCreateView(Buddha4.java:21)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:572)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1259)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
at dalvik.system.NativeStart.main(Native Method)

Buddha4.java-

package com.info.laughingbuddha;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class Buddha4 extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.buddha, container, false);
    TextViewEx t = (TextViewEx) rootView.findViewById(R.id.textView2);
    t.setText("The Standing Happy Buddha brings riches and happiness.",true);
    t.setMovementMethod(new ScrollingMovementMethod());
    ImageView iv = (ImageView) rootView.findViewById(R.id.image1);
    iv.setBackgroundResource(R.drawable.buddha4);
    return rootView;
    }
}

I dont know what code I need to post so If anyone require any code related to this please comment. Thanks.

回答1:

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?



回答2:

If you have high resolution image , you should scale them down to avoid different devices to load the image without facing a memory problem. In your case, some phones may not exhibit the same behavior on the first run, but eventually, without handling an optimized image loading solution, app will crash.

Check more on the "memory problem":

Topic under Load a Scaled Down Version into Memory.
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Out of memory error on Android

On how to avoid them:
How to avoid an out of memory error while using bitmaps in Android

For an overview:
http://blogs.innovationm.com/android-out-of-memory-error-causes-solution-and-best-practices/ http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html

Before setting a drawable as the background for your imageview, i.e.:

iv.setBackgroundResource(R.drawable.buddha4);  

as @rup35h suggested in the answer, get a scaled bitmap or try other options like inSampleSize, do check how your solution affects the quality of your image too.



回答3:

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



回答4:

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"


回答5:

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


回答6:

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


回答7:

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.