I am making a android application that can stream a video from a server to a android mobile. i have the image streaming properly and working but after 15 seconds the application crashes. i managed to track this to a Throwing OutOfMemoryError. I tried to recycle the bitmap after i pass it a interface to take it to the thread to display it but i get the error "unable to reuse recycled Bitmap". i am not sure how to fix this error or even if reclining it will fix it.
int read_count = 1;
long start_time = System.currentTimeMillis();
long timeout = 10000;
boolean timed_out = false;
byte[] data = new byte[size + 1];
while (read_count < size && !timed_out)
{
int len = in.read(data, read_count, size - read_count);
read_count += len;
timed_out = (System.currentTimeMillis() - start_time) >= timeout;
}
data[0] = (byte)0x89;
if (read_count == size)
{
final boolean is_left = (side == 0);
final byte[] tmp = data;
Bitmap Image_data = null;
System.out.println(tmp.length);
if (Listener != null)
{
Image_data = BitmapFactory.decodeByteArray(tmp, 0, tmp.length);
Listener.OnNewImageListenerBitmap(Image_data, is_left);
// this is where i tried recycling it//
}
}
i managed to track the memory error to the line "byte[] data = new byte[size + 1];" but after research i get the impression that is due to the bitmap.
has anyone else had issue with this problem and managed to fix it? any help on this would be awesome :D
Thanks
LOGCATOUTPUT:
02-10 15:22:15.488 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/System.out﹕ 373348
02-10 15:22:16.655 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/System.out﹕ 373348
02-10 15:22:17.371 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/System.out﹕ 373348
02-10 15:22:18.827 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/System.out﹕ 373348
02-10 15:22:29.167 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/art﹕ Alloc sticky concurrent mark sweep GC freed 37627(1380KB) AllocSpace objects, 0(0B) LOS objects, 6% free, 114MB/122MB, paused 905us total 7.018ms
02-10 15:22:29.178 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/art﹕ Alloc partial concurrent mark sweep GC freed 110(3KB) AllocSpace objects, 2(92MB) LOS objects, 40% free, 22MB/36MB, paused 1.112ms total 11.404ms
02-10 15:22:29.202 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/art﹕ Alloc concurrent mark sweep GC freed 141(17KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 22MB/36MB, paused 825us total 23.893ms
02-10 15:22:29.202 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/art﹕ Forcing collection of SoftReferences for 1GB allocation
02-10 15:22:29.223 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/art﹕ Alloc concurrent mark sweep GC freed 67(2520B) AllocSpace objects, 0(0B) LOS objects, 39% free, 22MB/36MB, paused 2.473ms total 18.440ms
02-10 15:22:29.223 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 1801149826 byte allocation with 15419532 free bytes and 233MB until OOM"
02-10 15:22:29.223 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-1308
Process: com.google.vrtoolkit.cardboard.samples.treasurehunt, PID: 32097
java.lang.OutOfMemoryError: Failed to allocate a 1801149826 byte allocation with 15419532 free bytes and 233MB until OOM
at Socket.ClientThread.run(ClientThread.java:130)
02-10 15:22:29.833 32097-32121/com.google.vrtoolkit.cardboard.samples.treasurehunt I/MainActivity﹕ onRendererShutdown
02-10 15:22:32.793 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/Process﹕ Sending signal. PID: 32097 SIG: 9
Had similar issue on Android 5. Move your images to /drawable-nodpi instead of /drawable.
EDIT: I think it has something to do with automatic scaling of image using current device DPI. For example if you have big image only in /drawable-mdpi, but your device is xxxhdpi, then Android takes the image available in mdpi and tries to scale it up to match xxxhdpi, which can occupy too much memory. But images from /drawable-nodpi are not scaled, they are used as they are.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With