Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicit nulling

Tags:

java

In what situations in java is explicit nulling useful. Does it in any way assist the garbage collector by making objects unreachable or something? Is it considered to be a good practice?

like image 386
baskin Avatar asked Mar 25 '09 07:03

baskin


People also ask

What is null in memory?

The value 0 (all bits at zero) is a typical value used in memory to denote null . It means that there is no value associated with name . You can also think of it as the absence of data or simply no data.

What is null in Java example?

In Java, null is a keyword much like the other keywords public, static or final. It is just a value that shows that the object is referring to nothing. The invention of the word “null” originated to denote the absence of something. For example, the absence of the user, a resource, or anything.

Why null is used in Java?

Null serves as the default value of any uninitialized reference variable, including instance variables and static variables (although you will still receive a compiler warning for uninitialized local variables).

Does null mean zero Java?

NULL means an absent value. Zero is a defined int with value 0 NULL is a undefined variable contains no value.


2 Answers

In Java it can help if you've got a very long-running method, and the only reference to the an object is via a local variable. Setting that local variable to null when you don't need it any more (but when the method is going to continue to run for a long time) can help the GC. (In C# this is very rarely useful as the GC takes "last possible use" into account. That optimization may make it to Java some time - I don't know.)

Likewise if you've got a member field referring to an object and you no longer need it, you could potentially aid GC by setting the field to null.

In my experience, however, it's rarely actually useful to do either of these things, and it makes the code messier. Very few methods really run for a long time, and setting a variable to null really has nothing to do with what you want the method to achieve. It's not good practice to do it when you don't need to, and if you do need to you should see whether refactoring could improve your design in the first place. (It's possible that your method or type is doing too much.)

Note that setting the variable to null is entirely passive - it doesn't inform the garbage collector that the object can be collected, it just avoids the garbage collector seeing that reference as a reason to keep the object alive next time it (the GC) runs.

like image 170
Jon Skeet Avatar answered Sep 30 '22 19:09

Jon Skeet


In general it isn't needed (of course that can depend on the VM implementation). However if you have something like this:

private static final Map<String, String> foo;

and then have items in the map that you no longer need they will not be eligible for garbage collection so you would need to explicitly remove them. There are many cases like this (event listeners is another area that this can happen with).

But doing something like this:

void foo()
{
    Object o;

    // use o

    o = null; // don't bother doing this, it isn't going to help
}

Edit (forgot to mention this):

If you work at it, you should find that 90-95% of the variables you declare can be made final. A final variable cannot change what it points at (or what its value is for primitives). In most cases where a variable is final it would be a mistake (bug) for it to receive a different value while the method is executing.

If you want to be able to set the variable to null after use it cannot be final, which means that you have a greater chance to create bugs in the code.

like image 33
TofuBeer Avatar answered Sep 30 '22 19:09

TofuBeer