Out of memory error when launching Activity in android App?

I am working on an application where one activity is very complex code with different views bloated in one layout like webview (which uses html files from SD card with images), images, scrolls. I am getting these errors when running this operation.

        E/AndroidRuntime(600): Caused by: java.lang.reflect.InvocationTargetException
        E/AndroidRuntime(600):  at java.lang.reflect.Constructor.constructNative(Native Method)
        E/AndroidRuntime(600):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
        E/AndroidRuntime(600):  at android.view.LayoutInflater.createView(LayoutInflater.java:586)
        E/AndroidRuntime(600):  ... 29 more
        E/AndroidRuntime(600): Caused by: java.lang.OutOfMemoryError
        E/AndroidRuntime(600):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        E/AndroidRuntime(600):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
        E/AndroidRuntime(600):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
        E/AndroidRuntime(600):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
        E/AndroidRuntime(600):  at android.content.res.Resources.loadDrawable(Resources.java:1935)
        E/AndroidRuntime(600):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
        E/AndroidRuntime(600):  at android.view.View.<init>(View.java:2785)
        E/AndroidRuntime(600):  at android.view.View.<init>(View.java:2722)
        E/AndroidRuntime(600):  at android.view.ViewGroup.<init>(ViewGroup.java:379)
        E/AndroidRuntime(600):  at android.widget.RelativeLayout.<init>(RelativeLayout.java:174)
        E/AndroidRuntime(600):  ... 32 more

      

How to solve this problem I've tried this code but still don't know what to do?

 @Override
    protected void onDestroy() {
    super.onDestroy();

    unbindDrawables(findViewById(R.id.rootView));
    System.gc();
    }

    private void unbindDrawables(View view) {
        if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
        }
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
        ((ViewGroup) view).removeAllViews();
        }
    }

      

What is a possible way to solve this problem? Please, help

+3


source to share


3 answers


The best way to optimize memory usage with bitmaps in your case is to keep a list of your ImageViews, do this before the call () for each ImageView ends:

((BitmapDrawable) myImageView.getDrawable()).getBitmap().recycle();
myImageView = null;

      



Use code in code onPause

and the main layout of your activity has an id top_layout

:

@Override
protected void onPause() {
    super.onPause();
    unbindDrawables(findViewById(R.id.top_layout));
    System.gc();
}

      

+3


source


After searching and looking at the above answer, I wrote a simple snippet, just changing the code from Android User.I used this, which works successfully. This code will setCallback the background of the view and even recycle all existing bitmaps.

     @Override
        protected void onDestroy() {
        super.onDestroy();

        unbindDrawables(findViewById(R.id.rootView));
        System.gc();
        }

 static void unbindDrawables(View view) {
     try{
     System.out.println("UNBINDING"+view);
            if (view.getBackground() != null) {

                ((BitmapDrawable)view.getBackground()).getBitmap().recycle();


                view.getBackground().setCallback(null);
                view=null;
            }

            if (view instanceof ViewGroup) {
                for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                unbindDrawables(((ViewGroup) view).getChildAt(i));
                }
            ((ViewGroup) view).removeAllViews();
            }

    }catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
 }   

      



EDIT:

The new thing I found today using images less than 20KB in size is good for memory optimization and less memory error should go by.

+2


source


You get an OutOfMemoryException if the image pixels exceed the limit (max resolution is 2048 * 2048)

You can shrink image pixels with Bitmapfactory.Options -

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap myBitmap = BitmapFactory.decodeFile(mUri.getPath(),options);
Drawable d = new BitmapDrawable(Resources.getSystem(),myBitmap);
updateDrawable(d);

      

When using your images in activity, reduce their resolution.

Note. inSampleSize = 2 means it decreases size by 1/2 and resolution by 1/4.

Try to keep inSampleSize with a cardinality of 2 to get the best results too.

Hope this works for you. Any request please comment.

+1


source







All Articles