objectbox-java icon indicating copy to clipboard operation
objectbox-java copied to clipboard

Use ForkJoinPool and provide builder API to limit threads count.

Open mstyura opened this issue 2 years ago • 3 comments

This PR provide API to configure thread pool limits use by BoxStore.

I was observed situation in Android application where (due to some other issues in app) application is crashed with OOM. At the moment of crash there were tens of alive threads belong to ObjectBox pool. The numbering suggest that thousands of thread were already created and destroyed by the pool at the moment of crash. So it would be nice to have an API to limit number of threads allowed in thread pool and minimum number of threads as well. This of course will not solve original problem with app I was debugging, but will probably help to reveal root problem earlier.

mstyura avatar Aug 12 '22 18:08 mstyura

Thanks! Maybe we should also consider setting the maximum size to 64 (but at least number of processors) by default. (It's the default parallelism number for Kotlin's IO scheduler.)

greenrobot-team avatar Aug 16 '22 10:08 greenrobot-team

Hi! I like the idea of having sane default limit to number of threads in pool. I've tried to implement it. I've set default value to small value like 4 and launched unit-tests. I've started getting RejectedExecutionException, which is not I wanted. RejectedExecutionException had happen due to usage of SynchronousQueue. I've tried to use unbounded LinkedBlockingQueue, but with unbounded queue the only corePoolSize make sense. Basically the meaning of ThreadPoolExecutor with corePoolSize=X and unbounded LinkedBlockingQueue is an executor service which at maximum can run X tasks in parallel. This is what ForkJoinPool does naturally, so I've decided to rather switch to it and only provide configuration to override maximum degree of parallelism. There is one drawback is that ForkJoinPool allow changing keep alive thread timeout only on the very recent API level. I think this is acceptable for now to use default, while having sane limit to number of threads.

mstyura avatar Aug 16 '22 22:08 mstyura

@mstyura Oh sorry, I didn't mean for you to implement this. With "we" I meant the ObjectBox team. But thanks for the investigation!

greenrobot-team avatar Aug 17 '22 06:08 greenrobot-team