Runtime stack frame structure local variable table explore slot reuse

Posted by briansol on Sun, 05 Apr 2020 00:58:38 +0200

Local variable table

As we know, the start of a method call is equivalent to the stack frame's stack in and out of the Java virtual machine stack.

Stack frame includes local variable table, operand stack, dynamic link, return address, etc.

Figure below: each thread corresponds to a Java virtual machine stack. There are many stack frames in the Java virtual machine stack

(source: csdn- Xiao Bing)

Local variable table of stack frame: the memory area where method parameters and local variables are stored.

java data types include: byte Boolean char short int long float double reference

The minimum unit in the local variable table of stack frame is slot. There is no specific slot size specified in the jvm specification. Usually, in a 32-bit operating system, slot occupies 32 bits, and long/double occupies two slots. In the 64 bit operating system, slot accounts for 64 bits. All data types account for 1 slot.

    

slot multiplexing

When the value of a variable's pc register is larger than the scope of slot, slot can be reused. (copied, I don't understand. So do your own experiments.)

Write a test class aa:

/**
 * Description:Test slot reuse
 * Operation parameters: - verbose:gc
 * Result: large object a will not be recycled by gc
 * Analysis: because object a is reachable in gc (because the local variable table slot1 references object a). So gc doesn't recycle a
 * @author LiuXianfa
 * @date 2018/4/3
 */
public class aa {
    public static void main(String[] args) {
        byte[] a = new byte[60*1024*1024];
		int b= 10;
        System.gc();
    }
}


C:\Users\AnXiaole\Desktop>javac aa.java // This command: compile aa.java
C:\Users\AnXiaole\Desktop>javap -v aa // This command: decompile the aa class under the current classpath
// Constant pools and other information are omitted here
{
// Construction method is omitted here

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=1, locals=3, args_size=1   // Note: the local variable table uses three slots (places = 3). Note: slot0 represents a reference to this object
         0: ldc           #2           // int 62914560
         2: newarray       byte
         4: astore_1                   // Note: put the reference of newarray byte array created in step 2 in slot1 position
         5: bipush        10
         7: istore_2                   // Note: put step 5 in the number 10 of the operand stack, and put it in slot 2
         8: invokestatic  #3           // Method java/lang/System.gc:()V
        11: return
      LineNumberTable:
        line 9: 0
        line 10: 5
        line 11: 8
        line 12: 11
}
SourceFile: "aa.java"
/**
 * Description:Test slot reuse
 * Operation parameters: - verbose:gc
 * Run result: large object a will be recycled by gc
 * Analysis: when gc is triggered, the object a is not reachable (as can be seen from the decompilation line 7, the reference to the a object in the heap in slot1 is covered by [int 10], so when gc is triggered, the a object in the heap becomes an unreachable object). So gc will recycle a
 * @author LiuXianfa
 * @date 2018/4/3
 */
public class aa {
    public static void main(String[] args) {
        {
			byte[] a = new byte[60*1024*1024]; //Notice that there is a code block here. The scope of a is in the code block
		}
		int b= 10;
        System.gc();
    }
}



C:\Users\AnXiaole\Desktop>javac aa.java

C:\Users\AnXiaole\Desktop>javap -v aa
Classfile /C:/Users/AnXiaole/Desktop/aa.class
// Constant pools and other information are omitted here
{
// Construction method is omitted here

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=1, locals=2, args_size=1   // Note: the local variable table uses two slots (locals = 2). Note: slot0 places a reference to this object
         0: ldc           #2           // int 62914560
         2: newarray       byte
         4: astore_1                   // Note: put the reference of newarray byte array created in step 2 in slot1 position
         5: bipush        10
         7: istore_1                   // Note: put step 5 in the number 10 of the operand stack, and put it in slot 1
         8: invokestatic  #3           // Method java/lang/System.gc:()V
        11: return
      LineNumberTable:
        line 10: 0
        line 12: 5
        line 13: 8
        line 14: 11
}
SourceFile: "aa.java"
/**
 * Description:Test slot reuse
 * Operation parameters: - verbose:gc
 * Run result: large object a will not be recycled by gc
 * Analysis: although it is outside the scope of a when it is in gc, a will not be recycled because there is no write operation to the slot (that is, when gc is triggered, the location of slot 1 still points to the application of object a, and the a object in the heap is still reachable).
 * @author LiuXianfa
 * @date 2018/4/3
 */
public class aa {
    public static void main(String[] args) {
        {
			byte[] a = new byte[60*1024*1024];
		}
		// int b= 10; / / commented out here
        System.gc();
    }
}


C:\Users\AnXiaole\Desktop>javac aa.java

C:\Users\AnXiaole\Desktop>javap -v aa
Classfile /C:/Users/AnXiaole/Desktop/aa.class
// Constant pools and other information are omitted here
{
// Construction method is omitted here

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=1, locals=2, args_size=1
         0: ldc           #2                  // int 62914560
         2: newarray       byte
         4: astore_1
         5: invokestatic  #3                  // Method java/lang/System.gc:()V
         8: return
      LineNumberTable:
        line 4: 0
        line 7: 5
        line 8: 8
}
SourceFile: "aa.java"
/**
 * Description:Test slot reuse
 * Operation parameters: - verbose:gc
 * Run result: object a will not be recycled by gc
 * Analysis: when gc is triggered, a is still the reachable object. (local variable table slot1 refers to a object)
 * @author LiuXianfa
 * @date 2018/4/3
 */
public class aa {
    public static void main(String[] args) {
        byte[] a = new byte[60*1024*1024];
		// int b= 10;
        System.gc();
    }
}


C:\Users\AnXiaole\Desktop>javac aa.java
C:\Users\AnXiaole\Desktop>javap -v aa
Classfile /C:/Users/AnXiaole/Desktop/aa.class
// Constant pools and other information are omitted here
{
// Construction method is omitted here

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=1, locals=2, args_size=1
         0: ldc           #2                  // int 62914560
         2: newarray       byte
         4: astore_1
         5: invokestatic  #3                  // Method java/lang/System.gc:()V
         8: return
      LineNumberTable:
        line 3: 0
        line 5: 5
        line 6: 8
}
SourceFile: "aa.java"

 

Encoding proposal

  

 

Reference resources

    In depth understanding of Java virtual machine (jvm performance optimization + memory model + virtual machine principle) Section 96 table of local variables

Topics: Java jvm encoding