Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxIoScheduler thread number keep increasing

Currently using rxjava ver 1.2.4 and rxandroid 1.2.1. I've a question regarding on .subscribeOn(Schedulers.io()) which i read from the javadoc which is:

"The implementation is backed by an Executor thread-pool that will grow as needed."

As see from the log I realize the number of threads keep increasing everytime I do some task:

12-28 11:17:03.879 D/HistoryDatabaseHelper: RxIoScheduler-22
12-28 11:17:03.918 D/HistoryDatabaseHelper: RxIoScheduler-19
12-28 11:17:03.918 D/HistoryDatabaseHelper: RxIoScheduler-18
  1. When the app becomes idle, suppose no any pending task is running. So the next task should use back RxIoScheduler-1, no?
  2. When would all the threads get clean up?
  3. Or it's just the thread name that confuse me where the old active thread like RxIoScheduler-1 is cleared when it's done its job?

I'm worry soon the app will get OOM or somehow hit OOM when performing some heavy task because of the number of opening threads.

like image 723
Bryanleesh Avatar asked Oct 27 '25 08:10

Bryanleesh


1 Answers

The io() Scheduler hosts a pool of single-threaded ScheduledExecutorServices which are handed out as workers when you use observeOn, subscribeOn and other operators that take a Scheduler. Until the associated sequence terminates or gets unsubscribed, that thread backing the worker is alive.

If you have multiple active sequences you get multiple IO Scheduler threads. Once done, they get back into the pool ready to be reused by another sequence. However, there is also a default 1 minute timeout that stops the threads if they don't get reused. If an operator then requires a worker again from the IO Scheduler, a new thread is started which gets a new value in its name, so you may get 19, 2002, etc. if the application runs long enough.

You may get OOM if your sequences don't terminate and just keep piling up, eventually running out of operating system threads.

like image 112
akarnokd Avatar answered Oct 29 '25 21:10

akarnokd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!