我最近有这个异常崩溃的应用程序:
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)
在崩溃报告中我可以看到创建了超过 1000个线程(RefQueueWorker),这解释了OOM。所有线程都在等待,这里是转储:
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)
用于获取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;
}
每个请求在finally块中关闭AndroidHttpClient:
...
finally
{
if ((client instanceof AndroidHttpClient))
{
((AndroidHttpClient) client).close();
}
}
我无法重现此崩溃,它只发生在一个用户(Nexus 5 / API 4.4.2)上。我想知道这个疯狂的线程创建的根本原因是什么?
由于
答案 0 :(得分:0)
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来避免或减少此错误的可能性。