0

I am facing too many crashes in Kids ABCD application and that too because of Inflate Exceptions which was arising due to OUT OF MEMORY ERROR.

Application is taking so much space, when it is running in foreground, causing it to crash.

This problem arises only when we are switching between different activities so rapidly that the heap area gets filled so fast and application begins to give inflate exception and crash eventually before the space freed on activity destroy. (Rapid switching between activities is done to check the app performance).

The main cause could be large images or application taking more space in cache background storage.

Large size images could not be the problem as we already verified that all images should be of moderate size. Another thing that we had done with the code is that we have finished each activity on each intent by calling finish()method. Here finish() method is used so that the activity could release all the bitmaps from current space (on closing the activity).

super.onCreate(savedInstanceState);
    setContentView(R.layout.ringa_ringa);   

    pause = (ImageView) findViewById(R.id.pause);
    txt = (TextView) findViewById(R.id.txt);
    pause.setVisibility(View.VISIBLE);
    txt.setTextColor(Color.parseColor("#ffffff"));
    list = new ArrayList<>();
    mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "Kids_Rhymes");

    ringa_thread_running = false;



    pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (counter == 0) {
                mediaPlayer.stop();
                ringa_thread_running = false;
                position = 0;
                if (lyrics_ring.isAlive()) {
                    lyrics_ring.interrupt();
                }
                pause.setImageResource(R.drawable.play);
                counter = 1;
            } else if (counter == 1) {
                pause.setImageResource(R.drawable.pause);
                counter = 0;
                onResume();
            }
        }
    });

    String[] words = getResources().getString(R.string.ringa).split("\\$");
    for (String w : words) {
        System.out.println("=======String====" + w);
        list.add(w);

    }

Tried to find out the accurate solution for this but didn't get the suitable one.

Sandip Singh
  • 17
  • 11

2 Answers2

0

put below code in your manifest

 android:largeHeap="true"

like

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
Mahesh Gawhane
  • 348
  • 3
  • 20
0

How to remove inflate exceptions ocurring due to out of memory error

Use less memory overall, an in particular avoid repeated large allocations. You can use Android Studio to help with this, such as by generating a heap dump to see what objects you have in memory at various points.

Large size images could not be the problem as we already verified that all images should be of moderate size

Bear in mind that "moderate size" needs to be measured in terms of heap space used, which is much greater than disk space used, as most images are stored in a compressed form. Also, bear in mind that drawable resources get scaled based upon the screen density of the device and the screen density that you declared for the resource in the resource directory.

Here finish() method is used so that the activity could release all the bitmaps from current space (on closing the activity).

First, the timing of your activity being destroyed is not perfectly synchronous with the timing of the next activity being started. The interactions here are more complex than that.

Second, for bitmaps that you are likely to use again, releasing them and trying to re-allocate them later are one good way to get OutOfMemoryErrors.

Overall, there are books and courses that cover Android memory management. You might consider learning from such resources.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491