Android 棧內存溢出bug fix小記(pthread_create (1040KB stack) failed: Out of memory)

錯誤日誌:java

java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Out of memory
    at java.lang.Thread.nativeCreate(Native Method)
    at java.lang.Thread.start(Thread.java:753)
    at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:970)
    at java.util.concurrent.ThreadPoolExecutor.ensurePrestart(ThreadPoolExecutor.java:1611)
    at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:342)
    at java.util.concurrent.ScheduledThreadPoolExecutor.schedule(ScheduledThreadPoolExecutor.java:579)
    at java.util.concurrent.ScheduledThreadPoolExecutor.submit(ScheduledThreadPoolExecutor.java:680)
    at io.reactivex.internal.schedulers.NewThreadWorker.a(NewThreadWorker.java:145)
    at io.reactivex.internal.schedulers.IoScheduler$EventLoopWorker.a(IoScheduler.java:231)
    at io.reactivex.Scheduler$Worker.a(Scheduler.java:371)複製代碼
由日誌能夠看出這是由Rxjava2內部,往線程池中添加任務,所報出的棧內存溢出。
本人使用的Rxjava2 (版本是2.2.2);線程調用器使用的Scheduler.io()經過查看
    public static Scheduler io() {
    return RxJavaPlugins.onIoScheduler(IO);
    } 
    -->
    public static Scheduler onIoScheduler(@NonNull Scheduler defaultScheduler) {
    Function<? super Scheduler, ? extends Scheduler> f = onIoHandler;
    if (f == null) {
        return defaultScheduler;
    }
    return apply(f, defaultScheduler);
    }
    -->默認的調度器就是IO:
     IO = RxJavaPlugins.initIoScheduler(new IOTask());
    -->經過IOTask():
    static final class IoHolder {
    static final Scheduler DEFAULT = new IoScheduler();
    }
    -->IoScheduler()內部:
    CachedWorkerPool(long keepAliveTime, TimeUnit unit, ThreadFactory threadFactory) {
        this.keepAliveTime = unit != null ? unit.toNanos(keepAliveTime) : 0L;
        this.expiringWorkerQueue = new ConcurrentLinkedQueue<ThreadWorker>();
        this.allWorkers = new CompositeDisposable();
        this.threadFactory = threadFactory;

        ScheduledExecutorService evictor = null;
        Future<?> task = null;
        if (unit != null) {
            evictor = Executors.newScheduledThreadPool(1, EVICTOR_THREAD_FACTORY);
            task = evictor.scheduleWithFixedDelay(this, this.keepAliveTime, this.keepAliveTime, TimeUnit.NANOSECONDS);
        }
        evictorService = evictor;
        evictorTask = task;
    }
    -->能夠發現是經過Executors.newScheduledThreadPool(1, EVICTOR_THREAD_FACTORY)來構造線程池;
    -->:
    public ScheduledThreadPoolExecutor(int corePoolSize,
                                   ThreadFactory threadFactory) {
    super(corePoolSize, Integer.MAX_VALUE,
          DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
          new DelayedWorkQueue(), threadFactory);
    }
    -->能夠看到最大線程數是無限大的;默認保活時間是10秒。
    -->因此,當你頻繁使用Rxjava2的Scheduler.io()來執行任務,
    可能會出現棧內存溢出的狀況。特別是一些華爲手機。經過Android Profilter觀察CPU使用狀況,就能夠發現大量的線程被建立,
    並且沒有被及時殺死。
複製代碼

解決方案:

自定義Schedulers的線程池,在頻繁使用Rxjava2的時候僅使用單個調用度的實例。 例如:react

if (scheduler==null){
    scheduler = Schedulers.from(Executors.newFixedThreadPool(10));
    }
    observable.subscribeOn(scheduler)
                    .unsubscribeOn(scheduler)
                    .observeOn(AndroidSchedulers.mainThread())
複製代碼
相關文章
相關標籤/搜索