Hi! Is there a reason why
DefaultDispatcher-worker
threads would keep getting spawned without any limits?
I’ve been debugging an Android app which is supposed to run in the background and periodically execute tasks that could be async (the async code uses coroutines). After running the app for long time it looks like the app gets bloated with
DefaultDispatcher-worker
threads (I can see an increasing number of threads in the Profiler and increasing ids of the threads when I print their names) and eventually crashes with the
OutOfMemory
error (it happened once in a coroutine running on a
DefaultDispatcher-worker-739
thread - yes, 739).
The coroutine handling in the app is quite poor, yes - that’s what I’m trying to fix, amongst others - and we use mostly the IO dispatcher. The only dependency we use that comes with a suspend interface that I’m aware of is Ktor. There’s also a place where we create a
newSingleThreadContext
and run a coroutine on that single thread. The single thread doesn’t seem to be an issue, though, the context gets closed after it’s no longer needed and I don’t see any zombie threads stacking over time coming from that part of the code.
So at this moment it looks like whatever is spawning
DefaultDispatcher-worker
threads is causing the problem. I thought, however, that both
Dispatchers.Default
and
<http://Dispatchers.IO|Dispatchers.IO>
are built on a limited thread pool, so I can’t understand how it could be possible that hundreds of such threads get spawned.