Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the proper method to null out an array ?

 
Alan Wilbur
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stephan van Hulst
Bartender
Pie
Posts: 5893
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49405
62
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, setting an object to null in order to help garbage collection is just a myth. Like aliens on Earth.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aleksey Vladimirovich
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49405
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Liar! You love it - admit it.

Curses! Found out again.

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…and everybody agrees with one another!
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic