4

So here is the example:

LocalVariableTable:
        Start  Length  Slot  Name   Signature
               0     133     0  this   Lcom/my/class/Test;
               2     131     1     a   I
               4     129     2     b   I
               7     126     3     i   I
              10     123     4    i2   I
              16     117     5    o1   Ljava/lang/Integer;
              31     102     6    o2   Ljava/lang/Integer;

What does start and length mean? Why does length have the value it has? Why length is different for the equal types (Integer)? Why length could be changed, when I add something to class and recompile it without touching that particular local variable?

dhblah
  • 9,751
  • 12
  • 56
  • 92

2 Answers2

11

Start is the start bytecode offset where this variable is visible. Length is number of bytecode bytes during which this variable is visible. Usually start points to the bytecode instruction where the variable is first assigned or to 0 for method parameters and this. In your case it seems that all variables are valid to the end of the method (start+length = 133 for every variable), but if you declare some variables inside blocks, their scope will be shorter.

Note that local variables table (LVT) is an optional debugging information. It's not necessary for program execution and can be switched off using -g:none during the compilation. The main purpose of this table is to make debugging more convenient: having it you can determine for each bytecode positions which variables are currently visible to display them in variables pane and hide them once you step out of the variable scope. Also this table is used by java decompilers and code analyzers like FindBugs.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
  • Thank you very much for your answer. If you don't mind, I'd like to ask another question based on your answer. If you're saying that LVT is optional and it's purpose is only for debugging, then how java op-codes that take local variable index as a parameter (e.g. aload) are working then? Is local variables information stored somewhere else when there is no LVT? – dhblah May 07 '15 at 16:21
  • 1
    First slots are automatically used by `this` (for non-static methods) and method parameters (2 slots per `long`/`double`, 1 slot for other types), this can be decided by method signature. After that if JVM sees a store instruction for new slot, it simply allocates this slot (or two slots for `lstore`/`dstore`) and sets its type based on the opcode. In verified bytecode there should be no load instruction which loads from the unknown slot (not taken by method arguments or previous store instruction). – Tagir Valeev May 07 '15 at 16:28
  • so basically you're saying that though LVT could be discarded at compile time, it serves only Informational purpose. And all the data concerning local variables, that is needed by the JVM is stored in the array of local variables in the stack frame. So, local variables can be addressed by their index in that array, while LVT contains only supplemental information to show\hide local variable at the correct step in a debugging pane during debugging. – dhblah May 08 '15 at 09:09
  • Yes, exactly. You may switch the debugging information off and see that your application still works normally, but you will unlikely to see the local variables in the debugger. – Tagir Valeev May 08 '15 at 09:12
  • How does the garbage collector differentiate between references and primitives types? – neoexpert Mar 02 '20 at 21:38
1

According to jsl

Inside Local variabale Table

u2 local_variable_table_length;
    {   u2 start_pc;
        u2 length;
        u2 name_index;
        u2 descriptor_index;
        u2 index;
    }

Each entry in the local_variable_table array indicates a range of code array offsets within which a local variable has a value. It also indicates the index into the local variable array of the current frame at which that local variable can be found.

Now for your start and length attribute JSL says that

start_pc, length

The given local variable must have a value at indices into the code 

array in the interval [start_pc, start_pc + length), that is, between start_pc inclusive and start_pc + length exclusive.

The value of start_pc must be a valid index into the code array of this 

Code attribute and must be the index of the opcode of an instruction.

The value of start_pc + length must either be a valid index into 

the code array of this Code attribute and be the index of the opcode of an instruction, or it must be the first index beyond the end of that code array.

So basically starts corresponds to your LineNumberTable

Ankur Anand
  • 3,873
  • 2
  • 23
  • 44