变量 = null 是否将其设置为垃圾收集 [英] Does variable = null set it for garbage collection

查看:23
本文介绍了变量 = null 是否将其设置为垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助我解决与同事的纠纷:在 Java 中将变量或集合设置为 null 是否有助于垃圾收集和减少内存使用?如果我有一个长时间运行的程序并且每个函数都可能被迭代调用(可能调用数千次):在将值返回给父函数之前将其中的所有变量设置为 null 是否有助于减少堆大小/内存使用量?

Help me settle a dispute with a coworker: Does setting a variable or collection to null in Java aid in garbage collection and reducing memory usage? If I have a long running program and each function may be iteratively called (potentially thousands of times): Does setting all the variables in it to null before returning a value to the parent function help reduce heap size/memory usage?

推荐答案

那是旧的性能知识.在 1.0 天之前确实如此,但是编译器和 JVM 已经过改进以消除需求(如果有的话).如果您有兴趣,这篇优秀的 IBM 文章将详细介绍:Java 理论与实践:垃圾收集与性能

That's old performance lore. It was true back in 1.0 days, but the compiler and the JVM have been improved to eliminate the need (if ever there was one). This excellent IBM article gets into the details if you're interested: Java theory and practice: Garbage collection and performance

这篇关于变量 = null 是否将其设置为垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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