user3271166
user3271166

Reputation: 623

Is it worth setting objects to null after their use is completed?

I am developing a 2d game in javafx. There are certain elements that I remove from scene once they have done their part. Do you think its a good practice to set all variables and values to 0/null in those objects just as they are removed?

I do know that such a level of optimization may not be required given modern computers have GBs of memory. But I am trying to kind of get as much lower memory as I can (just a personal challenge). I wonder if GC can detect that these objects have completed their lifecycle and are no longer required....?

Upvotes: 1

Views: 187

Answers (1)

jchampemont
jchampemont

Reputation: 2773

From the Book "Effective Java":

Nulling out references should be the exception, not the norm. The best way to eliminate an obsolete reference is to let the variable that contained the reference fall out of scope.

Upvotes: 3

Related Questions