aspose file tools*
The moose likes Java in General and the fly likes Is it good practice to nullify strings and objects after use to minimize chances of out of memory. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is it good practice to nullify strings and objects after use to minimize chances of out of memory." Watch "Is it good practice to nullify strings and objects after use to minimize chances of out of memory." New topic
Author

Is it good practice to nullify strings and objects after use to minimize chances of out of memory.

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 541
Is it good practice to nullify strings and objects after use to minimize chances of out of memory. By nullifying string I am talking about below code:



Should this be done after the last use of objects?

thanks
C Sanders
Greenhorn

Joined: Jul 29, 2013
Posts: 2
People apparently smarter than I have said that it's not necessary to set local scope variables to null because the compiler is smart enough to figure out when they are not needed anymore.
However, if there are member variables that will remain longer than they are needed, it is a good idea to set them to null in order to help prevent memory leaks.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

It's good practice to declare variables in the tightest possible scope. Once they go out of scope the objects they refer to are eligible for garbage collection, and you have nothing to gain by explicitly setting them to null.

That said, there are certain Java classes that attach native resources. If you create() a Graphics object, it's good practice to dispose() it when no longer needed. Under certain circumstances, it may be desirable or even necessary to flush() an Image. A top level Swing or AWT window can be dispose()d to release native resources. Various Listeners may still be reachable long after they are needed; you don't need to set them to null but you may need to removeXxxListener(...) to make them eligible for GC.

And of course streams and database connections should always be properly closed.


luck, db
There are no new questions, but there may be new answers.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

No, it is not good practice and not necessary to do this all the time.

Only do this when you have a really good reason to do so. In real code, this is almost never necessary.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

And never ever ever have a "helper" method which will accept an object instance and set it to null


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it good practice to nullify strings and objects after use to minimize chances of out of memory.
 
Similar Threads
nullifying an object in a generics list collection
Suggestion for increasing the chance of Garbage Collection
static variables - belong to the class.
Memory Leak Issues
Multi-Thread Performance