Android HttpClient splits application using java.lang.OutOfMemoryError: pthread_create

I recently excluded this exception from the application:

java.lang.OutOfMemoryError: pthread_create (stack size 16384 bytes) failed: Try again
       at java.lang.VMThread.create(VMThread.java)
       at java.lang.Thread.start(Thread.java:1029)
       at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:920)
       at java.util.concurrent.ThreadPoolExecutor.processWorkerExit(ThreadPoolExecutor.java:988)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
       at java.lang.Thread.run(Thread.java:841)

In the crash report, I see more than 1000 threads (RefQueueWorker), this explains OOM. All threads are just waiting, here is the dump:

RefQueueWorker@org.apache.http.impl.conn.tsccm.ConnPoolByRoute@43b42098
       at java.lang.Object.wait(Object.java)
       at java.lang.Object.wait(Object.java:401)
       at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:102)
       at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:73)
       at org.apache.http.impl.conn.tsccm.RefQueueWorker.run(RefQueueWorker.java:102)
       at java.lang.Thread.run(Thread.java:841)

(...)

RefQueueWorker@org.apache.http.impl.conn.tsccm.ConnPoolByRoute@45f62f08
       at java.lang.Object.wait(Object.java)
       at java.lang.Object.wait(Object.java:401)
       at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:102)
       at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:73)
       at org.apache.http.impl.conn.tsccm.RefQueueWorker.run(RefQueueWorker.java:102)
       at java.lang.Thread.run(Thread.java:841)

Code used to get HttpClient:

  public static HttpClient getHttpClient(Context context)
  {
    HttpClient httpClient = AndroidHttpClient.newInstance("appname", context);
    HttpParams params = httpClient.getParams();
    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpConnectionParams.setConnectionTimeout(params, 5000);
    HttpConnectionParams.setSoTimeout(params, 5000);
    return httpClient;
  }

Each request closes AndroidHttpClient in the finally block:

...
finally
{
  if ((client instanceof AndroidHttpClient))
  {
    ((AndroidHttpClient) client).close();
  }
}

I can not reproduce this failure, it happened for only one user (Nexus 5 / API 4.4.2). I am wondering what could be the main reason for this insane amount of created thread?

thanks

+4
source share
1 answer

java.util.concurrent.ThreadPoolExecutor.threadFactory :

/**
 * Factory for new threads. All threads are created using this
 * factory (via method addWorker).  All callers must be prepared
 * for addWorker to fail, which may reflect a system or user's
 * policy limiting the number of threads.  Even though it is not
 * treated as an error, failure to create threads may result in
 * new tasks being rejected or existing ones remaining stuck in
 * the queue.
 *
 * We go further and preserve pool invariants even in the face of
 * errors such as OutOfMemoryError, that might be thrown while
 * trying to create threads.  Such errors are rather common due to
 * the need to allocate a native stack in Thread.start, and users
 * will want to perform clean pool shutdown to clean up.  There
 * will likely be enough memory available for the cleanup code to
 * complete without encountering yet another OutOfMemoryError.
 */

, threadfactory, .

0

All Articles