• Post Reply Bookmark Topic Watch Topic
  • New Topic

nullyfying object.  RSS feed

 
Habeeb Shaikh
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we create the object then we used it. After this is it need to null this.if we do then garbage collector works better or not?
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The garbage collector does not "work better" because the variables are set to null after they are used. This practice potentially improves the memory management functions of the JRE when the application is executing. Once an object does not have any references to it, it then becomes eligible for garbage collection. By explicitly setting the variables to null, you are making it easier for the JRE to do its job.

Settting object variables to null when they are no longer needed is a Java programming best practice.
[ November 23, 2008: Message edited by: James Clark ]
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Setting references explicitly to "null" may even hurt a program's performance with modern garbage collector implementations. Read this really good article from Brian Goetz. There's a section about "explicit nulling" that explains why it may be a bad idea to generally set all references to null.

Marco
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Clark:
Settting object variables to null when they are no longer needed is a Java programming best practice.

No it is not, and I agree with Marco that it could even hurt performance. Do not make setting variables to null a habit - it isn't necessary and doesn't help the way you think it does.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hurt performance? Really now? It is a healthy practice that produces clean code and helps the programmers avoid possible mistakes by maintaining references when not needed. As a programming practice it is helpful.

In regards to the four-year old web article, the author barely explains the situation where he/she thinks it may be harmful. However, he/she also briefly explains the situation where this practice is helpful. The article does not contain enough information to be useful as a reference.

Performance is a metric that has to be defined in a context. Without the context or the metric value, discussions about "affecting performance" are typically nonsense forum chatter.

It takes dicipline to develop "setting unused references to null." Programmers that tend to rush through writing code like a text message taking shortcuts whenever possible, most likely will disagree with me.
Overall, it is a style issue. That is all.
[ November 24, 2008: Message edited by: James Clark ]
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The trouble is, when you say without qualification that setting variables to null so that objects have no references is a "best practice", you lead beginners into not-so-useful practices like setting local variables to null immediately before they go out of scope. Or, as in one piece of code I was given, overriding the finalize() method to set an instance variable to null. This latter is definitely not a "best practice".
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,

unfortunately I'm not an expert in garbage collector design or implementation, so I can't explain in greater detail when and why it's more like a bad practice to set references to null. Brian Goetz is really an expert and I think he knows enough about Java concurrency, the memory model or JVM internals. I'm pretty sure he wouldn't give wrong information in his articles but of course you're right, there are not very much information directly related to this subject in it. But I can tell you that this was not the only article I read about this topic.

I don't say that it's in general a bad practice to set references to null! But usually the programmer tries to "help" the garbage collector when references are set to null explicitly. And obviously there may be situations when you don't help the garbage collector but more probably prevent it from doing its work efficiently. And depending on the situation this in turn could hurt the overall program performance because the garbage collector thread(s) are definitely competing with the regular program threads.

But as I said, I'm not an expert and I just wanted to point out that there may be more to it than setting all references to null without thinking about the consequences. In some cases it would perhaps be better to take more care of the scope for object references as the article of Brian notes.

Marco
[ November 24, 2008: Message edited by: Marco Ehrentreich ]
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm thinking that if your program is designed properly that any objects that are no longer needed would no longer be in scope so therefor would not need to be set to null? Just a thought.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess that's exactly the point!

I recently wrote a GUI application of medium or large size which handles quite complex and big data structures. I often used a profiler to search for memory leaks or unnecessarily long allocated memory resources but there didn't show up any problems. In the source code there are only a few locations where I did set references explicitly to null (where it was required) but for all the other parts of the application I really didn't have to think too much about this problem because there are simply not too many situations where the problem exists if a program is well-designed.

Marco
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco, your the first person I've seen with "best scout" under their name... what does that mean?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I guess I'm really the only one here with "best scout" A few months ago a server which was hosting JavaRanch broke down and there was a campaign where everybody could donate for a new server. In turn one "gift" from the ranch was that you could choose one of these special names for your account. At first it actually was only "scout" but because of a little misunderstanding it became "best scout" That simple...

Marco
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Blindly" or "robotically" setting references to null without "thinking" is not a best practice. If setting one or more references to null causes a specific performance problem or a logic fault, then don't do it. Pretty simple, I think.

It is a good practice to "correctly" set references to null when they are "really" no longer needed and do not interfere with processing logic. This is a best practice. It is not that complex, and you don't have to be an expert to see the benefits.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!