• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Setting Object Reference to Null

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.
 
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Look ma! I'm selling my stuff!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic