13

I am reading the youtube response in Stream and converting it to String. using the below code:

URL: http://gdata.youtube.com/feeds/api/users/cnn/uploads?&v=2&max-results=25&alt=json

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 + "\n");
            }        
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    } 

This code is working in fine in all devices except Android 4.0 devices. I am getting the below logcat:

java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:278)
        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.OutOfMemoryError
        at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
        at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
        at java.lang.StringBuilder.append(StringBuilder.java:216)
        at com.android.mypack.MyApplications.convertStreamToString(MyApplications.java:959)
        at com.android.mypack.MyApplications.access$61(MyApplications.java:951)
        at com.android.mypack.MyApplications$YoutubeTask.doInBackground(MyApplications.java:2303)
        at com.android.mypack.MyApplications$YoutubeTask.doInBackground(MyApplications.java:1)
        at android.os.AsyncTask$2.call(AsyncTask.java:264)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
        ... 5 more
java.lang.OutOfMemoryError
        at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
        at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
        at java.lang.StringBuilder.append(StringBuilder.java:216)
        at com.android.mypack.MyApplications.convertStreamToString(MyApplications.java:959)
        at com.android.mypack.MyApplications.access$61(MyApplications.java:951)
        at com.android.mypack.MyApplications$YoutubeTask.doInBackground(MyApplications.java:2303)
        at com.android.mypack.MyApplications$YoutubeTask.doInBackground(MyApplications.java:1)
        at android.os.AsyncTask$2.call(AsyncTask.java:264)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)

Please help me to overcome this error. Thanks in Advance.

2
  • How many bytes are coming from this stream? Commented Dec 18, 2012 at 12:36
  • @lbalazscs Please see the URL, That is the response i am handling.. I don't know what is the problem with 4.0 devices. Commented Dec 18, 2012 at 14:46

1 Answer 1

2

Well, OutOfMemoryErrors are coming obviously when the virtual machine runs out of memory. The important point here is that the exact stack trace might not be informative, because it could happen that another thread uses all of the memory. Or something memory-consuming happened on this thread before this code, and this code was only the "last straw". This URL certainly does not contain too many bytes, I think the problem is in another place.

You could try a memory analyzer: Android ==> Memory Analysing ==> Eclipse memory analyzer?

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

4 Comments

huh, I was under the impression the VM would throw this exception after attempting an allocation and failing... That would imply the stack trace is actually useful, and, on top of that, it makes perfect sense for enlarging a buffer to cause an oom exception...
Of course, the VM will throw this after attempting an allocation and failing. The question is whether this allocation was only the "last straw" after other allocations or the root cause. This stream contains approximately 110 KBytes of data, it shouldn't be to much.
ahh ok. yeah, all we can know for sure is this was the last straw. it is very possible/likely he has a memory leak elsewhere
how about the String with the length of 276390 ?

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.