Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does variable = null set it for garbage collection

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?

like image 342
ashurexm Avatar asked May 28 '10 17:05

ashurexm


1 Answers

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

like image 131
sblundy Avatar answered Oct 04 '22 08:10

sblundy