0

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

Shermano
  • 1,100
  • 8
  • 31
  • To force GC, write `System.gc()`. However, you should not be controlling the collector. Hopefully, someone else can improve your current solution. – Taseer Jul 11 '19 at 17:48
  • Thanks Taseer, i just saw that there is nothing wrong with all room calls, it's the for loop and maybe it's the way im spliting and creating a map – Shermano Jul 11 '19 at 17:57

1 Answers1

2

There is no way to directly invoke garbage collection, but you can call a method System.gc() to send a notification to garbage collector that there is garbage to be collected, note that this doesn't guarantee that collection will be performed.

Anyway this is considered bad practice and probably won't solve your problem.

Your problem is most likely one of the following:

  1. You are having some memory leak which prevents GC process, most of the time this is caused by keeping reference to context/view after activity is destroyed which in turn prevents it to be processed by GC, you could also try LeakCanary.
  2. You are starting a lot of activities on top of each other, there is already great answer on this topic which you can read here - Android app out of memory issues.
FilipRistic
  • 2,661
  • 4
  • 22
  • 31