当我第一次运行Async任务时,它工作正常。实际上这个错误是不可预测的。我在这个问题上搜索了很多解决方案,但没有什么对我有用。 我得到的常见解决方案是,我们需要关闭InputStream / ByteArrayInputStream&我关闭了所有但是应用程序崩溃了。
堆栈跟踪:
java.lang.OutOfMemoryError:无法分配JNI Env java.lang.Thread.nativeCreate(Native Method)at java.lang.Thread.start(Thread.java:730)at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:941) 在 java.util.concurrent.ThreadPoolExecutor.processWorkerExit(ThreadPoolExecutor.java:1009) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1151) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:607) 在java.lang.Thread.run(Thread.java:761)
以下是AsyncTask;
public class AsyncHttpRequest extends AsyncTask<Void, Void, String> {
private String UrlString = "";
private boolean _showProgressDialog = false;
private CustomCircularLoadingDialog Dialog;
private Context mContext;
AppCompatActivity mActivity;
private IHttpRequestCompletedListener listener;
private boolean isActivity = true;
private String _messageText = "Please wait...";
private String type = "get";
private HttpUtility utility;
public AsyncHttpRequest(String urlString, Context context, IHttpRequestCompletedListener listener, boolean _showProgressDialog) {
UrlString = urlString;
this._showProgressDialog = _showProgressDialog;
this.mContext = context;
this.listener = listener;
Dialog = new CustomCircularLoadingDialog(this.mContext);
this.utility = new HttpUtility(this.UrlString, mContext);
Utilities.setCurrentHitURL(mContext, UrlString);
}
public void setOnCompletedListener(IHttpRequestCompletedListener listener) {
this.listener = listener;
}
public void setWaitMessage(String msgText) {
if (!msgText.equals(""))
msgText = "\n" + msgText;
this._messageText = _messageText + msgText;
}
public void addPostItem(NameValuePair nameValuePair) {
this.utility.addNameValuePairs(nameValuePair);
}
public void addGetHeader(String headerData) {
this.utility.addGetHeader(headerData);
}
public void addPostHeader(String headerData) {
this.utility.addPostHeader(headerData);
}
public void setTypePost() {
this.type = "post";
}
@Override
protected void onPreExecute() {
if (_showProgressDialog) {
Dialog.setMessage(this._messageText);
Dialog.setCancelable(false);
Dialog.setCanceledOnTouchOutside(false);
this.Dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Dialog.show();
}
}
@Override
protected String doInBackground(Void... params) {
if (!Utilities.isNetworkAvailable(mContext))
return "No network available";
try {
if (this.type.equals("get"))
return utility.doGetRequest();
else
return utility.doPostRequest();
} catch (MediCorporateException tex) {
if (listener != null) {
if (isActivity) {
((Activity) mContext).runOnUiThread(new Runnable() {
@Override
public void run() {
listener.OnHttpRequestError();
}
});
} else {
((GcmIntentService) mContext).runOnUIThread(new Runnable() {
@Override
public void run() {
listener.OnHttpRequestError();
}
});
}
}
Utilities.callCrashReport(mContext, tex);
}
Log.i("Exit ", "doInBackground");
return "";
}
@Override
protected void onPostExecute(String Result) {
if (_showProgressDialog)
this.Dialog.dismiss();
Log.i("Came in", "onPostExecute");
if (this.listener != null) {
if (!Utilities.isNullOrEmpty(Result))
listener.OnHttpRequestCompleted(Result);
else {
logoutUser();
listener.OnHttpRequestError();
}
}
Log.i("Exit ", "onPostExecute");
}
}
以下是处理请求的功能&amp; HttpUtility类中的响应;
public String doGetRequest() throws MediCorporateException {
String resp = "";
int responseCode = 0;
try {
if (header != null) {
if (header.length() > 0) {
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("Authorization", header);
}
}
responseCode = httpURLConnection.getResponseCode();
InputStream inputStream = new BufferedInputStream(this.httpURLConnection.getInputStream());
resp = readResponse(inputStream);
Log.v("Resp", "" + responseCode + " --- " + resp);
inputStream.close();
} catch (IOException ioExc) {
FileLog.e(getClass().getName(), ioExc);
resp = ioExc.getMessage();
throw new MediCorporateException("Http IO Exception...");
} catch (Exception ex) {
FileLog.e(getClass().getName(), ex);
throw new MediCorporateException("Http Error...");
} finally {
this.httpURLConnection.disconnect();
if (responseCode == 401)
return "" + responseCode;
if (responseCode != 200)
return null;
}
return resp;
}
以下是DoPostRequest():
public String doGetRequest() throws MediCorporateException {
String resp = "";
int responseCode = 0;
try {
if (header != null) {
if (header.length() > 0) {
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("Authorization", header);
}
}
responseCode = httpURLConnection.getResponseCode();
InputStream inputStream = new BufferedInputStream(this.httpURLConnection.getInputStream());
resp = readResponse(inputStream);
Log.v("Resp", "" + responseCode + " --- " + resp);
inputStream.close();
} catch (IOException ioExc) {
FileLog.e(getClass().getName(), ioExc);
resp = ioExc.getMessage();
throw new MediCorporateException("Http IO Exception...");
} catch (Exception ex) {
FileLog.e(getClass().getName(), ex);
throw new MediCorporateException("Http Error...");
} finally {
this.httpURLConnection.disconnect();
if (responseCode == 401)
return "" + responseCode;
if (responseCode != 200)
return null;
}
return resp;
}
以下是阅读&amp;写响应函数;
private void writePostMethod(OutputStream outputStream) throws Exception {
if (this.nameValuePairs.size() <= 0)
throw new Exception("Cannot use post method with no values to post");
String postStr = "";
for (NameValuePair item : this.nameValuePairs)
postStr += URLEncoder.encode(item.getName(), "UTF-8") + "=" + URLEncoder.encode(item.getValue(), "UTF-8") + "&";
postStr = postStr.substring(0, postStr.length() - 1);
Log.v("Post Values", postStr);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(postStr);
writer.flush();
writer.close();
}
private String readResponse(InputStream inputStream) throws IOException {
int i;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
while ((i = inputStream.read()) != -1)
outputStream.write(i);
byte[] responseBytes = outputStream.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(responseBytes);
InputStreamReader reader;
if (!this._urlString.contains("token")) {
GZIPInputStream gzis = new GZIPInputStream(bais);
reader = new InputStreamReader(gzis);
gzis.close();
} else
reader = new InputStreamReader(bais);
BufferedReader in = new BufferedReader(reader);
StringBuilder total = new StringBuilder();
String readed;
while ((readed = in.readLine()) != null) {
total.append(readed);
bais.close();
}
in.close();
reader.close();
inputStream.close();
outputStream.close();
return total.toString();
}
答案 0 :(得分:3)
在您从流中读取数据时,似乎没有正确关闭输入/输出流或缓冲区。请尝试使用以下代码片段来读取InputStream的响应。
使用org.apache.commons.io的IOUtils。你可以download jar
private String readResponse(InputStream inputStream) throws IOException {
if (!this._urlString.contains("token")) {
GZIPInputStream gzipIn = new GZIPInputStream(inputStream);
return IOUtils.toString(gzipIn);
}else {
if (inputStream != null) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
return null;
}
虽然大多数OOM都是由于分配给应用程序的堆大小造成的。我们可以通过在清单文件中添加标记来增加堆大小。
<application
android:name="ApplicationLoader"
android:largeHeap="true">
在完成您的回购后,我发现每次例外您都会向FireBase发送日志和崩溃报告。这导致线程池中的内存不足导致OOM。请确保您批量发送崩溃和事件。
答案 1 :(得分:2)
每个线程在内存消耗方面花费很多。
AsyncTask管理其线程池,但未针对网络活动进行优化。实际上,如果您对同一服务器有很多HTTP请求,那么在内存消耗和整体性能方面最好将它们保持在同一个线程上,并尽可能重用持久连接。 AsyncTask不考虑这些问题。
有许多可靠的HTTP客户端提供异步请求,例如J.Smith的OkHttp或volley或Android Asynchronous Http Client,或GitHub上的许多其他项目。
创建自己的HTTP客户端是可以的,但至少研究其他人在这个领域所做的事情以及他们之前犯的错误是明智的。