首页 > 解决方案 > 如何释放非空变量以进行内存泄漏保护

问题描述

我不知道如何在 Kotlin 中释放对象的非空变量。下面的代码是一个 TextView 列表。它被初始化为非空对象。

class aaa{
    var DenomList: MutableList<TextView> = mutableListOf()
    var DenomPCSList: MutableList<TextView> = mutableListOf()
    var DenomAmountList: MutableList<TextView> = mutableListOf()
    var DenomNotationList: MutableList<TextView> = mutableListOf()
    var DenomAmountNotationList: MutableList<TextView> = mutableListOf().
}

我想释放上述代码中的对象进行垃圾回收,以保护 Kotlin 中的内存泄漏。请给我一些信息。

标签: kotlin

解决方案


为了使对象符合垃圾回收条件,下面列出了几个选项:

1) 移除不再使用的特定元素。例如:

list.remove

2)清除列表中的所有元素

list.clear

3)使整个列表分配给null(如果适用,在更改为可空类型之后)

你需要用来制作一个合适的。


推荐阅读