3

我正在制作一个可以将视频从服务器流式传输到 android 手机的 android 应用程序。我的图像流式传输正常且工作正常,但 15 秒后应用程序崩溃。我设法将此追踪到 Throwing OutOfMemoryError。在将位图传递给接口以将其传递给线程进行显示后,我尝试回收位图,但出现错误“无法重用回收的位图”。我不确定如何修复此错误,或者即使倾斜它也会修复它。

                    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//
                        }

                    }

我设法将内存错误跟踪到“byte[] data = new byte[size + 1];”这一行 但经过研究,我得到的印象是由于位图。

有没有其他人遇到过这个问题并设法解决它?对此的任何帮助都会很棒:D

谢谢

日志输出:

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

4

1 回答 1

6

在 Android 5 上有类似的问题。将图像移动到 /drawable-nodpi 而不是 /drawable。

编辑:我认为这与使用当前设备 DPI 自动缩放图像有关。例如,如果您只有 /drawable-mdpi 中的大图像,但您的设备是 xxxhdpi,那么 Android 会获取 mdpi 中可用的图像并尝试将其放大以匹配 xxxhdpi,这可能会占用太多内存。但是来自 /drawable-nodpi 的图像没有缩放,它们按原样使用。

于 2015-04-23T01:11:29.443 回答