As soon as A
, B
and C
are out of scope, these are eligible to be garbage collected. In your example it means at the end of the for
statement.
Now if these are not referenced by any other variables, you can make them eligible to be garbage collected right now after using it and without waiting for the end of the for
statement by setting them to null
;
A = new int[100];
B = new int[100];
C = boolean[100];
// processing where these are required
...
// processing where these are not required
A = null;
B = null;
C = null;
It doesn't mean that it will free the memory right now but at least these objects will be considered as unused in the next collection of the GC.
EDIT
Recent JVM versions performs multiple optimizations at runtime (JIT).
You can found on the Oracle website some explanations about Just-In-Time Compilation and Optimization.
You can also found on the same page, some examples of JRockit optimizations runtime.
So, according to the used JVM versions, the assignment to null
may be useless as the JVM may consider the objects as out of scope as soon as these are not used any longer in their scope, so before the end of the for
statement.
The Stephen's answer to a close enough question suggests it.
Unfortunately, I have not found any concrete information about this specific optimization.