0

I need to make bunch of http requests(around 500) from Async Task. For that purpose i am using HttpClient. After around 200 requests i get this on LogCat:

I/dalvikvm-heap(25912): Grow heap (frag case) to 9.321MB for 526096-byte allocation D/dalvikvm(25912): GC_CONCURRENT freed 3K, 1% free 9516K/9612K, paused 3ms+1ms, total 24ms

When this occurs, async task is paused(code stops being executed). I assumed that i am using too much memory, so i`ve made those static class variables so they are allocated only once:

static HttpClient httpClient = new DefaultHttpClient();
static HttpContext localContext = new BasicHttpContext();
static HttpGet httpGet = new HttpGet();

Everything else is being set to null after usage. Also i`ve tried setting large heap requirement in Manifest "android:largeHeap="true"", but stil same problem occurs.

I cant figure out what am doing wrong. I know that requirement for 500 http request is very demanding, but there must be solution. Any ideas?

Ivan Alek
  • 1,899
  • 3
  • 19
  • 38

2 Answers2

1

There isn't really any solution, since it's not really a problem. It's simply the Garbage Collector doing its job, and unless 1 progress spawns hundreds of these, I wouldn't worry too much.

Also, it is normal that this stops executing your logic for a split second. It's how the GC work. It 'stops the world' for a little while.

What you are doing wrong though, is doing HTTP requests in AsyncTask. This is not what AsyncTask is made, or at least not suitable for. It's open to result in cancelled and/or lost requests.

You want this running in a Service, that doesn't die off as soon as the Activity that started it dies.

You could take a look at RoboSpice, which is a good library to help you with this.

Stefan de Bruijn
  • 6,289
  • 2
  • 23
  • 31
  • I am trying to implement service instead of AsyncTask, but there is problem because service runs on main thread. I`ve also tried with IntentService but still the same problem "Network on main thread exception". Any ideas? – Ivan Alek Jun 12 '13 at 12:18
1

control number of spawned threads by using executor with fixed thread pool

Executor executor = new Executors.newFixedThreadPool(NUMBER_OF_THREADS);

and then in your loop

myAnotherAsyncTask.executeOnExecutor(executor,param0,param1)
robotoaster
  • 3,082
  • 1
  • 24
  • 23
  • I am using only one Async task and http requests are fired up synchronously one by one inside the Async task. Can you please give me short explanation why do you think that this will be solution? – Ivan Alek Jun 12 '13 at 12:31
  • I assumed you are trying to run 500 AsyncTasks with requests. Anyway every time you invoke httpClient.execute() it creates new HttpResponse which has to be consumed before connection can be reused. See http://stackoverflow.com/questions/4775618/httpclient-4-0-1-how-to-release-connection. The way I would do it is make AsyncTask send one connection at a time and make executor take care that not too many concurrent tasks are running. – robotoaster Jun 12 '13 at 15:07