gpt4 book ai didi

Android 在 App 上抛出 OutOfMemoryError

转载 作者:太空宇宙 更新时间:2023-11-03 12:32:54 24 4
gpt4 key购买 nike

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

                    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

最佳答案

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

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

关于Android 在 App 上抛出 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28425351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com