Saturday 15 March 2014

java - Do unused local variables in a method acquire memory in JVM? -



java - Do unused local variables in a method acquire memory in JVM? -

i came across post in do uninitialized primitive instance variables utilize memory?

it states "in java, cost memory declare class level instance variable without initializing it? example: int i; utilize memory if don't initialize = 5;?"

my question in case of local variables, have method foo()

public int foo(){ int x; //write code not use/initialize x }

will local variable x occupy memory?

edit

jon's reply

update: doing bit more research on this, find page suggests me that, although compiled bytecode implies space allocated x, may indeed optimized away jvm. unfortunately, find no finish description of optimizations performed. particularly, jvm documentation chapter on compiling not mention removing unused variables stack. so, barring farther discoveries, reply it's implementation-dependent, seems sort of optimization self-respecting compiler perform. notice doesn't matter much local variable rather field - in fact, local variables ones optimized away, since easiest analyze , eliminate. (precisely because local)

let see if can find more evidences supports this.

this kind of question that's worth examining javap.

public class foo { public int bar(){ system.out.println("foo"); homecoming 8; } public int foo(){ int x; system.out.println("foo"); homecoming 8; } }

notice difference between foo() , bar() 1 declares local variable x , other not.

now @ jvm code (use javap -v foo see on machine)

public int bar(); descriptor: ()i flags: acc_public code: stack=2, locals=1, args_size=1 0: getstatic #2 // field java/lang/system.out:ljava/io/printstream; 3: ldc #3 // string foo 5: invokevirtual #4 // method java/io/printstream.println:(ljava/lang/string;)v 8: bipush 8 10: ireturn linenumbertable: line 6: 0 line 7: 8 public int foo(); descriptor: ()i flags: acc_public code: stack=2, locals=2, args_size=1 0: getstatic #2 // field java/lang/system.out:ljava/io/printstream; 3: ldc #3 // string foo 5: invokevirtual #4 // method java/io/printstream.println:(ljava/lang/string;)v 8: bipush 8 10: ireturn linenumbertable: line 12: 0 line 13: 8 }

the interesting thing line-by-line output identical, locals bar 1, , foo it's 2. looks space indeed allocated x, though compiler output doesn't ever utilize it.

update: doing bit more research on this, find this page suggests me that, although compiled bytecode implies space allocated x, may indeed optimized away jvm. unfortunately, find no finish description of optimizations performed. particularly, jvm documentation chapter on compiling not mention removing unused variables stack. so, barring farther discoveries, reply it's implementation-dependent, seems sort of optimization self-respecting compiler perform. notice doesn't matter much local variable rather field - in fact, local variables ones optimized away, since easiest analyze , eliminate. (precisely because local)

java

No comments:

Post a Comment