0

I have a RecyclerView with many small images (200x200, 8kb). In my Developer Console I get a lot of OOM crash reports from devices with Android version 7 and 7.1.

This is the OutOfMemory crash:

java.lang.OutOfMemoryError: 

  at dalvik.system.VMRuntime.newNonMovableArray (Native Method)

  at android.graphics.BitmapFactory.nativeDecodeAsset (Native Method)

  at android.graphics.BitmapFactory.decodeStream (BitmapFactory.java:700)

  at android.graphics.BitmapFactory.decodeResourceStream (BitmapFactory.java:535)

  at android.graphics.drawable.Drawable.createFromResourceStream (Drawable.java:1179)

  at android.content.res.ResourcesImpl.loadDrawableForCookie (ResourcesImpl.java:770)

  at android.content.res.ResourcesImpl.loadDrawable (ResourcesImpl.java:621)

  at android.content.res.Resources.getDrawable (Resources.java:1640)

  at android.content.Context.getDrawable (Context.java:525)

  at android.support.v4.content.ContextCompat.getDrawable (ContextCompat.java:463)

  at android.support.v7.widget.AppCompatDrawableManager.getDrawable (AppCompatDrawableManager.java:203)

  at android.support.v7.widget.AppCompatDrawableManager.getDrawable (AppCompatDrawableManager.java:191)

  at android.support.v7.content.res.AppCompatResources.getDrawable (AppCompatResources.java:102)

  at android.support.v7.widget.AppCompatImageHelper.setImageResource (AppCompatImageHelper.java:86)

  at android.support.v7.widget.AppCompatImageView.setImageResource (AppCompatImageView.java:94)

  at com.censored.censored.Tab1Adapter.onBindViewHolder (Tab1Adapter.java:135)

  at com.censored.censored.Tab1Adapter.onBindViewHolder (Tab1Adapter.java:19)

  at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder (RecyclerView.java:6781)

  at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder (RecyclerView.java:6823)

  at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline (RecyclerView.java:5752)

  at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline (RecyclerView.java:6019)

  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:5858)

  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:5854)

  at android.support.v7.widget.LinearLayoutManager$LayoutState.next (LinearLayoutManager.java:2230)

  at android.support.v7.widget.GridLayoutManager.layoutChunk (GridLayoutManager.java:557)

  at android.support.v7.widget.LinearLayoutManager.fill (LinearLayoutManager.java:1517)

  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren (LinearLayoutManager.java:612)

  at android.support.v7.widget.GridLayoutManager.onLayoutChildren (GridLayoutManager.java:171)

  at android.support.v7.widget.RecyclerView.dispatchLayoutStep2 (RecyclerView.java:3924)

  at android.support.v7.widget.RecyclerView.dispatchLayout (RecyclerView.java:3641)

  at android.support.v7.widget.RecyclerView.onLayout (RecyclerView.java:4194)

  at android.view.View.layout (View.java:18798)

  at android.view.ViewGroup.layout (ViewGroup.java:5951)

  at android.widget.RelativeLayout.onLayout (RelativeLayout.java:1079)

  at android.view.View.layout (View.java:18798)

  at android.view.ViewGroup.layout (ViewGroup.java:5951)

  at android.support.v4.view.ViewPager.onLayout (ViewPager.java:1775)

  at android.view.View.layout (View.java:18798)

  at android.view.ViewGroup.layout (ViewGroup.java:5951)

  at android.widget.LinearLayout.setChildFrame (LinearLayout.java:1741)

  at android.widget.LinearLayout.layoutVertical (LinearLayout.java:1585)

  at android.widget.LinearLayout.onLayout (LinearLayout.java:1494)

  at android.view.View.layout (View.java:18798)

  at android.view.ViewGroup.layout (ViewGroup.java:5951)

  at android.widget.FrameLayout.layoutChildren (FrameLayout.java:323)

  at android.widget.FrameLayout.onLayout (FrameLayout.java:261)

  at android.view.View.layout (View.java:18798)

  at android.view.ViewGroup.layout (ViewGroup.java:5951)

  at android.support.v4.widget.DrawerLayout.onLayout (DrawerLayout.java:1231)

  at android.view.View.layout (View.java:18798)

  at android.view.ViewGroup.layout (ViewGroup.java:5951)

  at android.widget.LinearLayout.setChildFrame (LinearLayout.java:1741)

  at android.widget.LinearLayout.layoutVertical (LinearLayout.java:1585)

  at android.widget.LinearLayout.onLayout (LinearLayout.java:1494)

  at android.view.View.layout (View.java:18798)

  at android.view.ViewGroup.layout (ViewGroup.java:5951)

  at android.widget.FrameLayout.layoutChildren (FrameLayout.java:323)

  at android.widget.FrameLayout.onLayout (FrameLayout.java:261)

  at android.view.View.layout (View.java:18798)

  at android.view.ViewGroup.layout (ViewGroup.java:5951)

  at android.widget.LinearLayout.setChildFrame (LinearLayout.java:1741)

  at android.widget.LinearLayout.layoutVertical (LinearLayout.java:1585)

  at android.widget.LinearLayout.onLayout (LinearLayout.java:1494)

  at android.view.View.layout (View.java:18798)

  at android.view.ViewGroup.layout (ViewGroup.java:5951)

  at android.widget.FrameLayout.layoutChildren (FrameLayout.java:323)

  at android.widget.FrameLayout.onLayout (FrameLayout.java:261)

  at android.view.View.layout (View.java:18798)

  at android.view.ViewGroup.layout (ViewGroup.java:5951)

  at android.widget.LinearLayout.setChildFrame (LinearLayout.java:1741)

  at android.widget.LinearLayout.layoutVertical (LinearLayout.java:1585)

  at android.widget.LinearLayout.onLayout (LinearLayout.java:1494)

  at android.view.View.layout (View.java:18798)

  at android.view.ViewGroup.layout (ViewGroup.java:5951)

  at android.widget.FrameLayout.layoutChildren (FrameLayout.java:323)

  at android.widget.FrameLayout.onLayout (FrameLayout.java:261)

  at com.android.internal.policy.DecorView.onLayout (DecorView.java:887)

  at android.view.View.layout (View.java:18798)

  at android.view.ViewGroup.layout (ViewGroup.java:5951)

  at android.view.ViewRootImpl.performLayout (ViewRootImpl.java:2648)

  at android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:2364)

  at android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:1515)

  at android.view.ViewRootImpl$TraversalRunnable.run (ViewRootImpl.java:7091)

  at android.view.Choreographer$CallbackRecord.run (Choreographer.java:927)

  at android.view.Choreographer.doCallbacks (Choreographer.java:702)

  at android.view.Choreographer.doFrame (Choreographer.java:638)

  at android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:913)

  at android.os.Handler.handleCallback (Handler.java:751)

  at android.os.Handler.dispatchMessage (Handler.java:95)

  at android.os.Looper.loop (Looper.java:154)

  at android.app.ActivityThread.main (ActivityThread.java:6682)

  at java.lang.reflect.Method.invoke (Native Method)

  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1520)

  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1410)

And this is the code which causes the crash:

holder1.itemImageView.setImageResource(imageResources[position]);

holder1.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View v) {
        final Context context = v.getContext();
        holder1.itemImageView.setImageResource(pressedResource[position]);
        new Handler().postDelayed(new Runnable() {
            public void run() {
                holder1.itemImageView.setImageResource(imageResources[position]);
            }
        }, 500);
        // Calls a method that plays the sound
        // Should be handled in an extra thread
        new Thread(new Runnable() {
            @Override
            public void run() {


                EventHandlerClass.startMediaPlayer(v, soundID);
            }
        }).start();

    }
});

Can I catch the OOM crash and tell it to use a smaller image size? If yes, how do I do that?

I already tried to use the Image Loading Library Picasso to prevent the OOM crash but that causes that the image takes too long to load so it looks as follows: https://media.giphy.com/media/STlGbpXvT8B9iBvnQS/giphy.gif

Or is there any other way to solve the OOM crash?

I really need help, I'm getting many bad reviews at the moment cause of the crash.

Thanks :)

HavanaSun
  • 446
  • 3
  • 12
  • 39
  • *Before* attempting to allocate the memory, you could compare an estimate of how much you'll need to the actual amount of free memory. See also [Catching java.lang.OutOfMemoryError?](https://stackoverflow.com/questions/2679330/catching-java-lang-outofmemoryerror) . – Andy Thomas Oct 25 '19 at 21:58
  • @AndyThomas The problem is that the app is running fine on my device and on all of my Emulators. I only get the crash in the Developer Console. – HavanaSun Oct 25 '19 at 22:01
  • ```try { … } catch (OutOfMemoryException ex) { … }``` with whatever arrangements you need to retry at a smaller size. –  Oct 25 '19 at 22:11

0 Answers0