Does variable = null point to garbage collection

Help me resolve a dispute with a colleague: Does a variable or collection set to null in Java in garbage collection and reduce memory usage? If I have a long running program and each function can be iteratively called (potentially thousands of times): Does all the variables in it set to null before returning the value of the parent function, helps reduce heap size / memory usage?

+47
java performance memory-management memory
May 28 '10 at 17:28
source share
8 answers

This is an old idea of โ€‹โ€‹performance. This was true for 1.0 days, but the compiler and JVM were improved to eliminate the need (if ever). This great IBM article goes into detail if you're interested: Java theory and practice: garbage collection and performance

+57
May 28 '10 at 17:33
source share

From the article:

There is one case where the use of explicit zeroing is not only useful, but also practically necessary, and it is there that the reference to the object has a wider scope than is used or considered valid according to the specification of the program. This includes cases such as using a static field or instance to store a reference to a temporary buffer rather than a local variable, or using an array to store references that may remain available to the runtime, but not by the implied semantics of the program.

Translation: "obviously null" persistent objects that are no longer needed. (If you want. "Practically necessary" statement too strong?)

+7
May 28 '10 at 19:28
source share

Java VM Specification

12.6.1 Implementation of completion Each object can be characterized by two attributes: it can be accessed, the finalizer is available or inaccessible, and it can also be non-finalized, finalized or finalized.

A reachable object is any object that can be accessed in any potential ongoing computation from any live stream . You can optimize program conversions that reduce the number of objects that are achievable, less than those that are naively considered accessible. For example, a compiler or code generator may choose to set a variable or parameter that will no longer be used for a null value in order to more quickly restore the potential storage of such an object earlier.

Discussion

Another example of this occurs if the values โ€‹โ€‹in the fields of objects are stored in registers. Then the program can access the registers instead of the object and never access the object again. This would mean that the object is garbage.

An object is available if it can be involved in any potential ongoing calculation. Therefore, if your code refers to a local variable, and nothing else refers to it, you can force the object to assemble by setting it to null. This will either result in a null pointer exception, or change the behavior of your program, or if you donโ€™t have it either, then you do not need a variable in the first place.

If you nullify a field or an element of an array, this may make sense for some applications, and this will lead to the fact that the memory will be fixed faster. Once a case creates a large array to replace the existing array referenced by the field in the class, if the field is in zero order before creating the replacement, it can reduce the pressure on the memory.

Another interesting feature of Java is that the region does not appear in class files, so the region is not related to reachability; these two methods create the same bytecode, and therefore, the virtual machine does not see the area of โ€‹โ€‹the created object at all:

static void withBlock () { int x = 1; { Object a = new Object(); } System.out.println(x+1); } static void withoutBlock () { int x = 1; Object a = new Object(); System.out.println(x+1); } 
+5
May 28 '10 at 17:42
source share

Not necessary. An object becomes suitable for garbage collection when there are no more live streams that contain a reference to the object.

Local variables go out of scope when the method returns, and it makes no sense to point the local variables to zero - the variables disappear in any case, and if there is nothing that contains a reference to the objects referenced by the variables, then these objects become suitable for garbage collection.

The key is not to look only at the variables, but to the objects referenced by these variables, and find out where these objects refer to your program.

+3
May 28 '10 at 17:29
source share

This is useless for local variables, but it may be useful / necessary to clear instance variables that are no longer needed (for example, after initialization).

(Yes, I know how to apply the Builder pattern ...)

+2
May 28 '10 at 17:37
source share

This may only make sense in the following scenario:

 public void myHeavyMethod() { List hugeList = loadHugeListOfStuff(); // lots of memory used ResultX res = processHugeList(hugeList); // compute some result or summary // hugeList = null; // we are done with hugeList ... // do a lot of other things that takes a LOT of time (seconds?) // and which do not require hugeList ... } 

Here it may bring some benefit to uncomment the string hugeList = null , I think.

But it would be wiser to rewrite the method (perhaps refactoring into two, or specifying the internal volume).

+1
May 28 '10 at 17:43
source share

Setting the object reference to null only makes it acceptable for garbage collection. This does not necessarily free up memory, which depends on when the garbage collector (which depends on the JVM) is running. When the garbage collector is running, it frees the heap by deleting only those objects that are allowed to collect garbage.

0
Jun 14 '15 at 12:25
source share

Good to have. When you set objects to null, there is a chance that the object can be assembled faster in the immediate GC loop. But there is no guaranteed mechanism for garbage collection of an object at a given time.

-four
May 28 '10 at 17:37
source share



All Articles