首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android从服务器加载大字符串时出现内存不足错误

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

Stack Overflow用户
提问于 2012-10-10 17:59:14
回答 1查看 4.1K关注 0票数 4

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

这是我的代码:

代码语言:javascript
运行
复制
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();
    }

我在这一行得到了异常:

代码语言:javascript
运行
复制
while ((line = reader.readLine()) != null) {
    sb.append(line + NEW_LINE);
}

这是我的logcat详细信息:

代码语言:javascript
运行
复制
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)

在这一点上,任何人都可以帮助我。

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-29 19:23:13

解决这个问题的一种方法。在动画中,您可以调用Runtime.getRuntime().gc();来调用垃圾收集器。同样,在活动onDestroy()方法中,u可以调用Runtime.getRuntime().gc();

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

How to resolve out of memory issue

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12816648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档