Results 1 to 7 of 7
- 10-15-2010, 10:11 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 35
- Rep Power
- 0
Is it better to set array elements to null before setting the arrayreference to null?
Hi,
I would like to know, is it a better practice to set array elements to null before releasing the handle of the array? I mean, does it help the gc to do it and is there an overall gain in using this practice?
Thanks!Last edited by kreyszig; 10-15-2010 at 10:23 PM.
- 10-16-2010, 08:41 AM #2
In general, you shouldn't set anything to null. Declare variables in the tightest possible scope and let them be eligible for GC when no longer visible.
For classes that may hold on to native resources, a method is provided to release those, typically dispose(). Examples are Graphics / Graphics2D, and top-level window GUI classes. In a memory-intensive application, it may help to dispose() such objects before they go out of scope.
Other classes may have a flush() method. The documentation will tell you whether this is involved in releasing resources or not.
But setting a reference to null is mostly meaningless.
db
- 10-16-2010, 12:44 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 35
- Rep Power
- 0
Hmm, maybe I should be a bit more specific about my question...
What I am doing is writing a class similar to ArrayList, which has an array of Object and a capacity that is is general larger than the number of entries.
If I remove an entry from the end, I could simply decrement the the counter for the number of entries, but I think it makes much more sense to set the last element to null as well, because otherwise I am holding a pointer to this "ghost" element indefinitely...
In the worse case scenario, if I "reset" my array (setting the number of entries to zero but without changing the array capacity), not setting any pointer to null would hold all the elements of the array alive for no reason such that the reset function becomes pretty much useless.
Now my question is what if I want to "clear" my array (setting the number of entries to zero and the capacity to zero), should I simply set the pointer of the array to null, or set the elements of the array to null right before doing it? The memory used by the objects that are pointed by the array is obviously much larger than the memory used by the array itself, so the quicker the GC realizes it can clear up these objects the better. I just don't know if it is a better practice to do it in my class in this case or let the GC realize first that the handle to the array is lost and then that it means the handles to the objects are lost as well, so it should do something about it...
Thanks!
- 10-16-2010, 02:36 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
That's exactly how Java's Collections do it, i.e. when an element is removed the slots are explicitly nulled if necessary; it's the correct thing to do.
kind regards,
Jos
- 10-16-2010, 03:45 PM #5
Yes, but ArrayList#clear() doesn't reduce the capacity nor assign a new array to the internal array elementData, as the OP seems to be inclined to do.
@OP: You don't save anything by assigning a new array. Just assign null to all elements of the existing array in clear() -- as does ArrayList. But if you do assign a new array, then there's no point in nullifying the elements of the old one, as both the old array and its elements will be eligible for GC.
db
- 10-16-2010, 08:32 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 35
- Rep Power
- 0
Well I guess my definition of the clear() method is different from ArrayList. What you described is what I do in the reset() method
I know, but I was wondering if it would help the GC to notice more quickly that the objects are no longer needed by nullifying the references in the array...But if you do assign a new array, then there's no point in nullifying the elements of the old one, as both the old array and its elements will be eligible for GC.
db
- 10-18-2010, 10:40 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Similar Threads
-
How to get null values stored in array
By Ms.Ranjan in forum New To JavaReplies: 4Last Post: 05-21-2009, 10:29 PM -
Displaying array elements as null except the last index
By vasavi.singh in forum Advanced JavaReplies: 2Last Post: 04-06-2009, 11:42 AM -
help array set null or delete
By chkm8 in forum New To JavaReplies: 1Last Post: 01-19-2009, 08:15 PM -
How setting an Object to null help Garbage Collection?
By piyu.sha in forum Advanced JavaReplies: 3Last Post: 10-06-2008, 07:35 AM -
Null array when passed to MouseListener
By stevemcc in forum New To JavaReplies: 2Last Post: 04-02-2008, 10:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks