You should not be be using GlobalScope to execute your coroutines as they execute outside the scope of the Activity and will not be garbage collected when the Activity is. The solution is to have the Activity implement CoRountineContext() interface. You can assign the appropriate dispatcher to the coroutineContext such as:
override val coroutineContext: CoroutineContext
get() = Dispatchers.IO + SupervisorJob()
Then when your class needs to execute a coroutine you can simply do:
` launch { someCode() }
To clean all your coroutines up so nothing leaks memory and gets gc’d when the activity does in the onStop() add the following code to clean up any coroutines that may still be running
coroutineContext.cancelChildren()
For more information see here: Kotlin Coroutines patterns & anti-patterns | by Dmytro Danylyk | ProAndroidDev