Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting Object Reference to Null  RSS feed

 
Vasudevan Gopalan
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Will it improve the performace of JVM by Setting the object to NULL explicitly if we are not using it.
Regards
Vasu
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you've finished using it for good though, and no-one else is using it, it will save memory - the next run of the garbage collector will clean it up.
If the object has a big-enough memory footprint and you're using virtual memory there could be a performance boost caused by less paging.
-Tim
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim West:
If you've finished using it for good though, and no-one else is using it, it will save memory - the next run of the garbage collector will clean it up.

Correct - but only if the reference is living much longer, of course.
 
Randall Twede
Ranch Hand
Posts: 4583
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is a good practice. kind of like saying please and thank you. no, you dont have to do it. yes, it might not make any difference. but is a good habit to develop.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Randall Twede:
it is a good practice. kind of like saying please and thank you. no, you dont have to do it. yes, it might not make any difference. but is a good habit to develop.

I wholeheartedly disagree. To me, all it does is adding clutter to the code.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36396
453
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel it is better to give the objects a more local scope (like inside of an if statement) than explicitly nulling them out. Then the developer doesn't need to think about whether the reference is valid.
It's a good habit to null out if you are also programming in C++. I can't see what value it brings to java alone.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Randall Twede:
it is a good practice. kind of like saying please and thank you. no, you dont have to do it. yes, it might not make any difference. but is a good habit to develop.

I disagree with Ilja Preuss.
And as long as you don't put these sentences above into a comment, the code says something different to me. It says:
I was written by a foreign-language-programmer, who is used to nulling.
Or written by a pupil of a foreign-language-programmer, who blinly believed everthing what he or she saw.
Or it is written by desperate I-make-everthing-in-my-own-way hacker, who implemented his own compiler and jvm, which had a minor bug.
Do you write code like this:

Why not?
Last but not least: Setting a variable to null is a great performance-tradeoff, since it needs an additional assignment.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!