I am trying to understand the performance difference between Threads and Coroutines, So what I did to see this, I have created a task where a millions of threads will be created in a for loop and each thread will have a delay of 2000L Milliseconds and then It should print thread name to Log cat.
My Android Device Details :
Operating System:
Android 7.0
Processor:
Cortex A7 + ARM
Memory:
8GB eMMC Flash + 1GB DDR RAM | Optional: 16GB eMMC Flash + 2GB DDR RAM
Extended Micro SD Card Slot Up To 128GB
So below are the cases along with the errors:
Case1 With Threads:
class MainActivity : AppCompactActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Thread {
kotlin.run {
repeat(1000000) {
Thread {
kotlin.run {
Thread.sleep(2000L)
Logger.logMessage(Thread.currentThread().name)
}
}.start()
}
}
}.start()
}
}
For this above code below is the error:
FATAL EXCEPTION: Thread-530
Process: com.example.myapplication, PID: 23186
java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again
at java.lang.Thread.nativeCreate(Native Method)
at java.lang.Thread.start(Thread.java:1063)
at com.example.myapplication.MainActivity.onCreate$lambda-4(MainActivity.kt:27)
at com.example.myapplication.MainActivity.$r8$lambda$wxhHVdQ1CufqTvgll4vfwuRdMr0(MainActivity.kt)
at com.example.myapplication.MainActivity$$ExternalSyntheticLambda0.run(D8$$SyntheticClass)
at java.lang.Thread.run(Thread.java:818)
Case2 With Coroutines:
class MainActivity : AppCompactActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
CoroutineScope(Dispatchers.IO).launch {
repeat(100_00_00) {
CoroutineScope(Dispatchers.IO).launch {
delay(2000L)
Logger.logMessage(Thread.currentThread().name)
}
}
}
}
}
For this above coroutine code below is the error:
2022-09-17 08:54:28.916 17040-17140 AndroidRuntime com.example.myapplication E FATAL EXCEPTION: DefaultDispatcher-worker-20
Process: com.example.myapplication, PID: 17040
java.lang.OutOfMemoryError: Failed to allocate a 8388620 byte allocation with 16777120 free bytes and 23MB until OOM; failed due to fragmentation (required continguous free 8392704 bytes where largest contiguous free 196608 bytes)
at java.util.concurrent.atomic.AtomicReferenceArray.<init>(AtomicReferenceArray.java:64)
at kotlinx.coroutines.internal.LockFreeTaskQueueCore.<init>(LockFreeTaskQueue.kt:83)
at kotlinx.coroutines.internal.LockFreeTaskQueueCore.allocateNextCopy(LockFreeTaskQueue.kt:230)
at kotlinx.coroutines.internal.LockFreeTaskQueueCore.allocateOrGetNextCopy(LockFreeTaskQueue.kt:225)
at kotlinx.coroutines.internal.LockFreeTaskQueueCore.next(LockFreeTaskQueue.kt:214)
at kotlinx.coroutines.internal.LockFreeTaskQueue.removeFirstOrNull(LockFreeTaskQueue.kt:61)
at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:39)
at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:749)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
So I want to know why coroutines also run out of memory, Please help me in this.
Many Thanks In advance