title: java.lang.OutOfMemoryError: [memory exhausted] in android
body: <p>I am making this android app which requires solving puzzle8 using A star algorithm. In order to implement this, I used a comparator as shown below:</p>
  
  <pre><code>public class PuzzleBoardComparator implements Comparator&lt;PuzzleBoard&gt; {
  @Override
  public int compare(PuzzleBoard A, PuzzleBoard B) {
      Integer priorityA=A.priority();
      Integer priorityB=B.priority();
      if(priorityA.compareTo(priorityB)&lt;0)
          return -1;
      if(priorityA.compareTo(priorityB)&gt;0)
          return 1;
      else
          return 0;
      }
  }
  </code></pre>
  
  <p>This comaparator uses a priority() which is based on calculating Manhattan Distance and is given below:</p>
  
  <pre><code>public int priority() {
      int manhattanDistance=0;
      for(int x=0;x&lt;NUM_TILES;x++){
          for(int y=0;y&lt;NUM_TILES;y++){
                if(tiles.get(XYtoIndex(x,y))!=null){
                      int numTile=tiles.get(XYtoIndex(x,y)).getNumber();
                      numTile-=1;
                      int xNumTile=numTile/NUM_TILES;
                      int yNumTile=numTile%NUM_TILES;
                      manhattanDistance+=Math.abs(x-xNumTile)+Math.abs(y-yNumTile);
                }
          }
      }
      return manhattanDistance+steps;
  }
  </code></pre>
  
  <p>Now, this is the implementation of A-star algorithm to solve the puzzle:</p>
  
  <pre><code>public void solve() {
      //Log.d("DEMO1","LOOP");
      PuzzleBoardComparator comparator=new PuzzleBoardComparator();
      //Log.d("DEMO2","LOOP");
      PriorityQueue&lt;PuzzleBoard&gt; queue=new PriorityQueue&lt;&gt;(10,comparator);
      //Log.d("DEMO3","LOOP");
      puzzleBoard.steps=0;
      puzzleBoard.previousBoard=null;
      queue.add(puzzleBoard);
      while(!queue.isEmpty()){
            //Log.d("DEMO4","LOOP");
            PuzzleBoard cBoard=queue.remove();
            if(cBoard.resolved()){
                 ArrayList&lt;PuzzleBoard&gt; solution=new ArrayList&lt;&gt;();
                 //solution.add(cBoard);
                 while(cBoard.getParent()!=null){
                       solution.add(cBoard);
                       cBoard=cBoard.getParent();
                 }
                 solution.add(cBoard);
                 Collections.reverse(solution);
                 animation=solution;
                 break;
            }
            ArrayList&lt;PuzzleBoard&gt; neigh;
            neigh=cBoard.neighbours();
            Iterator&lt;PuzzleBoard&gt; it=neigh.iterator();
            while(it.hasNext()){
                 queue.add(it.next());
            }
      }
  }
  </code></pre>
  
  <p>But, My application is not working at all. Whenever I press a button to solve the puzzle, nothing happen. After certain amount of time I get this error message on logcat:</p>
  
  <pre><code>07-14 15:14:08.491 6172-6172/com.google.engedu.puzzle8 E/dalvikvm-heap: Out of memory on a 64-byte allocation.
      07-14 15:14:09.482 6172-6172/com.google.engedu.puzzle8 E/dalvikvm-heap: Out of memory on a 184-byte allocation.
      07-14 15:14:09.662 6172-6172/com.google.engedu.puzzle8 E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.google.engedu.puzzle8, PID: 6172
      java.lang.IllegalStateException: Could not execute method of the activity
      at android.view.View$1.onClick(View.java:3823)
      at android.view.View.performClick(View.java:4438)
      at android.view.View$PerformClick.run(View.java:18422)
      at android.os.Handler.handleCallback(Handler.java:733)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:136)
      at android.app.ActivityThread.main(ActivityThread.java:5017)
      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:785)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
      at dalvik.system.NativeStart.main(Native Method)
      Caused by: java.lang.reflect.InvocationTargetException
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:515)
      at android.view.View$1.onClick(View.java:3818)
      at android.view.View.performClick(View.java:4438) 
      at android.view.View$PerformClick.run(View.java:18422) 
      at android.os.Handler.handleCallback(Handler.java:733) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5017) 
      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:785) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
      at dalvik.system.NativeStart.main(Native Method) 
      Caused by: java.lang.OutOfMemoryError: [memory exhausted]
      at dalvik.system.NativeStart.main(Native Method) 
  </code></pre>
  
  <p>Please help me to resolve the issue</p>
  
  <p>EDIT 1: This is the element of xml file where onclick attribute is present.</p>
  
  <pre><code>&lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/solve_button_label"
      android:id="@+id/solve_button"
      android:onClick="solve"
      android:layout_alignParentBottom="true"
      android:layout_toRightOf="@+id/shuffle_button"
      android:layout_toEndOf="@+id/shuffle_button" /&gt;
  </code></pre>
  
  <p>EDIT 2: This is the github link to the project <code>https://github.com/akhileshydv/Puzzle8.git</code></p>