My activity is running a algorithm that may use a large size of device memory as soon as activity is created, but is working ok, but when I keep exiting pressing back and then taping to enter on activity again non-stop a lot of times, it seems that the app is accumulating memory, I saw on my profile and forcing the app like that just crash my app on less than 20 attempts.
However, commenting this code let me just quit and back again from the activity as quickly as I can no matter how much times I do that.
EDIT: I just erased the for loop and tested it and works great, it seems that the way Im spliting large strings a lot of times to create a map is accumlating memory, there is a way to handle this?
This is my algorithm, it's on a ViewModel if that matters
fun getAllChartPoint(id: Int){
getTotalPackages(id) { // ROOM CALL returns as integer
myCallRepository.getOrder(id){ // room call to get an object 'CALL'
val odr = SupportVersionHelper.getAdcRateConfigOrDefault(it.adcOdrX!!,it. adcOdrY!!)
myPackgerepository.getAllPackagesWithId(it){ // Another ROOM call, returns a list of objects, that contanis a big string that will be splitted
for (index in 0 until it.size) {
if (it[index].signal != null) {
val splitedPoint = it[index].signal!!.split(" ")
if (isMemoryLow(context)){
return@getFhirPackagesForMonitoringWithId
}
splitedPoint.map { signal ->
signal.toFloatOrNull()?.let { floatSignal ->
map[currIndexMap.toFloat()] = floatSignal
currIndexMap++
}
}
}
}
}
}
}
}
All Room calls are being added to a CompositeSubscription() that are being cleared out on onCleared() method of the ViewModel
fun getNumberPackages(monitoringId: Long, onCompletion: (Int) -> Unit, onFail: (Throwable) -> Unit){
val subscription = Single.fromCallable { fhirPackageDao?.numberOfFhirPackageForMonitoring(monitoringId) }
?.subscribeOn(Schedulers.io())
?.subscribe({
onCompletion(it ?: return@subscribe)
}, {
onFail(it)
BleLogHelper.writeError("Error fetching totak of packages", it)
})
subscriptions.add(subscription)
}
There is something I can do to destroy all instances when the viewModel is cleared? I tried to surroung this code with doAsync from anko lib, but it didn't work at all