3

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
user3774031
  • 67
  • 1
  • 8
  • possible duplicate of [Strange out of memory issue while loading an image to a Bitmap object](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – SMR Feb 10 '15 at 06:20
  • i tried looking at that and using the solution and it didn't work in my case – user3774031 Feb 10 '15 at 06:22

1 Answers1

6

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.

Borzh
  • 5,069
  • 2
  • 48
  • 64
  • Fixed the problem for me +1. – Arun Oct 11 '15 at 10:14
  • Thank you.. it is working for me also.. can u explain me how it is working ?? – Anil Chandra Varma Dandu Jan 05 '16 at 09:27
  • If you're right, it means that images in the `drawable/` directory are scaled too, right? But how in this case does Android computes the scale factor (cause there's no density involved there)? – Mickäel A. May 18 '16 at 16:48
  • Yes, `drawable/` is scaled. Quoting from [here](https://developer.android.com/guide/practices/screens_support.html): `the resources in drawable/ are the default drawable resources. The system assumes that default resources are designed for the baseline screen size and density, which is a normal screen size and a medium-density. As such, the system scales default density resources up for high-density screens and down for low-density screens, as appropriate.` – Borzh May 18 '16 at 19:51