Results 1 to 7 of 7
  1. #1
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Question 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.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    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

  3. #3
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    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
    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!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    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

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    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.
    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

  6. #6
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    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.
    Well I guess my definition of the clear() method is different from ArrayList. What you described is what I do in the reset() method

    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
    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...

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    The basic rule is don't try and second guess the gc.
    There are cases, but they are rare and generally quite specific.

Similar Threads

  1. How to get null values stored in array
    By Ms.Ranjan in forum New To Java
    Replies: 4
    Last Post: 05-21-2009, 10:29 PM
  2. Displaying array elements as null except the last index
    By vasavi.singh in forum Advanced Java
    Replies: 2
    Last Post: 04-06-2009, 11:42 AM
  3. help array set null or delete
    By chkm8 in forum New To Java
    Replies: 1
    Last Post: 01-19-2009, 08:15 PM
  4. How setting an Object to null help Garbage Collection?
    By piyu.sha in forum Advanced Java
    Replies: 3
    Last Post: 10-06-2008, 07:35 AM
  5. Null array when passed to MouseListener
    By stevemcc in forum New To Java
    Replies: 2
    Last Post: 04-02-2008, 10:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •