Zero local variables can really help in some cases with an edge. This does not apply to the situation in the original question, but in any case is educational ... Consider this program:
public class Main { public static void main(String[] args) { { Main local = new Main();
If inner = null; commented out, the object in the local variable cannot be garbage collected during the while loop. The reason is that the Java Virtual Machine is not aware of such areas. All that he has:
D:\workspaces\workspace-3.4\test\src>javap -verbose -c Main public class Main extends java.lang.Object minor version: 0 major version: 50 Constant pool: const #1 = Method #4.#11; // java/lang/Object."<init>":()V const #2 = class #12; // Main const #3 = Method #2.#11; // Main."<init>":()V const #4 = class #13; // java/lang/Object const #5 = Asciz <init>; const #6 = Asciz ()V; const #7 = Asciz Code; const #8 = Asciz main; const #9 = Asciz ([Ljava/lang/String;)V; const #10 = Asciz StackMapTable; const #11 = NameAndType #5:#6;// "<init>":()V const #12 = Asciz Main; const #13 = Asciz java/lang/Object; { public Main(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: Stack=2, Locals=2, Args_size=1 0: new #2; //class Main 3: dup 4: invokespecial #3; //Method "<init>":()V 7: astore_1 8: goto 8 StackMapTable: number_of_entries = 1 frame_type = 8 /* same */ }
Information about the scope of the local variable is missing. Therefore, from the perspective of the JVM above, the program is equivalent:
public class Main { public Main() { } public static void main(String args[]) { Main main1 = new Main(); do ; while(true); } }
(generated by JAD decompiler)
Conclusion: There are some reasons for resetting local variables in special cases like this. But if the method ends soon (as in my original question), this will not help.
This was inspired by a comment by Zdenek Tronicek on the java-cz mailing list (in Czech, sorry)
Peter Štibraný Feb 24 '09 at 20:16 2009-02-24 20:16
source share