Curzibn / Luban

Luban(鲁班)—Image compression with efficiency very close to WeChat Moments/可能是最接近微信朋友圈的图片压缩算法
Apache License 2.0
13.59k stars 2.25k forks source link

OOM问题,比较难复现,但确实存在 #184

Open Yanqilong opened 7 years ago

Yanqilong commented 7 years ago

调用Luban.with(getApplicationContext()).load(file).get(),出现OOM,请大大查看原因。

08-21 15:37:00.767 29433-8961/com.newseax.tutor E/AndroidRuntime: FATAL EXCEPTION: RxCachedThreadScheduler-4
                                                                  Process: com.newseax.tutor, PID: 29433
                                                                  java.lang.OutOfMemoryError: Failed to allocate a 12979212 byte allocation with 9991920 free bytes and 9MB until OOM
                                                                      at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                      at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
                                                                      at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:639)
                                                                      at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:615)
                                                                      at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391)
                                                                      at top.zibin.luban.Engine.compress(Engine.java:100)
                                                                      at top.zibin.luban.Luban.get(Luban.java:119)
                                                                      at top.zibin.luban.Luban.access$700(Luban.java:15)
                                                                      at top.zibin.luban.Luban$Builder.get(Luban.java:171)
                                                                      at com.newseax.tutor.service.DynamicPublishService$2.apply(DynamicPublishService.java:179)
                                                                      at com.newseax.tutor.service.DynamicPublishService$2.apply(DynamicPublishService.java:176)
                                                                      at io.reactivex.internal.operators.flowable.FlowableMap$MapSubscriber.onNext(FlowableMap.java:62)
                                                                      at io.reactivex.internal.operators.flowable.FlowableFromIterable$IteratorSubscription.fastPath(FlowableFromIterable.java:179)
                                                                      at io.reactivex.internal.operators.flowable.FlowableFromIterable$BaseRangeSubscription.request(FlowableFromIterable.java:121)
                                                                      at io.reactivex.internal.subscribers.BasicFuseableSubscriber.request(BasicFuseableSubscriber.java:152)
                                                                      at io.reactivex.internal.operators.flowable.FlowableToListSingle$ToListSubscriber.onSubscribe(FlowableToListSingle.java:84)
                                                                      at io.reactivex.internal.subscribers.BasicFuseableSubscriber.onSubscribe(BasicFuseableSubscriber.java:66)
                                                                      at io.reactivex.internal.operators.flowable.FlowableFromIterable.subscribe(FlowableFromIterable.java:68)
                                                                      at io.reactivex.internal.operators.flowable.FlowableFromIterable.subscribeActual(FlowableFromIterable.java:46)
                                                                      at io.reactivex.Flowable.subscribe(Flowable.java:12885)
                                                                      at io.reactivex.internal.operators.flowable.FlowableMap.subscribeActual(FlowableMap.java:36)
                                                                      at io.reactivex.Flowable.subscribe(Flowable.java:12885)
                                                                      at io.reactivex.internal.operators.flowable.FlowableToListSingle.subscribeActual(FlowableToListSingle.java:57)
                                                                      at io.reactivex.Single.subscribe(Single.java:2657)
                                                                      at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
                                                                      at io.reactivex.Scheduler$1.run(Scheduler.java:134)
                                                                      at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:59)
                                                                      at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:51)
                                                                      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:154)
                                                                      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
                                                                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                      at java.lang.Thread.run(Thread.java:818)
LABELNET commented 7 years ago

连续进行4张图片压缩就OOM了,怎么解决?

Yanqilong commented 6 years ago

@LABELNET 好久没开 issues 了 你这个问题我也碰到过,解决办法: 结合 RX 操作符执行在i/o线程 + LuBan 同步压缩来解决你这个问题 代码:

        final List<File> rawFileList = new ArrayList<>();
        Flowable.fromIterable(rawFileList)
                .map(new Function<File, File>() {
                    @Override
                    public File apply(File file) throws Exception {
                        return top.zibin.luban.Luban.with(getApplicationContext()).load(file).get().get(0);
                    }
                })
                .toList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<File>>() {

                    @Override
                    public void accept(List<File> fileList) throws Exception {
                     //得到压缩后的图片

                });