io.reactivex.exceptions.UndeliverableException: java.lang.OutOfMemoryError: Failed to allocate a 528 byte allocation with 1775608 free bytes and 1733KB until OOM, target footprint 268435456, growth limit 268435456; giving up on allocation because <1% of heap free after GC.
at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)
at io.reactivex.internal.schedulers.ScheduledDirectPeriodicTask.run(ScheduledDirectPeriodicTask.java:43)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:307)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 528 byte allocation with 1775608 free bytes and 1733KB until OOM, target footprint 268435456, growth limit 268435456; giving up on allocation because <1% of heap free after GC.
at java.util.concurrent.atomic.AtomicReferenceArray.<init>(AtomicReferenceArray.java:93)
at io.reactivex.internal.queue.SpscLinkedArrayQueue.resize(SpscLinkedArrayQueue.java:103)
at io.reactivex.internal.queue.SpscLinkedArrayQueue.offer(SpscLinkedArrayQueue.java:88)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.onNext(ObservableObserveOn.java:117)
at io.reactivex.internal.operators.observable.ObservableInterval$IntervalObserver.run(ObservableInterval.java:83)
at io.reactivex.internal.schedulers.ScheduledDirectPeriodicTask.run(ScheduledDirectPeriodicTask.java:38)
... 6 more
最近在老项目里面发现一个Rxjava的坑,如上所示异常;
场景:用Rxjava 中的Schedulers类实现一个定时器,偶尔会造成页面卡主,再操作页面会提示ANR(应用无响应);
代码如下:
Observable.interval(0, 1, TimeUnit.MICROSECONDS)
.observeOn(Schedulers.io())
.subscribe(new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
// disposable = d;
}
@Override
public void onNext(Long aLong) {
Log.d("Alex", Thread.currentThread().toString() + "====" + Thread.currentThread().hashCode());
}
@Override
public void onError(Throwable e) {
Log.d("Alex", e.toString());
}
@Override
public void onComplete() {
Log.d("Alex", " ---- onComplete ---- ");
}
});
查看Observable.interval(),如下:
Scheduler:
interval operates by default on the computation Scheduler.
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.COMPUTATION)
public static Observable<Long> interval(long initialDelay, long period, TimeUnit unit) {
return interval(initialDelay, period, unit, Schedulers.computation());
}
方法描述,说时间调度依赖Schedulers的computaion(),再看这个方法,如下:
Returns a default, shared Scheduler instance intended for computational work.
This can be used for event-loops, processing callbacks and other computational work.
It is not recommended to perform blocking, IO-bound work on this scheduler. Use io() instead.
The default instance has a backing pool of single-threaded {@link ScheduledExecutorService} instances equal to the number of available processors ({@link java.lang.Runtime#availableProcessors()}) to the Java VM.
This type of scheduler is less sensitive to leaking {@link io.reactivex.Scheduler.Worker} instances, although not disposing a worker that has timed/delayed tasks not cancelled by other means may leak resources and/or
execute those tasks "unexpectedly".
@NonNull
public static Scheduler computation() {
return RxJavaPlugins.onComputationScheduler(COMPUTATION);
}
此方法描述,说不推荐使用此方法处理IO绑定的和阻塞相关的任务,就是异步任务,且只会产生一个线程来处理任务,还可能产生内存泄漏;
结论:内存泄漏,最终会导致内存溢出,最后APP会crash或者ANR,尝试过使用开源框架LeakCanary和AS自带分析工具Profiler,都无济于事,LeakCanary根本检查不到泄漏,Profiler直接因为需要分析的Heap文件太大而崩溃了;
不建议使用
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.COMPUTATION)
public static Observable<Long> interval(long initialDelay, long period, TimeUnit unit) {
return interval(initialDelay, period, unit, Schedulers.computation());
}
或者使用
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.CUSTOM)
public static Observable<Long> interval(long initialDelay, long period, TimeUnit unit, Scheduler scheduler) {
ObjectHelper.requireNonNull(unit, "unit is null");
ObjectHelper.requireNonNull(scheduler, "scheduler is null");
return RxJavaPlugins.onAssembly(new ObservableInterval(Math.max(0L, initialDelay), Math.max(0L, period), unit, scheduler));
}
一句话:就是不使用Schedulers的computaion(),而是Schedulers的io(),
做定时器,定时器的方法还有很多,比如ScheduledExecutorService、AlarmManager、CountDownTimer等。
好了,又可以愉快玩耍了。