如何避免由于自定义静态处理程序类而导致的内存泄漏

如何避免由于自定义静态处理程序类而导致的内存泄漏

本文介绍了如何避免由于自定义静态处理程序类而导致的内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自定义处理程序类中发生了某些内存泄漏,但不确定如何解决.在线签出了一些示例,但是我的代码没有什么特别的,因此不确定如何进行操作:

I have certain memory leaks happening in my custom handler class ,but not sure how to fix it. checkedout a couple of examples online but nothing is specific to my code so not sure how to go about it :

private val startupCallback = object: RetryCallback(NUMBER, DELAY) {
        override fun onRetry(retryCount: Int) {

            mySdkApi.applicationStartup(this)
        }

        override fun onCompleted(): Boolean {
            updateStatus(Callback.Status.StartUpSDK)

            return true
        }

        override fun onFailed(e: MyException?) {
            updateStatus(Callback.Status.StartUpSDK, "", e)
        }
    }

Android Studio不断提示此处理程序类应该是静态的,否则可能会发生泄漏".有什么主意吗?

Android studio keeps prompting "This handler class should be static or leaks might occur".Any ideas how to go about it?

推荐答案

Android Studio抱怨很合理.问题在于匿名类会捕获对创建它们的父类的引用.

The Android Studio complaining is pretty reasonable. The problem is that anonymous classes capture reference to the parent class that they were created in.

基本上有两种解决方法:不美观"和丑陋".它们都与WeakReference有关.

There are basically two solutions the "not pretty" and the ugly.) Both of them are about WeakReference.

#1 不太好的解决方案是创建一个需要较弱引用的类

#1 The not pretty solution is to make a class that will take a weak ref

class ApiRetryCallback(activity: Activity): RetryCallback(NUMBER, DELAY) {

    private val weakActivity = WeakReference(activity)

    override fun onRetry(retryCount: Int) {

        weakActivity.get()!!.mySdkApi.applicationStartup(this) //or weakThis.get()? to swallow null cases
    }

    override fun onCompleted(): Boolean {
        weakActivity.get()!!.updateStatus(Callback.Status.StartUpSDK)

        return true
    }

    override fun onFailed(e: MyException?) {
        weakActivity.get()!!.updateStatus(Callback.Status.StartUpSDK, "", e)
    }
}

活动中:

private val startupCallback = ApiRetryCallback(this) //this is MainActivity here

#2 丑陋的解决方案基于以下事实:lambda只能在直接使用父引用的情况下才捕获父引用.因此,我想出了这种替代方法,但在调试器中没有看到强引用,但是您应该检查一下:

#2 The ugly solution is based on a fact that lambdas should capture parent reference, only where there is a direct usage of it. So I came up with this substitution and I didn't see strong references in a debugger but you should check that:

private val startupCallback = {
    val weakActivity = WeakReference(this@MainActivity)

    object : RetryCallback(NUMBER, DELAY) { //returned as last expression

        override fun onRetry(retryCount: Int) {

            weakActivity.get()!!.mySdkApi.applicationStartup(this) //or weakThis.get()? to swallow null cases
        }

        //....else methods....
    }

}()

在这里,lambda将立即被调用,并且将仅捕获对象内部的弱引用,还将返回对象是对象的最后一个表达式.

Here the lambda will be called immediately and will capture only the weak reference inside the object, also it will return the last expression wich is object.

#3 在撰写本文时,我想出了第三个解决方案,该解决方案接近#2

#3 While I was writing, I came up with a third solution, which is close to #2

private val startupCallback = WeakReference(this).let { //this here is MainActivity
    val weakActivity = it //it of let scope wich is WeakReference

    object : RetryCallback(NUMBER, DELAY) { //returned as last expression

        override fun onRetry(retryCount: Int) {

            weakActivity.get()!!.mySdkApi.applicationStartup(this) //or weakThis.get()? to swallow null cases
        }

        //....else methods....
    }

}

这篇关于如何避免由于自定义静态处理程序类而导致的内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:17