您好,非常感谢您上次回答我的疑问,看了你的demo后我实现了加载图片,速度也还可以,但是在我虚拟机上出现了oom,具体错误如下:
Alloc concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 99MB/99MB, paused 265us total 10.030ms
I/art: WaitForGcToComplete blocked for 255.761ms for cause Alloc
I/art: Starting a blocking GC Alloc
I/art: Waiting for a blocking GC Alloc
I/art: Waiting for a blocking GC Alloc
I/art: Waiting for a blocking GC Alloc
I/art: Waiting for a blocking GC Alloc
I/art: Waiting for a blocking GC Alloc
W/art: Throwing OutOfMemoryError "Failed to allocate a 28 byte allocation with 0 free bytes and 3GB until OOM" (recursive case)
I/art: Waiting for a blocking GC Alloc
W/art: "tiny-compress-thread-4" prio=5 tid=27 Runnable
W/art: | group="main" sCount=0 dsCount=0 obj=0x22c0f820 self=0xded82a00
W/art: | sysTid=8819 nice=0 cgrp=default sched=0/0 handle=0xdc1fa930
W/art: | state=R schedstat=( 2617287020 851805915 3970 ) utm=93 stm=168 core=1 HZ=100
W/art: | stack=0xdc0f8000-0xdc0fa000 stackSize=1038KB
W/art: | held mutexes= "mutator lock"(shared held)
W/art: native: #00 pc 0058b0e2 /system/lib/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits >&, int, char const*, art::ArtMethod*, void*)+226)
W/art: native: #01 pc 00551c3e /system/lib/libart.so (art::Thread::ThrowOutOfMemoryError(char const*)+542)
W/art: native: #02 pc 0029b73d /system/lib/libart.so (art::gc::Heap::ThrowOutOfMemoryError(art::Thread*, unsigned int, art::gc::AllocatorType)+1559)
W/art: native: #03 pc 002a4ad2 /system/lib/libart.so (art::gc::Heap::AllocateInternalWithGc(art::Thread*, art::gc::AllocatorType, unsigned int, unsigned int*, unsigned int*, unsigned int*, art::mirror::Class**)+5218)
W/art: native: #04 pc 001a185d /system/lib/libart.so (art::mirror::Class::AllocObject(art::Thread*)+1677)
W/art: native: #05 pc 0054db66 /system/lib/libart.so (art::Thread::ThrowNewWrappedException(char const*, char const*)+774)
W/art: native: #06 pc 00551cfa /system/lib/libart.so (art::Thread::ThrowOutOfMemoryError(char const*)+730)
W/art: native: #07 pc 0029b73d /system/lib/libart.so (art::gc::Heap::ThrowOutOfMemoryError(art::Thread*, unsigned int, art::gc::AllocatorType)+1559)
W/art: native: #08 pc 002a4ad2 /system/lib/libart.so (art::gc::Heap::AllocateInternalWithGc(art::Thread*, art::gc::AllocatorType, unsigned int, unsigned int*, unsigned int*, unsigned int*, art::mirror::Class**)+5218)
W/art: native: #09 pc 00445884 /system/lib/libart.so (art::mirror::String::AllocFromModifiedUtf8(art::Thread*, int, char const*)+2420)
W/art: native: #10 pc 0044635d /system/lib/libart.so (art::mirror::String::AllocFromModifiedUtf8(art::Thread*, char const*)+47)
W/art: native: #11 pc 00408aec /system/lib/libart.so (art::JNI::NewStringUTF(_JNIEnv*, char const*)+540)
W/art: native: #12 pc 0017c545 /system/lib/libart.so (art::CheckJNI::NewStringUTF(_JNIEnv*, char const*)+632)
W/art: native: #13 pc 000dd23e /system/lib/libandroid_runtime.so (getMimeTypeString(_JNIEnv*, SkImageDecoder::Format)+94)
W/art: native: #14 pc 000ddedd /system/lib/libandroid_runtime.so (???)
W/art: native: #15 pc 000de8b5 /system/lib/libandroid_runtime.so (???)
W/art: native: #16 pc 00d29a32 /data/dalvik-cache/x86/system@[email protected] (Java_android_graphics_BitmapFactory_nativeDecodeByteArray___3BIILandroid_graphics_BitmapFactory_00024Options_2+182)
W/art: native: #17 pc 0184c747 /dev/ashmem/dalvik-main space 1 (deleted) (???)
W/art: at android.graphics.BitmapFactory.nativeDecodeByteArray(Native method)
W/art: at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:522)
W/art: at com.zxy.tiny.core.BitmapCompressor.sampleCompress(BitmapCompressor.java:201)
W/art: at com.zxy.tiny.core.BitmapCompressor.compress(BitmapCompressor.java:84)
W/art: at com.zxy.tiny.callable.BitmapCompressCallableTasks$UriAsBitmapCallable$1.callback(BitmapCompressCallableTasks.java:102)
W/art: at com.zxy.tiny.core.HttpUrlConnectionFetcher.fetch(HttpUrlConnectionFetcher.java:82)
W/art: at com.zxy.tiny.callable.BitmapCompressCallableTasks$UriAsBitmapCallable.call(BitmapCompressCallableTasks.java:99)
W/art: at com.zxy.tiny.callable.BitmapCompressCallableTasks$UriAsBitmapCallable.call(BitmapCompressCallableTasks.java:87)
W/art: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/art: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/art: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/art: at com.zxy.tiny.core.CompressThreadFactory$1.run(CompressThreadFactory.java:28)
W/art: at java.lang.Thread.run(Thread.java:818)
D/skia: --- OOM in getMimeTypeString()
I/art: Waiting for a blocking GC Alloc
I/art: Clamp target GC heap from 115MB to 99MB
I/art: Alloc partial concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 99MB/99MB, paused 618us total 16.222ms
I/art: WaitForGcToComplete blocked for 48.670ms for cause Alloc
I/art: Starting a blocking GC Alloc
I/art: Waiting for a blocking GC Alloc
不是跑demo出现的,是我自己的demo上的,但是写法是按照你的demo上写的
Demo中为了比较原图与压缩后的效果,所以直接加载了原图,你是否直接加载了原图,如果是那将可能会oom
@cjcj125125 可以把加载代码与图片大小贴出来一下
Tiny.BitmapCompressOptions options = new Tiny.BitmapCompressOptions();
Tiny.getInstance().source(Uri.parse(url)).asBitmap().withOptions(options).compress(new BitmapCallback() {
@Override
public void callback(boolean isSuccess, Bitmap bitmap) {
if(isSuccess)
{
imageView.setImageBitmap(bitmap);
}
}
});
我按照你的demo上写的,其中url是图片地址,这样有问题吗???
@cjcj125125 没问题,可以把图片url地址发一下吗?
@cjcj125125 压缩图片本身的含义是保证图片不会过于大(宽高或者文件大小),tiny在加载大图时压缩在一个良好的分辨率的范围(类似微信朋友圈保持在最大为1280x720),对于你如果需要小图你可以设置Tiny.BitmapCompressOptions 参数中的 width与height。另你也可以打印出加载后bitmap宽高,自己计算下耗费内存大小,是否是你的应用本身存在大量内存没释放而导致oom的?
@Sunzxyong 你好,我的代码是
for (int i = 0; i <20 ; i++) {
file[i]=new File("/storage/emulated/0/DCIM/Camera/22222222222.jpg");
}
Tiny.getInstance().source(file).batchAsFile().withOptions(options).batchCompress(new FileWithBitmapBatchCallback()
超过20张图片数据,这个图片大小才有90kb左右,就OOM了
@lilealyh 你这个图片文件大小是90KB嘛?
如果是:文件大小和所占内存是没关联的,具体看图片宽高以及所占位数,批量压缩20张,意味着将会有20张图的,每张图算1280x720,位数4,大约3.5M,3.5x20大约70M内存,压缩过程中将还有中间的tmp图,当然还占内存,不过这个可随gc而回收。所以这样一下申请70+M内存,任何情况下将大几率触发OOM。
如果不是,而是你计算的图片所占内存大小90KB,那么无论如何也不会导致OOM。