1

Let me first premise my question with the fact that I am learning programming/data structures. Yes, I know that Java has a collections API to do what I am coding, but for the moment, I would rather learn the basic ideas first.

I am coding an ArrayList implementation and am curious about garbage collection when references become obsolete. So in my case I have a generic object array i.e E[] data = (E[]) new Object[size]; and have a method E set(int index, E item) which will replace the data[index] with item. Here is my method body:

public E set(int index, E item) {
    if(index<0||index>size-1) throw new IndexOutOfBoundsException(); //Not relevant to question
    E previousValue = data[index];
    data[index]=null; 
    data[index]=item;
    return previousValue;
}

On to my question. Is it necessary to make the object null even if it the index is going to be reassigned. It seems like I should, but the code looks redundant to me. Or if am correct, is there a less redundant looking way to code this? I would really appreciate more than a single sentence answer, I'm really curious about this topic.

Maxim
  • 725
  • 1
  • 8
  • 24
  • 1
    I don't know what more than a single sentence you're looking for. `null` isn't special, it's a value. Assigning null to an element in an array then assigning something else is redundant. – Brian Roach Dec 29 '13 at 08:50
  • 1
    http://stackoverflow.com/questions/850878/does-setting-java-objects-to-null-do-anything-anymore – Justin Jasmann Dec 29 '13 at 08:54

2 Answers2

10

No, there's no need to do that. It won't make any difference to garbage collection. It's only important to set a variable to null (in terms of garbage collection) in one of these cases:

  • It's a local variable in a long-running method
  • It's a static variable
  • It's an instance variable and the containing object itself is not going to be eligible for garbage collection

My personal experience is that these situations are relatively rare - for example, instance variables are usually useful throughout the lifetime of the containing object. Long-running methods containing some variables which aren't needed all the time should usually be split up into multiple methods, although that won't always be the case.

It's important to note that you're not setting an object to null - there's no such concept. You can only set the value of a variable to null. It's really important to distinguish between these concepts:

  • Variables (named storage locations)
  • References (a value which allows you to "navigate" to an object)
  • Objects

The value of a variable is never an object - only a reference or a primitive value. Think of a variable as being like a piece of paper - a piece of paper can't contain an actual house (the object) but you can write the address of a house on the piece of paper (the reference used to navigate to the object).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    You have a real talent wording concepts in the most easy to understand manner. – Sotirios Delimanolis Dec 29 '13 at 09:01
  • @ZouZou: One of these days I must really write that up in a proper article. I've used it a lot, and it seems to be effective. – Jon Skeet Dec 29 '13 at 09:06
  • I dislike the analogy myself. Imagine there are multiple references to one object. Some will be in variables, some will be anonymous expressions. I'd rather say that the piece of paper is the reference, and the variable is the cardboard folder that I store the piece of paper in, so that I don't lose it. Then, I could even keep that cardboard folder inside a house, to indicate an instance variable of an object! Oh, and a blank piece of paper is a null reference, obviously. – Dawood ibn Kareem Dec 29 '13 at 09:10
  • @JonSkeet, I also have a remove method in my List implementation. This method removes the Object at an index and shifts everything over one. In this case, would I have to set the last _variable_ null i.e data[data.length-1]? – Maxim Dec 29 '13 at 09:15
  • @DavidWallace: There can be multiple pieces of paper all with the same address written on them - that's easy. I think it's easier to think of a value as literally a value written on paper. Will expand in an article. – Jon Skeet Dec 29 '13 at 09:16
  • 2
    @Maksim: Yes, you need to do it in that case. – Jon Skeet Dec 29 '13 at 09:16
  • But that implies multiple variables, which isn't necessarily what you have. Call it a matter of taste. You like your analogy; I dislike it. It's no big deal. – Dawood ibn Kareem Dec 29 '13 at 09:17
  • 1
    @DavidWallace You may dislike it, but your argumentation doesn't stand: *the address itself* is the reference, not the physical ink marks on the paper. It exists without the paper or ink. But anyway, anonymous instances of the reference can't be durable therefore don't really need to be taken into account when reasoning about GC. – Marko Topolnik Dec 29 '13 at 10:03
1

No you do not need to set it to null. You can just overwrite the value. The JVM knows when a object has no more references to it. After that it is automatically garbage collected when the Garbage Collect is triggered.

How garbage collection works is best explained in the official documentation

The answer to which items are eligible for Garbage collection can be found on this SO post

Community
  • 1
  • 1
Alex
  • 2,953
  • 1
  • 27
  • 37