Android 栈内存溢出bug fix小记(pthread_create (1040KB stack) failed: Out of memory)

错误日志:

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 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();
        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的时候仅使用单个调用度的实例。 例如:

    if (scheduler==null){
    scheduler = Schedulers.from(Executors.newFixedThreadPool(10));
    }
    observable.subscribeOn(scheduler)
                    .unsubscribeOn(scheduler)
                    .observeOn(AndroidSchedulers.mainThread())
复制代码

你可能感兴趣的:(Android 栈内存溢出bug fix小记(pthread_create (1040KB stack) failed: Out of memory))