Out of memory error on Android

I am developing an android game application where I have about 15 screens. When I continuously play, I get an out of memory error problem randomly, sometimes in the 15th screen, and sometimes in the 12th screen or so.

Also, check out the xml below, of one the screens.

      <?xml version="1.0" encoding="UTF-8"?>
       <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
           android:id="@+id/MainFrame"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent" >

   <RelativeLayout
       android:id="@+id/mainlayout"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:background="@drawable/woodenbg1" >

       <Chronometer
        android:id="@+id/chronometer"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:textSize="20sp"
        android:textStyle="bold"
        android:typeface="sans"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/imageline"
        android:layout_width="10000dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="105dp"
        android:src="@drawable/lineblackfornormal" />

    <ImageView
        android:id="@+id/imageviewunderalphac"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="350dp"
        android:src="@drawable/whitec" />

    <ImageView
        android:id="@+id/backgroundofalphab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"
        android:src="@drawable/backgroundfordragimages"
        android:visibility="invisible" />

      <ImageView
          android:id="@+id/backgroundofalphaa"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="20dp"
          android:layout_marginTop="35dp"
          android:src="@drawable/backgroundfordragimages"
          android:visibility="invisible" />

       <ImageView
           android:id="@+id/backgroundofalphac"
           android:layout_width="100dp"
           android:layout_height="wrap_content"
           android:layout_alignParentRight="true"
           android:layout_marginTop="33dp"
           android:src="@drawable/backgroundfordragimages"
           android:visibility="invisible" />

       <ImageView
          android:id="@+id/imageviewunderleftalphab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_marginBottom="250dp"
          android:layout_marginLeft="15dp"
          android:src="@drawable/whiteb" />

      <ImageView
          android:id="@+id/imageviewunderrightalphaa"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentRight="true"
          android:layout_marginBottom="250dp"
          android:layout_marginRight="10dp"
          android:src="@drawable/whitea" />

      <ImageView
          android:id="@+id/imageviewabovealphab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="35dp"
          android:src="@drawable/colouredb" />

       <ImageView
          android:id="@+id/imageviewabovealphaa"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="20dp"
          android:layout_marginTop="35dp"
          android:src="@drawable/coloureda" />

       <ImageView
          android:id="@+id/imageviewabovealphac"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/backgroundofalphac"
          android:layout_marginTop="33dp"
          android:src="@drawable/colouredc" />
       </RelativeLayout>

     </FrameLayout>

Logcat

  05-16 12:22:18.989: E/GraphicsJNI(6745): VM won't let us allocate 2225664 bytes
  05-16 12:22:18.999: E/AndroidRuntime(6745): FATAL EXCEPTION: main
  05-16 12:22:18.999: E/AndroidRuntime(6745): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.graphics.Bitmap.nativeCreate(Native Method)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.content.res.Resources.loadDrawable(Resources.java:1709)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.content.res.Resources.getDrawable(Resources.java:581)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.view.View.setBackgroundResource(View.java:7533)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at com.ssn.myapp.NumbersLevel3.onCreate(NumbersLevel3.java:173)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2832)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.app.ActivityThread.access$1600(ActivityThread.java:117)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.os.Handler.dispatchMessage(Handler.java:99)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.os.Looper.loop(Looper.java:130)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at android.app.ActivityThread.main(ActivityThread.java:3683)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at java.lang.reflect.Method.invokeNative(Native Method)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at java.lang.reflect.Method.invoke(Method.java:507)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  05-16 12:22:18.999: E/AndroidRuntime(6745):     at dalvik.system.NativeStart.main(Native Method)

Recycle bitmaps when not in use. After honeycomb bitmaps stay on heap.

 bitmaps.recycle();

http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html.

The link above has a topic on how to avoid memory leaks.

Also if you have high resolution image , you should scale down. See the topic under Load a Scaled Down Version into Memory.

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Suppose you navigate form actiivty A to B. In onResume() load bitmaps. In onPause() recycle bitmaps. Do this for every activity where you load bitmaps.

You can use MAT Analyzer as shwon inthe video below to analyze and fix memeory leaks http://www.youtube.com/watch?v=_CruQY55HOk

You could use android:largeHeap in manifest under application tag.

Warning : As the heap size increases the GC kicks in more frequently and more frequently app pauses. So just because you require a bigger heap you should not use this. You can see the video above and the guy warns about the same.

 android:largeHeap

Whether your application's processes should be created with a large Dalvik heap. This applies to all processes created for the application. It only applies to the first application loaded into a process; if you're using a shared user ID to allow multiple applications to use a process, they all must use this option consistently or they will have unpredictable results.

Most apps should not need this and should instead focus on reducing their overall memory usage for improved performance. Enabling this also does not guarantee a fixed increase in available memory, because some devices are constrained by their total available memory.

To query the available memory size at runtime, use the methods getMemoryClass() or getLargeMemoryClass().