When Android is Universe, then Coroutine is SpaceShip (Coroutine Vs RxJava)

Atul Sakhala
3 min readMar 14, 2021

Data-driven analysis
So, I’m writing my first blog so you to ignore the mistakes and capture meaningful words from the sentences.
In the Android world, for the past couple of years, I’m very curious and observing the little things around the low-level architecture and trying to understand why Google guys are keen to migrating the developers from JAVA to Kotlin especially in android. I know Kotlin internally using JAVA but it’s not an easy statement. In the Mobile world, everything revolves around limited space(Memory) and time(Optimization). So In this topic, we will try to deduce how time (let’s say Cosmos-ship) works in android.

The Mandalorian with his cosmos starship

In today’s world, Space ships using Ion thruster to float in the vacuum. Similarly, Android uses “Threads” to process any task on the CPU. So the question should not be on CPU/Vacuum because it’s a part of our evolution system, but the question should be on how efficiently Ion thruster/Threads created that’s a matter of discussion for today.
In Android, we generally use RxJava and Coroutine to perform any asynchronous task. But one question always arises in my mind which is better. Declaimer: I don’t believe in materialism So we don’t talk about syntax. but we will talk about their truth.
I did the experiment around IO and Computation type of operations i.e Generation of QR Code 1000 to 10000 times.
Max and Min memory and CPU usage.

Max and min usage of memory and CPU on different techniques.

Used Android Studio’s profiler to generate the consolidated report by taking thread dump with the mentioned data set. So I invoke Qr-generation logic on the I/O thread and the Computation thread in async and sync way both. What I got coroutine performed well.
Now the question arises in my mind, How and why coroutine? and my answer is coroutine uses lightweight threads. That’s why our cosmos ship flies to the edge of the universe.
Heavyweight threads, (usually kernel/os level threads) have a lot of contexts (hardware registers, kernel stacks, etc). So it takes a lot of time to switch between threads. Heavyweight threads may also have restrictions on them, for example, on some OSes, kernel threads cannot be pre-empted, which means they can’t forcibly be switched out until they give up control.
Lightweight threads on the other hand (usually, user-space threads) have much less context. (They essentially share the same hardware context), they only need to store the context of the user stack, hence the time taking to switch lightweight threads is much shorter.
Overall Conclusion: Coroutine reduces the wastage of CPU cycles and increases optimization.
I hope you enjoyed the story.
This is the Way !!

This is the Way !!

Reference : Android studio profiler https://developer.android.com/studio/profile/android-profiler?gclid=CjwKCAiAhbeCBhBcEiwAkv2cY_gJKQXlI88EjaRXDYHKQ8HKOsgDym_sMGkL8twtR5_aXCON9jcHmxoCE9YQAvD_BwE&gclsrc=aw.ds

Light weight threads : https://en.wikipedia.org/wiki/Light-weight_process

The Mandalorian https://en.wikipedia.org/wiki/The_Mandalorian

--

--

Atul Sakhala

Android Developer with the fusion of Scientific Spirituality - FreeLancer, Founder of GameReel.gg and Tryhard.gg, Ex-Rivigo, Ex-BharatPe