Java.lang.OutOfMemoryError:无法分配JNI Env

8

当我第一次运行异步任务时,它能够正常工作。实际上,这个错误是不可预测的。我在这个问题上搜索了很多解决方案,但是没有一个能够解决我的问题。

我得到的普遍解决方案是,我们需要关闭InputStream / ByteArrayInputStream,我已经关闭了所有,但是应用程序仍然崩溃。

以下是堆栈跟踪:

java.lang.OutOfMemoryError:无法分配JNI Env at java.lang.Thread.nativeCreate(Native Method)at java.lang.Thread.start(Thread.java:730)at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:941) at java.util.concurrent.ThreadPoolExecutor.processWorkerExit(ThreadPoolExecutor.java:1009) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1151) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at 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");
    }

}

以下是 HttpUtility 类中处理请求和响应的函数:

以下是 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;
    }

Following is 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;
    }

以下是读取和写入响应函数:
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();
    }

堆栈跟踪在哪里? - Michael
我已经编辑了我的问题。 - priyanka kamthe
每个线程在内存消耗方面都非常昂贵。OOM发生在本地方法中,但是当您的应用程序已耗尽堆空间时,OOM的实际点是无关紧要的。因此,我已经移除了**[tag:jni]**标签。 - Alex Cohn
参见:https://dev59.com/lGAf5IYBdhLWcg3wqUON - Alex Cohn
2个回答

6

每个线程在内存消耗方面都会花费很多成本。

AsyncTask 确实管理了它的线程池,但它并没有针对网络活动进行优化。实际上,如果您有许多 HTTP 请求要发送到同一台服务器,最好在内存消耗和整体性能方面将它们保持在同一线程上,并尽可能重用持久连接。AsyncTask 并未考虑这些问题。

有很多可靠的 HTTP 客户端提供异步请求,例如 OkHttpVolley 或由 J.Smith 提供的 Android 异步 Http 客户端,或者 GitHub 上的其他项目。

发明自己的 HTTP 客户端是可以的,但至少在此领域研究其他人所做过的和犯过的错误是明智之举。


3
作为一名专业人士,研究这个领域中别人所做的和他们所犯的错误是明智的。 - asgs

3

看起来你没有正确关闭输入/输出流或缓冲区,而在从流中读取数据时。请尝试使用以下代码片段从InputStream读取响应。

使用 org.apache.commons.io 的 IOUtils。您可以下载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,请确保您批量发送崩溃和事件。


@user1269737 的链接无法访问。这里是 Web 存档的链接:link - Sahil Doshi
1
警告!永远不要仅仅因为内存不足而请求大堆内存,以便快速解决问题——您应该使用分析器来查找阻止垃圾回收的内存对象。 - StepanM
堆越大,垃圾回收的工作就会变得更长 => 当垃圾回收发生在UI线程上时,处理时间更长 => UI卡顿 - StepanM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接