Siva K
Siva K

Reputation: 4968

Out of memory on a 10171204-byte allocation in android app

in my app i am trying to download some video in button click. i am showing a progress bar in the notification area, i have already asked a question regarding this here (it is under bounty)

Now to solve my problem i changed the code to be as follows, when the first button is clicked it starts an progress bar in notification by a class UploadService extends IntentService and the progress bar is shown

When the second button is clicked it tries to start another progress bar in notification by a class UploadService2 extends IntentService, but immediately the app gets crashed and it shows the following error as follows

E/dalvikvm-heap( 2325): Out of memory on a 10171204-byte allocation.
E/AndroidRuntime( 2325): Uncaught handler: thread IntentService[UploadService2] exiting due to uncaught exception
E/AndroidRuntime( 2325): java.lang.OutOfMemoryError
E/AndroidRuntime( 2325):        at uk.me.hardill.android.notification.UploadService2$1.run(UploadService2.java:93)
E/AndroidRuntime( 2325):        at uk.me.hardill.android.notification.UploadService2.onHandleIntent(UploadService2.java:134)
E/AndroidRuntime( 2325):        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:30)
E/AndroidRuntime( 2325):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2325):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2325):        at android.os.HandlerThread.run(HandlerThread.java:60)

My UploadServices class is here

Upvotes: 1

Views: 2304

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500145

Given that you're only using the buffer in this bit of code:

byte[] buffer = new byte[10171188];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) 
{
    fos.write(buffer, 0, len1);
}

the answer seems pretty simple: use a smaller buffer. Even on a desktop I'd normally create a buffer of about 32K - creating a 10M buffer on a mobile device is a really bad idea IMO.

Even if you could fill that buffer, there's no need to have one that big - and as you're actually fetching a network resource, it's incredibly unlikely that even 1% of the buffer is ever going to be populated in a single read.

Upvotes: 7

Related Questions