如何避免由于自定义静态处理程序类导致的内存泄漏? [英] How to avoid memory leaks due to custom static handler class?

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

问题描述

我的自定义处理程序类中发生了某些内存泄漏,但不确定如何修复它.在线检查了几个示例,但没有特定于我的代码,所以不知道如何去做:

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 丑陋的解决方案基于这样一个事实,即 lambdas 应该捕获父引用,只有在直接使用它的地方.所以我想出了这个替换,我没有在调试器中看到强引用,但你应该检查一下:

#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....
    }

}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆