内存不足 – 从服务器加载大字符串时出现内存不足错误

从服务器加载大字符串时出现内存不足错误.

这是我的代码:

public String sendRequest(HttpRequest httpRequest){
        Log.d(TAG, "Sending HTTP request to the server");
        Log.d("memory", "memory size is:"+Runtime.getRuntime().maxMemory());
        String response = null;
        HttpHost host = new HttpHost(context.getString(R.string.HOST), -1, context.getString(R.string.SCHEME));
        try {
            HttpResponse httpResponse = httpClient.execute(host, httpRequest);
            int status=httpResponse.getStatusLine().getStatusCode();

            if (status==200) {
                HttpEntity responseEntity = httpResponse.getEntity();
                if (responseEntity != null) {   
                    InputStream stream = responseEntity.getContent();
                    response = convertStreamToString(stream);
                    stream.close();                     
                }
            }
            else{
                response="failed:"+httpResponse.getStatusLine().toString();
            }
        } catch (Exception e) {
            Log.v(TAG,"Request sending failed: ", e);
        }
        Log.d(TAG, "Completed, Sending HTTP request to the server");
        return response;
    }   
    /**
     * Convert stream to string.
     *
     * @param is the input Stream.
     * @return the string
     */
    private String convertStreamToString(InputStream is) {  
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + NEW_LINE);
            }
            reader.close();

        } catch (IOException e) {
            //do nothing.
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                //do nothing.    
            }
        }
        reader=null;
        return sb.toString();
    }

我在这一行得到例外:

while ((line = reader.readLine()) != null) {
    sb.append(line + NEW_LINE);
}

这是我的logcat详细信息:

10-10 15:01:50.270: D/dalvikvm(632): GC_FOR_ALLOC freed <1K, 37% free 24085K/38215K, paused 225ms
10-10 15:01:51.032: I/dalvikvm-heap(632): Grow heap (frag case) to 31.845MB for 8628650-byte allocation
10-10 15:01:51.929: D/dalvikvm(632): GC_CONCURRENT freed <1K, 15% free 32511K/38215K, paused 10ms+32ms
10-10 15:01:52.192: D/dalvikvm(632): GC_FOR_ALLOC freed 0K, 15% free 32511K/38215K, paused 226ms
10-10 15:01:52.192: I/dalvikvm-heap(632): Forcing collection of SoftReferences for 12942970-byte allocation
10-10 15:01:52.589: D/dalvikvm(632): GC_BEFORE_OOM freed 9K, 15% free 32502K/38215K, paused 299ms
10-10 15:01:52.589: E/dalvikvm-heap(632): Out of memory on a 12942970-byte allocation.
10-10 15:01:52.589: D/dalvikvm(632): GC_BEFORE_OOM freed 9K, 15% free 32502K/38215K, paused 299ms
10-10 15:01:52.589: E/dalvikvm-heap(632): Out of memory on a 12942970-byte allocation.
10-10 15:01:53.219: E/AndroidRuntime(632): FATAL EXCEPTION: IntentService[SurveyQuestionService]
10-10 15:01:53.219: E/AndroidRuntime(632): java.lang.OutOfMemoryError
10-10 15:01:53.219: E/AndroidRuntime(632):  at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
10-10 15:01:53.219: E/AndroidRuntime(632):  at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
10-10 15:01:53.219: E/AndroidRuntime(632):  at java.lang.StringBuilder.append(StringBuilder.java:216)
10-10 15:01:53.219: E/AndroidRuntime(632):  at com.handigital.products.agilesurveys.services.SurveyQuestionService.processResponse(SurveyQuestionService.java:34)
10-10 15:01:53.219: E/AndroidRuntime(632):  at com.handigital.products.agilesurveys.services.AbstractIntentService.onHandleIntent(AbstractIntentService.java:44)
10-10 15:01:53.219: E/AndroidRuntime(632):  at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
10-10 15:01:53.219: E/AndroidRuntime(632):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 15:01:53.219: E/AndroidRuntime(632):  at android.os.Looper.loop(Looper.java:137)
10-10 15:01:53.219: E/AndroidRuntime(632):  at android.os.HandlerThread.run(HandlerThread.java:60)

任何人都可以请帮助我.

提前致谢.

最佳答案 解决这个问题的一种方法.在动画中,您可以调用Runtime.getRuntime().gc();

调用垃圾收集器.也可以在activity onDestroy()方法中调用Runtime.getRuntime().gc();

你甚至可以参考下面的链接,

How to resolve out of memory issue

点赞