gpt4 book ai didi

JAVA - 我是否应该在使用后为每个实例化对象设置 null?

转载 作者:行者123 更新时间:2023-11-29 07:03:52 26 4
gpt4 key购买 nike

我对 Java 垃圾收集器有疑问。只是为了说明,我有一个这样的 json:

List<Test_DTO> testes = new ArrayList();
JSONArray list = new JSONArray();

for (Test_DTO test : testes) {
JSONObject obj = new JSONObject();

if(test != null)
obj.put("codigo",""+test .getCodigo());

list.put(obj);
}

list = null;

我是否应该在我的文件末尾设置 list = null 以便收集器可以“清理”它?如果是这样,我应该在 list.put(obj); 之后的 for 中设置 obj = null 吗?或者(清洁)会自动发生吗?这个问题不是针对这个例子的,正如我所说的,只是为了更清楚地理解。

提前致谢

卢卡斯。

最佳答案

无需将其设置为空。垃圾收集器会处理它。看看这里以获得更好的理解。 http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

关于JAVA - 我是否应该在使用后为每个实例化对象设置 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22095205/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com