4

Getting Out of memory error while loading large string from the server.

This is my code:

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();
    }

I am getting exception at this line:

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

This is my logcat details:

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)

Any one can please help me on this.

Thanks in advance.

1 Answer 1

2

One way to solve this problem. In animation you can call Runtime.getRuntime().gc(); to invoke garbage collector. also in activity onDestroy() method u can call Runtime.getRuntime().gc();

You can even refer the link below,

How to resolve out of memory issue

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.