0

Consider a simple while loop like below

public static void main(String[] args) {
   int i = 6;
   int k = 10;
   while(i<k) {
      int v = 5;
      i++;
   }
}

Is it true that the space for variable v is allocated only when the while loop is being executed and de-allocated when the loop ends?

Or does it stay in the memory regardless of the termination or execution of the while loop?

Is it any different if it is not a primitive data type(any kind of object)?

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Hello
  • 286
  • 3
  • 18
  • In this case there won't be any memory allocation because the compiler most likely will not include an unused variable into the compiled bytecode. – f1sh Apr 04 '16 at 12:28
  • so there won't be any memory allocation at all for the variable v? even if the while loop is being executed? Then what if the int v was used for some function, how would it gain access to the value it holds? – Hello Apr 04 '16 at 12:29
  • How would it be possible for v to store a value without memory allocation? Variables live in their scopes (where they are declared) and available for garbage collection when their scope ends. – Areca Apr 04 '16 at 12:30
  • @Areca so is my first statement correct? is it true? – Hello Apr 04 '16 at 12:34
  • 1
    What do you mean by allocation? For a primitive data type like your example, the variable will be allocated on the stack and then deallocated on the stack with little overhead (see http://stackoverflow.com/questions/3646632/do-java-primitives-go-on-the-stack-or-the-heap). If it was an object created with `new` then there would be a dynamic allocation. – pcarter Apr 04 '16 at 12:35
  • @Areca read f1sh's comment carefully: since `v` is not used at all the compiler will probably just remove it so there won't be any memory needed. Besides that, if it were not removed the memory would be allocated on the stack (since it's a local variable) and reused on every iteration. – Thomas Apr 04 '16 at 12:35
  • @Thomas What does 'compiler will probably remove it' mean? Compiler removes or not? – Areca Apr 04 '16 at 12:37
  • @Areca that depends on the compiler and settings. If the compiler is not required to include debug information it may conclude that removing statement `int v = 5;` has no side effect since `v` is never used and thus is free to remove it in the compiled code altogether. – Thomas Apr 04 '16 at 12:39
  • It depends on the implementation. Different Java implementation may well not behave the same. Most will make some smart choice, though. So I wouldn’t worry. At least not until I see a performance issue, and even by then there will likely be other places I can gain a greater performance improvement. – Ole V.V. Apr 04 '16 at 12:41

1 Answers1

4

JVM creates a new frame when a method is invoked. A frame contains an array of local variables. The number of the variables is determined at compiled time.

In this case, the frame for the method main will contain an array of 4 local variables: args, i, k, and v.

dejvuth
  • 6,986
  • 3
  • 33
  • 36
  • Is "Frame" considered as some kind of memory? So would my first statement be true or false? "Is it true that the space for variable v is allocated only when the while loop is being executed and de-allocated when the loop ends?" – Hello Apr 04 '16 at 12:39
  • 2
    @LookAtTheBigPicture read it carefully: the frame will reside in memory (although not the heap) but since the frame is for the entire method it will contain `v` (if the compiler decides so) regardless of whether the loop runs or not. – Thomas Apr 04 '16 at 12:41
  • @Thomas thank you for the clarification. I think I got it now :). – Hello Apr 04 '16 at 12:44
  • A frame is created when a method is invoked, so no, the space for `v` is not allocated only when the while loop is being executed. – dejvuth Apr 04 '16 at 12:44