Hello, guys. At my work I have a code that launche...
# coroutines
s
Hello, guys. At my work I have a code that launches a lot of coroutines - in fact, so much so that the app runs out of memory, while memory dump shows this (removed domain classes for privacy reasons):
Copy code
+------------+-----------+-------------+--------------------------------------------------------------------+
| Total size | Instances |     Largest | Class name                                                         |
+------------+-----------+-------------+--------------------------------------------------------------------+
|    2.02GiB |  19346752 | 112.00bytes | kotlinx.coroutines.ChildHandleNode                                 |
|    1.54GiB |  18829802 |  88.00bytes | kotlinx.coroutines.NodeList                                        |
|    1.19GiB |   9414895 | 136.00bytes | kotlinx.coroutines.JobSupport$ChildCompletion                      |
|    1.18GiB |  39507640 |  32.00bytes | kotlin.coroutines.CombinedContext                                  |
|    1.09GiB |  14593270 |  80.00bytes | kotlinx.coroutines.StandaloneCoroutine                             |
|  938.18MiB |  10465478 |  94.00bytes | kotlinx.coroutines.internal.DispatchedContinuation                 |
|  742.92MiB |   5125080 | 152.00bytes | ___my_suspend_function__$append$2$1                                |
|  529.53MiB |   4957649 | 112.00bytes | kotlinx.coroutines.ChildContinuation                               |
|  528.99MiB |   4333524 | 128.00bytes | ___my_suspend_function_2__$runLoop$2$3$1$1                         |
|  514.25MiB |   4786174 |   267.04KiB | java.lang.Object[]                                                 |
|  460.03MiB |   5131677 |  94.00bytes | kotlinx.coroutines.CancellableContinuationImpl                     |
|  432.55MiB |   9449215 |  48.00bytes | kotlinx.coroutines.JobSupport$Finishing                            |
|  427.99MiB |   5099730 |  88.00bytes | kotlinx.coroutines.internal.ScopeCoroutine                         |
|  321.36MiB |   4680202 |  72.00bytes | kotlinx.coroutines.internal.ListClosed                             |
|  194.54MiB |   5099727 |  40.00bytes | kotlinx.coroutines.sync.MutexImpl$CancellableContinuationWithOwner |
The code is structured in a way that a Java library (Pcap4j) writes a lot of data in a normal Java queue, and then coroutine-aware code reads this pieces of data, processes them and passes along via channels (with both CPU-bound and IO-bound steps, each step has its own dispatcher with an independent executor). Just from the bird's eye view, are there any obvious better approaches?
l
Perhaps a diagram would have been a bit helpful here. But, from my imagination maybe your dispatcher are asking for more threads than you have available on your machine? (I am talking about CPU work)
s
I'm not really sure. What do you mean about a diagram? I'll check the amount of threads that dispatchers are getting
l
I mean some kind of draw.io chart to explain the flow