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

查看:184
本文介绍了variable = 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

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

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