当前位置:网站首页>JVM memory model
JVM memory model
2022-07-07 01:44:00 【Choice~】
Author's brief introduction : Creators in the field of Internet of things , Alibaba cloud expert Blogger Huawei cloud sharing expert
️ Personal home page :Choice~
Maxim : But just because it's difficult , It's worth it !
Series column :
1️⃣ C/C++
2️⃣ C And a pointer
3️⃣ Linux
4️⃣ Data structure and algorithm
5️⃣ JavaScript From entry to mastery
6️⃣ 101 Algorithm JavaScript describe
JAVA The gist of is its famous WOTA:“ Write once , Run anywhere ”. In order to apply it ,Sun Microsystems Created Java virtual machine , This is interpreted as compiled Java The basis of the code, the abstraction of the operating system .JVM yes JRE(Java Runtime environment ) Core components , Yes for running Java Code , But now by other languages (Scala,Groovy,JRuby,Closure…) Use .
In this paper , I will focus on JVM As described in the specification Run time data area . These areas are designed to store programs or JVM Data used by itself . I'll start with JVM Overview , Then there is what the bytecode is , And end with different data areas .
Global Overview
JVM Is the abstraction of the underlying operating system . It ensures that the same code will run with the same behavior , No matter what JVM On what hardware or operating system . for example :
- No matter what JVM Whether in 16 position /32 position /64 Running on a bit operating system , Primitive type int The size of will always be from -2^31 To 2^31-1 Of 32 Bit signed integer .
- Every JVM All in big order ( High byte takes priority ) Store and use data in memory , Regardless of the underlying operating system / Is the hardware big end or small end sequence .
Be careful : Sometimes ,JVM The behavior of implementation is different from that of another JVM Make a difference , But usually the same .
The figure below shows JVM Overview :
- JVM explain The source code of the compiled class Generated Bytecode . Although the term JVM representative “Java virtual machine ”, But it can run other languages , Such as scala or groovy, As long as they can be compiled into java Bytecode .
- To avoid disk I/O, Bytecode consists of Class loader loads To JVM in . This code will remain in memory , until JVM Stop or class loader ( Load it ) Be destroyed .
- then , Loaded code from perform Engine interpretation and execution .
- The execution engine needs to store data , It's like a pointer to the executing code . It also needs to store the data processed in the developer code .
- The execution engine is also responsible for processing the underlying operating system .
Be careful : many JVM The implementation execution engine will not always interpret bytecode , Instead, compile bytecode into native code ( If you use ). It is called Just In Time(JIT) compile , Greatly accelerated JVM The speed of . The compiled code is temporarily stored in what is often called Area of code cache in . Because the area is not JVM Specification , So I won't discuss it in the rest of this article .
Stack based architecture
JVM Use a stack based architecture . Although it is invisible to developers , But it's for the generated bytecode and JVM Architecture has a huge impact , That is why I will briefly explain this concept .
JVM Through execution Java The basic operations described in bytecode to execute the developer's code ( We will see it in the next chapter ). The operand is the value of the instruction operation . according to JVM standard , These operations are required through a process called Stack of operand stack Pass parameters .
for example , Let's take 2 Basic addition of integers . This operation is called iadd( about integer addition). If you want to add 3 and 4:
- He first pushes 3 and 4.
- And then call iadd Instructions .
- iadd The last... Will pop from the operand stack 2 It's worth .
- int result (3 + 4) Pushed to the operand stack , For other operations .
This way of working is called stack based architecture . There are other ways to handle basic operations , for example , Register based architecture stores operands in small registers , Not in the stack . This register based architecture consists of desktop / The server (x86) Processor and previous Android virtual machine Dalvik Use .
Bytecode
because JVM Interpret bytecode , So it's useful to know what it is before going deep .
java Bytecode is converted into a set of basic operations java Source code . Each operation consists of a byte representing the instruction to be executed ( be called opcode or Operation code ) And zero or more bytes for passing parameters ( But most operations use operand stacks to pass parameters ). stay 256 A possible one byte long opcode ( Slave value 0x00 To hexadecimal 0xFF) in , Yes 204 Currently in java8 Used in specifications .
The following is a list of different types of bytecode operations . For each category , I added a small description and hex range of operation code :
- Constant : Used to remove values from the constant pool ( We'll see it later ) Or push from a known value to the operand stack . From value 0x00 To 0x14
- load : Used to load values from local variables into the operand stack . From value 0x15 To 0x35
- Storage : Used to store the operand stack in local variables . From value 0x36 To 0x56
- Stack : Used to process operand stack . From value 0x57 To 0x5f
- Math: Used to perform basic mathematical operations on the values in the operand stack . From value 0x60 To 0x84
- transformation : Used to convert from one type to another . From value 0x85 To 0x93
- Compare : Used for basic comparison between two values . From value 0x94 To 0xa6
- control : Basic operation , If you go to , return ,… Allow more advanced operations , Such as a loop or function that returns a value . From value 0xa7 To 0xb1
- quote : Used to allocate objects or arrays , Get or check the object , Method or static method reference . It is also used to call ( static state ) Method . From value 0xb2 To 0xc3
- Expand : Operations in other categories added later . From value 0xc4 To 0xc9
- Retain : For each Java Virtual machines are used internally .3 It's worth :0xca、0xfe and 0xff.
this 204 The operation is very simple , for example :
- Operands ifeq (0x99 ) Check 2 Are values equal
- Operands iadd (0x60) add to 2 It's worth
- Operands i2l (0x85) Convert an integer to a long integer
- Operands The length of the array (0xbe) Give the size of the array
- Operands pop (0x57) Pop the first value from the operand stack
To create bytecode , Need a compiler ,JDK Standards contained in Java The compiler is javac.
Let's look at a simple addition :
public class Test {
public static void main(String[] args) {
int a =1;
int b = 15;
int result = add(a,b);
}
public static int add(int a, int b){
int result = a + b;
return result;
}
}
“javac Test.java” Command in Test.class Generate a bytecode in . because java Bytecode is binary code , So humans can't read it .Oracle In its JDK Provides a tool in javap, This tool converts binary bytecode into JVM Human readable markup operation code set in the specification .
command “javap -verbose Test.class” Give the following results :
Classfile /C:/TMP/Test.class
Last modified 1 avr. 2015; size 367 bytes
MD5 checksum adb9ff75f12fc6ce1cdde22a9c4c7426
Compiled from "Test.java"
public class com.codinggeek.jvm.Test
SourceFile: "Test.java"
minor version: 0
major version: 51
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #4.#15 // java/lang/Object."<init>":()V
#2 = Methodref #3.#16 // com/codinggeek/jvm/Test.add:(II)I
#3 = Class #17 // com/codinggeek/jvm/Test
#4 = Class #18 // java/lang/Object
#5 = Utf8 <init>
#6 = Utf8 ()V
#7 = Utf8 Code
#8 = Utf8 LineNumberTable
#9 = Utf8 main
#10 = Utf8 ([Ljava/lang/String;)V
#11 = Utf8 add
#12 = Utf8 (II)I
#13 = Utf8 SourceFile
#14 = Utf8 Test.java
#15 = NameAndType #5:#6 // "<init>":()V
#16 = NameAndType #11:#12 // add:(II)I
#17 = Utf8 com/codinggeek/jvm/Test
#18 = Utf8 java/lang/Object
{
public com.codinggeek.jvm.Test();
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 3: 0
public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=4, args_size=1
0: iconst_1
1: istore_1
2: bipush 15
4: istore_2
5: iload_1
6: iload_2
7: invokestatic #2 // Method add:(II)I
10: istore_3
11: return
LineNumberTable:
line 6: 0
line 7: 2
line 8: 5
line 9: 11
public static int add(int, int);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=3, args_size=2
0: iload_0
1: iload_1
2: iadd
3: istore_2
4: iload_2
5: ireturn
LineNumberTable:
line 12: 0
line 13: 4
}
Can be read .class Indicates that bytecode contains more than java Simple transcription of source code . It contains :
- Description of constant pool of class . The constant pool is JVM One of the data areas of , It stores metadata about classes , For example, the name of the method , Parameters … When a class is in JVM When loading in , This part enters the constant pool .
- image LineNumberTable or LocalVariableTable Such information , Used to specify the location of the function ( In bytes ) And the position of its variables in the bytecode .
- Developer's java Code ( Plus hidden constructors ) Transcription in bytecode .
- Handle specific operations of the operand stack , More broadly, it is the way to deal with passing and obtaining parameters .
For reference only , The following is stored in .class A brief description of the information in the document :
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
Run time data area
The runtime data area is an in memory area for storing data . These data are generated by the developer's program or JVM For its internal work .
This picture shows JVM Overview of different runtime data areas in . Some areas are unique to other areas of each thread .
Pile up
Heap is all Java Memory area shared between virtual machine threads . It is created when the virtual machine starts . All classes example and Array It's all in the pile Distribute ( Use new Operator ).
MyClass myVariable = new MyClass();
MyClass[] myArrayClass = new MyClass[1024];
This area must be Garbage collector management , In order to delete the instance assigned by the developer when it is no longer used . The strategy for cleaning up memory depends on JVM Realization ( for example ,Oracle Hotspot Provides a variety of algorithms ).
Heaps can be dynamically expanded or shrunk , And can have a fixed minimum and maximum size . for example , stay Oracle Hotspot in , Users can use Xms and Xmx Parameter specifies the minimum size of the heap “java -Xms=512m -Xmx=1024m…”
Be careful : The maximum size of the heap cannot be exceeded . If the limit is exceeded ,JVM Will throw a OutOfMemoryError.
Method area
The method area is all Java Memory shared between virtual machine threads . It is created when the virtual machine starts , from Class loader Load from bytecode . As long as the class loader in the load method area is active , They will remain in memory .
Method area storage :
- Class information ( Field / Number of methods 、 Superclass name 、 The interface name 、 Version, etc )
- Bytecode for methods and constructors .
- The runtime constant pool for each loaded class .
The specification does not force the implementation of method areas in the heap . for example , stay JAVA7 Before ,Oracle HotSpot Use a name called PermGen To store the method area . This PermGen And Java Pile up ( And from... Like a pile JVM Managed memory ) Is a continuous , And is limited to the default space 64Mo( By the parameter -XX:MaxPermSize modify ). from Java 8 Start ,HotSpot Now store the method area in a file called Metaspace In a separate native memory space , The maximum available space is the total available system memory .
Be careful : The maximum size of the method area cannot be exceeded . If the limit is exceeded ,JVM Will throw a OutOfMemoryError.
Runtime constant pool
This pool is a sub part of the method area . Because it is an important part of metadata , therefore Oracle Specifications except “ Method area ” outside , It also describes the runtime constant pool . For each loaded class / Interface , This constant pool will increase . This pool is like the symbol table of traditional programming languages . let me put it another way , When referencing classes 、 Method or field ,JVM Search the actual address in memory by using the runtime constant pool . It also contains constant values , Such as a string litteral Or constant primitive .
String myString1 = “This is a string litteral”;
static final int MY_CONSTANT=2;
PC register ( Every thread )
Each thread has its own pc( Program counter ) register , Create with thread . At any time , Every Java The virtual machine thread is executing the code of a single method , That is, the thread The current method of .pc The register contains the currently executing Java Virtual machine instructions ( In the method area ) The address of .
notes : If the method that the thread is currently executing is native , be Java The virtual machine pc The value of the register is undefined .Java The virtual machine pc The register is wide enough , It can be saved on a specific platform returnAddress Or local pointer .
Java Virtual machine stack ( Every thread )
The stack area stores multiple frames , So before we talk about stacks , I will introduce these frames .
frame
A frame is a data structure , It contains multiple data , These data indicate The current method ( Called method ) The state of the thread in :
Operand stack : I have introduced the operand stack in the chapter on stack based architecture . This stack is used by bytecode instructions to process parameters . This stack is also used in (java) Pass parameters in method call , And get the result of the called method at the top of the stack of the calling method .
Array of local variables : This array contains all local variables in the scope of the current method . This array can hold primitive types 、 Reference or return the value of the address . The size of this array is calculated at compile time .Java Virtual machines use local variables to pass parameters during method calls , The array of called methods is created from the operand stack of the calling method .
Runtime constant pool reference : For the current Method **** Current class of Constant pool reference .JVM Use it to put the symbol method / Variable references ( for example :myInstance.method()) Convert to actual memory reference .
Fold
Every Java Virtual machine threads have a private Java Virtual machine stack , Create at the same time as the thread .Java The virtual machine stack stores frames . Every time a method is called , Will create a new frame and put it on the stack . When the method call of the frame is completed , Whether the completion is normal or sudden ( It will throw an uncaught exception ), Frames will be destroyed .
There's only one frame ( The frame of the execution method ) Is active at any point in a given thread . This frame is called The current frame , The method is called The current method . The class in which the current method is defined is The current class . Operations on local variables and operand stacks usually refer to the current frame .
Let's look at the following example , This is a simple addition
public int add(int a, int b){
return a + b;
}
public void functionA(){
// some code without function call
int result = add(2,3); //call to function B
// some code without function call
}
The following is when function A() When running, it is JVM How to work in :
Internal function A() frame A Is the top of the stack frame , It's the current frame . Add in the internal call () when , A new frame ( frame B) Placed on the stack . frame B Become the current frame . frame B The array of local variables is through the pop-up frame A Operand stack to fill . When add() After completion , frame B Will be destroyed , frame A Become the current frame again .add() The results of Frame A On the operand stack of , In order to functionA() You can use it by popping its operand stack .
Be careful : The functionality of this stack makes it dynamically scalable and contractible . There is a maximum size that the stack cannot exceed , This limits the number of recursive calls . If the limit is exceeded ,JVM Will throw a StackOverflowError.
Use Oracle HotSpot, You can use parameters -Xss Specify this limit .
Native method stack ( Threads )
This is a use Java Stack of native code written in languages other than , And pass JNI(Java Native interface ) call . Because it's a “ This machine ” Stack , Therefore, the behavior of this stack depends entirely on the underlying operating system .
Conclusion
I hope this article can help you better understand JVM. in my opinion , The trickiest part is JVM Stack , Because it is related to JVM Is closely related to the internal functions of .
- If it's helpful , Please support the third company !
- If you have any questions, please leave a message in the comment area , Help everyone solve it in time !
边栏推荐
- Long press the button to execute the function
- 机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。
- Yunna | work order management software, work order management software app
- Appium automation test foundation uiautomatorviewer positioning tool
- 设置Wordpress伪静态连接(无宝塔)
- Public key \ private SSH avoid password login
- 爬虫实战(六):爬笔趣阁小说
- Vocabulary in Data Book
- Set WordPress pseudo static connection (no pagoda)
- Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
猜你喜欢
随机推荐
mysqlbackup 还原特定的表
Gin 入门实战
7.6 simulation summary
使用nodejs完成判断哪些项目打包+发版
MySQL最基本的SELECT(查询)语句
Dark horse notes - exception handling
AcWing 361. 观光奶牛 题解(spfa求正环)
LeetCode:1175. Prime permutation
IDEA常用的快捷键
Make Jar, Not War
Mysqlbackup restores specific tables
Public key \ private SSH avoid password login
Comparison of picture beds of free white whoring
First experience of JSON learning - the third-party jar package realizes bean, list and map to create JSON format
The cradle of eternity
Appium自动化测试基础 — uiautomatorviewer定位工具
JS how to quickly create an array with length n
Hutool post requests to set the body parameter to JSON data
Use nodejs to determine which projects are packaged + released
从底层结构开始学习FPGA----FIFO IP的定制与测试