• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

usefullness to set vars to null

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A vendor's website offers this advice (when programming in j2me):

For saving memory, explicitly set unused object to null.

In my midlet, I am closing InputStreams via this method:

Should I add a "is = null" in this method ?
The article that I took this advice from is several years old.
Is it still a sound advice to adhere to ?

Thanks
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer the name "istream" (some would say "inputStream" just like its class) for a variable name rather than "is". Aside from the problems I've have in the past when using too-short variable names for variables that aren't throwaways (i, jk, x, etc.), the fact that "is" is very a common English word can be distracting.

But that's more human engineering. The compiler won't care.

Here are some good reasons for nulling out variables when you don't need them anymore:

1. If you accidentally reference the variable past the point where you've nulled it, you get immediate notification that you've used it improperly. A smart compiler would notice the null value if the logic path wasn't too complicated, and even if you could get a clean compile, the ensuing NullPointerException at runtime would make things obvious. This is one of the anti-bugging tricks I like to use.

2. Java reclaims resources by garbage collection. However, an object cannot be garbage-collected as long as at least one other object (or variable) knows about it. So by removing the reference, you make the "is" object more eligible for garbage collection - it doesn't have to wait to go out of context.

For deeper discussion on this and other fascinating bits of Java's innards, the best place to go is our general Java forums.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is it still a sound advice to adhere to ? Yes.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim & Nicholas,

Thanks for your responses.

Regarding Tim's comment about the variable name 'is'; I'll put that on my new year's programming resolution. I have a habit of doing that and once got a reprimand from my tech lead to avoid naming a logging method:

'd' here stands for "debug". From a personal point of view, I wanted to reduce the verbosity in the code. It wasn't about laziness.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Nicholas,

Your code example would not work.
This is the working version (my initial implementation of "close" remains unchanged).

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic