当前位置:网站首页>JVM (6): slot variable slot, operand stack, code trace, stack top cache technology
JVM (6): slot variable slot, operand stack, code trace, stack top cache technology
2022-06-11 04:17:00 【Prince you】
One 、Slot Understanding of variable slots
Parameter values are always stored in the local variable array index0 Start , To array length -1 End of index of .
Local variable table , The most basic storage unit is slot( Variable slot )
The local variable table stores various basic data types known at compile time (8 Kind of ), Reference type (reference), returnAddress Variable of type .
In the local variable table ,32 Types within bits occupy only one slot( Include returnAddress type ),64 Bit type (long and double) Take two slot.
- byte 、short 、 char Converted to before storage int,boolean Also converted to int,0 Express false , Not o Express true.
- long and double They occupy two slot.

Slot Reuse of waste
The slots in the local variable table in the stack frame can be reused , If a local variable goes beyond its scope , Then the new local variable declared after its scope is likely to reuse the slot of the expired local variable , So as to save resources .
public class SlotTest {
public void localVal1() {
int a = 0;
System.out.println(a);
int b = 0;
}
public void localVal2() {
{
int a = 0;
System.out.println(a);
}
// here b It will be reused a Slot of
int b = 0;
}
}Two 、 The comparison between static variables and local variables
After the parameter list is assigned , According to the order and scope of the variables defined in the method body .
We know that class variable table has two initialization opportunities , For the first time “ Preparation stage ”, Perform system initialization , Set a zero value on a class variable , The other was in “ initialization ” Stage , Give the programmer the initial value defined in the code .
Unlike class variable initialization , There is no system initialization process in the local variable table , This means that once a local variable is defined, it must be artificially initialized , Otherwise it won't work .
public void test(){
int i;
System.out.println(i);
}This code is wrong , Cannot use without assignment .
Additional explanation
In the stack frame , The most relevant part of performance tuning is the local variable table mentioned earlier . When the method executes , The virtual machine uses the local variable table to complete the method transfer .
Variables in the local variable table are also important garbage collection root nodes , As long as the objects directly or indirectly referenced in the local variable table are not recycled .
3、 ... and 、 The stack of operands
Each independent stack frame contains a list of local variables , One in and one out (Last-In-First-out) The stack of operands , It can also be called an expression stack (Expression stack) .
The stack of operands , During method execution , According to bytecode instruction , Write or extract data from the stack , That's the stack (push)/ Out of the stack (pop).
- Some bytecode instructions push values into the operands stack , The rest of the bytecode instructions take the operands out of the stack . Use them and push the results on the stack .
- such as : Perform replication 、 In exchange for 、 Sum and so on


The stack of operands , It is mainly used to save the intermediate results of the calculation process , At the same time, it serves as the temporary storage space for variables in the calculation process .
The stack of operands is JVM A workspace of the execution engine , When a method is first executed , A new stack frame will also be created , The operand stack of this method is empty .
Each stack of operands has a clear stack depth for storing values , The maximum depth required is defined at compile time , Save in the method of code Properties of the , by max_stack Value .
Any element in the stack can be arbitrary Java data type .
- 32bit The type occupies a stack unit depth
- 64bit The type of the stack occupies two stack units of depth
The operand stack does not use the way of access index to access data , But only through the standard entry branch (push) And out of the stack (pop) Operation to complete a data access .
If the called method has a return value , Its return value will be pushed into the stack of operands in the current stack frame , And update the PC The next bytecode instruction to be executed in the register .
The data type of the elements in the operand stack must strictly match the sequence of bytecode instructions , This is verified by the compiler during the compiler , At the same time, in the class loading process, the data flow analysis phase of the class verification phase needs to be verified again .
in addition , We said Java The interpretation engine of virtual machine is stack based execution engine , The stack refers to the operand stack .
Four 、 Code tracking





5、 ... and 、 Stack top caching technology
I mentioned it earlier , Virtual machines based on trestle architecture use more compact zero address instructions , But when completing an operation, it is necessary to use more stack in and out instructions , This also means that more instruction dispatch will be required (instruction dispatch) Times and memory reads / Write times .
Because the operands are stored in memory , Therefore, memory reads are performed frequently / Write operations will inevitably affect the speed of execution . To solve this problem ,HotSpot JVM The designers of the stack top cache (Tos,Top-of-stack Cashing) technology , Cache all stack top elements in physical cPu In the register of , In order to reduce the memory read / Write times , Improve the execution efficiency of the execution engine .
边栏推荐
猜你喜欢

Programming battle -- challenging college entrance examination questions

mysql存储过程

三层带防护内网红队靶场

Explain in detail the structure and working principle of the crystal oscillator

【网络篇】套接字编程

Some differences between people
![[laser principle and application-2]: key domestic laser brands](/img/55/a87169bb75429f323159e3b8627cc6.jpg)
[laser principle and application-2]: key domestic laser brands

Embedded basic interface-i2s

Given a project, how will you conduct performance testing?

Guanghetong won the "science and Technology Collaboration Award" of Hello travel, driving two rounds of green industries to embrace digital intelligence transformation
随机推荐
数据类型的转换和条件控制语句
Esp32 development -lvgl display picture
Sslstrip Ultimate - location hijacking
七个好用的装饰器
Market prospect analysis and Research Report of seed laser in 2022
Market prospect analysis and Research Report of engraving laser in 2022
Unity prefab scene conflict merge tool unityyamlmerge
JVM(2):内存结构、类的加载过程
Market prospect analysis and Research Report of integrated scanner in 2022
密码找回功能可能存在的问题(补充)
Market prospect analysis and Research Report of modular lithium ion battery in 2022
华生·K的秘密日记
Some differences between people
SQL injection correlation analysis
使用工具类按一定规则读取Excel文件
B - wall painting (C language)
详解 | 晶振的构造及工作原理
雷达辐射源调制信号仿真(代码)
QT日志模块的个性化使用
Market prospect analysis and Research Report of nitrogen liquefier in 2022