title: How to solve FATAL EXCEPTION in AsyncTask caused by java.lang.OutOfMemoryException
body: <p>I wrote this code in my <code>AsyncTask</code>, but I do not understand this <code>Exception</code>. Can someone tell me where I'm wrong? I don't know what I have to do. Here are my 3 methods in my <code>AsyncTask</code>.</p>
  
  <pre><code>protected void onPreExecute() {
  
          HttpParams params = httpClient.getParams();
  
  
          if(script.contains("appscan.php"))
          {
              Params.printLog(TAG,"onPreExecute appscan.php script:"+script);
              HttpConnectionParams.setConnectionTimeout(params, 10000);
              HttpConnectionParams.setSoTimeout(params, 10000);
          }
          else
          {
              Params.printLog(TAG,"onPreExecute  script:"+script);
              HttpConnectionParams.setConnectionTimeout(params, 120000);
              HttpConnectionParams.setSoTimeout(params, 120000);
          }
      }
  
      protected String doInBackground(Void... urls) {
          String st_response="";
          try {
              final HttpResponse response;
              //synchronized (httpClient) {
                  if(request==null)
                       cancel(true);
                  String m_url;
                  if(isPost)
                  {
                      m_url=baseUrl+script;
                      HttpPost httppost = new HttpPost(m_url);
                      List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(2);
                      nameValuePairs.add(new BasicNameValuePair("id", "12345"));
  
                      httppost.setEntity(new UrlEncodedFormEntity((List&lt;NameValuePair&gt;)request));
                      if(getClient()==null)
                      {
                          return "";
                      }
                      response = getClient().execute(httppost);
  
                  }
                  else
                  {
                      String m_request=request==null?"":Uri.encode(request.toString());
                      if(DEBUG)
                          m_url=baseUrl1+script+m_request;
                      else
                          m_url=baseUrl+script+m_request;
  
                      Params.printLog(TAG,"run :"+m_url);
                      HttpUriRequest request1=new HttpGet(m_url);
                      if(getClient()==null || request1==null)
                      {
                          return "";
                      }
                      response = getClient().execute(request1);
                  }
                  StringBuilder builder = new StringBuilder();
                  InputStream is=response.getEntity().getContent();
                  BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                  for (String line = null; (line = reader.readLine()) != null;) {
                      builder.append(line).append("\n");
                  }
                  is.close();
                  st_response=builder.toString();
              //}
          } catch (ClientProtocolException e) {
              e.printStackTrace();
          } catch (UnknownHostException e)
          {
              st_response="{\"httperr\":1}";
              e.printStackTrace();
          } 
          catch (IOException e) {
              st_response="{\"httperr\":1}";
              e.printStackTrace();
          }
          return st_response;
      }
  
      protected void onPostExecute(String content) {
          if(o==null)
              ((ResponseListener)listener).onResponseReceived(content);
          else
              ((ResponseWithObjectListener)listener).onResponseReceived(content,o);
       }
  </code></pre>
  
  <p>When I try to load a lot of data I get this error:</p>
  
  <pre><code>03-15 13:50:23.160: E/AndroidRuntime(4980): FATAL EXCEPTION: AsyncTask #4
  03-15 13:50:23.160: E/AndroidRuntime(4980): java.lang.RuntimeException: An error occured while executing doInBackground()
  03-15 13:50:23.160: E/AndroidRuntime(4980):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
  03-15 13:50:23.160: E/AndroidRuntime(4980):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
  03-15 13:50:23.160: E/AndroidRuntime(4980):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
  03-15 13:50:23.160: E/AndroidRuntime(4980):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
  03-15 13:50:23.160: E/AndroidRuntime(4980):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
  03-15 13:50:23.160: E/AndroidRuntime(4980):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
  03-15 13:50:23.160: E/AndroidRuntime(4980):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
  03-15 13:50:23.160: E/AndroidRuntime(4980):     at java.lang.Thread.run(Thread.java:1019)
  03-15 13:50:23.160: E/AndroidRuntime(4980): Caused by: java.lang.OutOfMemoryError
  03-15 13:50:23.160: E/AndroidRuntime(4980):     at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
  03-15 13:50:23.160: E/AndroidRuntime(4980):     at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:153)
  03-15 13:50:23.160: E/AndroidRuntime(4980):     at java.lang.StringBuilder.append(StringBuilder.java:217)
  03-15 13:50:23.160: E/AndroidRuntime(4980):     at com.qriket.qriket.httpclient.HttpAsyncTask.doInBackground(HttpAsyncTask.java:126)
  03-15 13:50:23.160: E/AndroidRuntime(4980):     at com.qriket.qriket.httpclient.HttpAsyncTask.doInBackground(HttpAsyncTask.java:1)
  03-15 13:50:23.160: E/AndroidRuntime(4980):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
  03-15 13:50:23.160: E/AndroidRuntime(4980):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
  </code></pre>
  
  <p>Thanks</p>