Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

284
Vistas
How many coroutines is too many?

I need to speed up a search over some collection with millions of elements. Search predicate needs to be passed as argument.

I have been wondering wether the simplest solution(at least for now) wouldn't be just using coroutines for the task.

The question I am facing right now is how many coroutines can I actually create at once. :D As a side note there might be more than one such search running concurrently.

Can I make millions of coroutines(one for every item) for every such search? Should I decide on some workload per coroutine(for example 1000 items per coroutine)? Should I also decide on some cap for coroutines amount?

I have rough understanding of coroutines and how they actually work, however, I have no idea what are the performance limitations of this feature.

Thanks!

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

The memory weight of a coroutine scales with the depth of the call trace from the coroutine builder block to the suspension point. Each suspend fun call adds another Continuation object to a linked list and this is retained while the coroutine is suspended. A rough figure for one Continuation instance is 100 bytes.

So, if you have a call trace depth of, say, 5, that amounts to 500 bytes per item. A million items is 500 MB.

However, unless your search code involves blocking operations that would leave a thread idle, you aren't gaining anything from coroutines. Your task looks more like an instance of data paralellism and you can solve it very efficiently using the java.util.stream API (as noted by user marstran in the comment).

over 4 years ago · Santiago Trujillo Denunciar

0

According the kotlin coroutine starter guide, the example launches 100K coroutines. I believe what you intend to do is exactly what kotlin coroutine is designed for.

over 4 years ago · Santiago Trujillo Denunciar

0

If you will not do many modifications over your collection then just store it in a HashMap, else store it in a TreeMap. Then just search items there. I believe the search methods implemented there are optimized enough to handle a million items in a blink. I would not use coroutines in this case.

Documentation (for Kotlin):

HashMap: https://developer.android.com/reference/kotlin/java/util/HashMap

TreeMap: https://developer.android.com/reference/kotlin/java/util/TreeMap

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda