wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What is the proper method to null out an array ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the proper method to null out an array ?" Watch "What is the proper method to null out an array ?" New topic
Author

What is the proper method to null out an array ?

Alan Wilbur
Greenhorn

Joined: Sep 14, 2012
Posts: 1
When nulling out an array object and the objects referenced in the array to make all the objects available for garbage collection right away, do you have to iterate thru the array and null each array reference item and then null the array object or is it sufficient just to null the array object?
Richa Sinha
Greenhorn

Joined: Sep 14, 2012
Posts: 5
You can null out an array just by making it point to NULL.
E.g. Suppose you have an array like

String []arr=new String[10];
// A few lines of code.
// Now you want to garbage collect the array.
arr=null; //This line will make your array available for garbage collection.

You don't need to iterate through the array and make every instance point to null. A single line of code making the whole array object pointing to null will also work.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4377
    
    8

Hi Alan and Richa. Welcome to the Ranch!

Richa is correct, although you should also note that simply letting the variable go out of scope is sufficient to make it eligible for garbage collection. In most cases, in a well designed program, you don't need to bother setting references to null.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7718
    
  20

Alan Wilbur wrote:When nulling out an array object and the objects referenced in the array to make all the objects available for garbage collection right away, do you have to iterate thru the array and null each array reference item and then null the array object or is it sufficient just to null the array object?

As others have said, you don't need to null out elements specifically. However, at some point you may have a requirement to initialize an array with some value other than null. If you ever do, have a look at Arrays.fill().

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3617
    
  14

The question is, why would you want to do this in the first place?

Having null values around usually isn't necessary. If you have a non-local collection or array around that should not contain any elements, make them empty, not null. In this case, that would be array = new TypeName[0].
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Welcome again, both of you
Garbage collection (GC) is automatic. Let the JVM handle it, and don’t worry about it. You don’t think about breathing, but do it automatically (most of the time), and just let it happen.
So don’t think about GC; just let it happen. As Matthew said earlier, simply let it go out of scope.
Rishi Shah
Ranch Hand

Joined: Sep 05, 2012
Posts: 43

No, setting an object to null in order to help garbage collection is just a myth. Like aliens on Earth.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1761
    
    7

In a security conscious context, clearing an array's elements might make sense, for instance JAAS' PasswordCallback defines a clearPassword() method that clears the char array that holds the password by fillling it with space (0x20) characters.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Aleksey Vladimirovich
Ranch Hand

Joined: Sep 05, 2012
Posts: 56
Rishi Shah wrote:No, setting an object to null in order to help garbage collection is just a myth. Like aliens on Earth.

I don't think so...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Aleksey Vladimirovich wrote:
Rishi Shah wrote:No, setting an object to null in order to help garbage collection is just a myth. Like aliens on Earth.

I don't think so...
Agree. Aliens are real.
Aleksey Vladimirovich
Ranch Hand

Joined: Sep 05, 2012
Posts: 56
Campbell Ritchie wrote:
Aleksey Vladimirovich wrote:
Rishi Shah wrote:No, setting an object to null in order to help garbage collection is just a myth. Like aliens on Earth.

I don't think so...
Agree. Aliens are real.

I see your sarcasm, but you are saying that if one have a big collection of objects consuming, say, 20MB of RAM and he's done some calculations, gotten some result and now is performing some time-consuming processing of the result, but there is no need in original collection of objects anymore, so he shouldn't set it to null for saving some memory for other needs and wait untill this object goes out of scope?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3013
    
  10
On rare occasions it can make sense to null out some elements of an array, if you need to keep the array around in the JVM for a longer period of time, but you want to make individual objects referenced by the array available for garbage collection. This is done by ArrayList internally, for example. It's not common, but it can be useful and even necessary.

As for nulling out a reference to the array itself, that too is sometimes useful, and other times unnecessary. If a reference is held by a local variable, it's usually pointless to null it out; just let it go out of scope instead. But if it's a field in an object that needs to stay in memory, it may occasionally be useful to null out that field to make something available for GC after you no longer need it. Again, not common, but occasionally useful and even necessary.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7718
    
  20

Aleksey Vladimirovich wrote:I see your sarcasm, but you are saying that if one have a big collection of objects consuming, say, 20MB of RAM and he's done some calculations, gotten some result and now is performing some time-consuming processing of the result, but there is no need in original collection of objects anymore, so he shouldn't set it to null for saving some memory for other needs and wait untill this object goes out of scope?

I hate to disagree with my colleague Mike here, but: NO.

If that makes the difference between your program working or not (or even working badly or working well), then almost certainly your program has much bigger problems than just reclaiming memory. If, after proper analysis, you can prove that that's not the case, give it more memory; and then start thinking about a budget for a new server...fast.

Winston

[Edit] And I will qualify my response for the specific example you give: If your program is properly designed, the collection(s) required to get the totals you need to do your "time-consuming processing of the result" should already be out of scope.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3013
    
  10
Winston Gutkowski wrote:I hate to disagree with my colleague Mike here, but: NO.

Liar! You love it - admit it.

I'm just heading out the door, so I'll just say I agree with Winston as far as general rules go. I allow for the possibility of occasional individual exceptions where what Aleksey describes might still make sense. I haven't read through and thought through this particular scenario carefully. But I will pretty much always disagree with any rule that says we must always do things a certain way.

Later...
Aleksey Vladimirovich
Ranch Hand

Joined: Sep 05, 2012
Posts: 56
Winston Gutkowski wrote:[Edit] And I will qualify my response for the specific example you give: If your program is properly designed, the collection(s) required to get the totals you need to do your "time-consuming processing of the result" should already be out of scope.

Yeah, I was waiting for response like that I'm not saying that it is a necessary thing to do, but sometimes it is better to use it. Of course the example I gave was an exaggeration, but we don't live in the perfect world and a developer may have to work with a "legacy code" or circumstances / "religious beleifs" / deadlines / whatever might not allow to architect a product properly and it is better to "help garbage collector" (at least as a temporary solution) than claim that "it's the same as aliens on Earth" and do nothing.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7718
    
  20

Mike Simmons wrote:Liar! You love it - admit it.

Curses! Found out again.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
…and everybody agrees with one another!
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3013
    
  10
I don't!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the proper method to null out an array ?