0

I am using in-app update library and I noticed that it is leading to a memory leak.

Here is my code:


class ForceUpdateBaseActivity : AppCompatActivity() {

    private val realtimeDatabaseService: RealtimeDatabaseService by inject()
    private var appUpdateManager: AppUpdateManager? = null

    private val bag = CompositeDisposable()

    override fun onResume() {
        super.onResume()

        val contextWeakReference = WeakReference(applicationContext)
        appUpdateManager = contextWeakReference.get()?.let {
            AppUpdateManagerFactory.create(it)
        }

        // Checks that the update is not stalled during 'onResume()'.
        // However, you should execute this check at all entry points into the app.
        appUpdateManager
            ?.appUpdateInfo
            ?.addOnSuccessListener { appUpdateInfo ->
                if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
                    // If an in-app update is already running, resume the update.
                    appUpdateManager?.startUpdateFlowForResult(
                        appUpdateInfo,
                        AppUpdateType.IMMEDIATE,
                        this,
                        IN_APP_UPDATE_REQUEST_CODE
                    )
                }
            }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == IN_APP_UPDATE_REQUEST_CODE) {
            if (resultCode != RESULT_OK) {
                // Update flow has failed. Re-initiate force update
                checkForAppUpdate()
            }
        }
    }

    fun checkForAppUpdate() {
        realtimeDatabaseService.enableForceUpdate.filter { it }.subscribe {
            val appUpdateInfoTask = appUpdateManager?.appUpdateInfo ---> causing memory leak

            appUpdateInfoTask?.addOnSuccessListener { appUpdateInfo ->
                if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE &&
                    appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
                ) {
                    appUpdateManager?.startUpdateFlowForResult(
                        appUpdateInfo,
                        AppUpdateType.IMMEDIATE,
                        this,
                        IN_APP_UPDATE_REQUEST_CODE
                    )
                }
            }
        }.addTo(bag)
    }

    override fun onStop() {
        super.onStop()
        appUpdateManager = null
        bag.clear()
    }

    companion object {
        const val IN_APP_UPDATE_REQUEST_CODE = 123490
    }
}

I tracked down that

val appUpdateInfoTask = appUpdateManager?.appUpdateInfo

is causing memory leak.

I referred to solution in this link: How to prevent memory leak with In-App Update Library and used a week reference of application context in creating AppUpdateManager but it still leaks.

Any idea as to what else I can try to fix the memory leak issue.

srisindhu saride
  • 391
  • 6
  • 25

0 Answers0