当前位置:网站首页>Summarize the knowledge points of the ten JVM modules. If you don't believe it, you still don't understand it
Summarize the knowledge points of the ten JVM modules. If you don't believe it, you still don't understand it
2022-07-29 03:14:00 【Program dog tapping code】
01 JVM Memory structure
Java The memory space of virtual machine is divided into two parts 5 Parts of :
- Program counter
- Java Virtual machine stack
- Native Method Stack
- Pile up
- Method area
JDK 1.8 Same as JDK 1.7 Than , The biggest difference is : Metadata area replaces permanent generation . The essence and permanence of meta space Generational similarity , All right. JVM Implementation of method area in specification . But the biggest difference between Metaspace and permanent generation is : The metadata space is not in the virtual machine , It's using local memory .
1.1 Program counter (PC register )
(1) Definition of program counter A program counter is a small amount of memory , Is the location of the bytecode instruction that the current thread is executing site . If the current thread is executing a local method , Then the program counter is Undefined.
(2) The function of program counter
- Bytecode interpreter reads instructions in turn by changing program counter , So as to realize the flow control of the code .
- In the case of multithreading , The program counter records the current thread execution position , Thus, when the thread switches back to when , I know where the thread last executed .
(3) Features of program counter It's a small amount of memory .
- Thread private , Each thread has its own program counter .
- Life cycle : Created with the creation of threads , Destroy as the thread ends .
- It's the only one that won't show up OutOfMemoryError The memory area of .
Because of the length of the article , The content of the Department will be shown in pictures , If you have a partner, you need to check the complete document, watch and like + After paying attention 【 Click here to 】 Can get !!
1.2 Java Virtual machine stack (Java Stack )
(1)Java Definition of virtual machine stack Java Virtual machine stack is a description of Java The memory model of the method run .
Java The virtual machine stack will run for each Java Method to create a block called “ Stack frame ” Region , It is used to store some information during the operation of the method , Such as :
- Local variable table
- The stack of operands
- Dynamic links
- Method exit information
- …
(2) Stack pressing process When a local variable needs to be created during method operation , Store the value of the local variable into the local variable in the stack frame In the scale .
Java The stack frame at the top of the virtual machine stack is the active stack currently executing , That is, what is currently being implemented Method ,PC The register will also point to this address . Only the local variables of this active stack frame can be manipulated Use as a stack , When we call another method in this stack frame , The corresponding stack frame will be created , new The created stack frame is pressed into the top of the stack , Becomes the current active stack frame .
After the method is over , The current stack frame is moved out , The return value of the stack frame becomes one of the operand stacks in the new active stack frame Operands . If there is no return value , Then the new active stack frame has no change in the operand stack .
because Java The virtual machine stack corresponds to threads , Data is not shared by threads , So don't worry about data Consistency issues , There won't be a problem with synchronization locks .
(3)Java The characteristics of virtual machine stack
- The local variable table is created as the stack frame is created , Its size is determined at compile time , Just allocate things when creating You can specify the size first . During method operation , The size of the local variable table does not change .
- Java There are two exceptions to the virtual machine stack :StackOverFlowError and OutOfMemoryError.
- StackOverFlowError if Java The size of the virtual machine stack does not allow dynamic expansion , Then when the thread please The depth of the stack exceeds the current Java The maximum depth of the virtual machine stack , Throw out StackOverFlowError abnormal .
02 HotSpot Exploring virtual machine objects
2.1 Memory layout of objects stay HotSpot In the virtual machine , The memory layout of the object is divided into the following 3 area :
- Object head (Header)
- The instance data (Instance Data)
- Alignment filling (Padding)
(1) Object head
The object header records some data that the object needs to use in the running process :
- Hash code
- GC Generational age
- Lock status flag
- A lock held by a thread
- To the thread ID
- Bias timestamp
The object header may contain a type pointer , Through this pointer, you can determine which class the object belongs to . If the object is a Array , Then the object header will also include the array length .
(2) The instance data
The instance data part is the value of the member variable , It includes parent class member variables and this class member variables .
(3) Alignment filling
Used to ensure that the total length of the object is 8 Integer multiples of bytes . HotSpot VM The automatic memory management system of requires that the size of the object must be 8 Integer multiples of bytes . and The object header is exactly 8 Multiple of bytes (1 Times or 2 times ), therefore , When the object instance data Department When the points are not aligned , It needs to be filled by aligning the padding . Aligned padding does not necessarily exist , It doesn't mean anything , It's just a placeholder
03 Garbage collection strategy and algorithm
Program counter 、 Virtual machine stack 、 The native method stack is built with threads , It also dies with the thread ; Stack frame with method The beginning of the stack , Stack as method ends . The memory allocation and recycling of these areas are accurate qualitative , There is no need to think too much about recycling in these areas , Because the method ends or the thread ends when , Memory naturally follows the recycling . And for Java Heap and method area , We only know what objects will be created when the program is running , This part of memory allocation and recycling are dynamic , This is what the garbage collector is focusing on .
3.1 Determine whether the object is alive
If an object is not referenced by any object or variable , So it's an invalid object , Need to be recycled .
(1) Reference counting
Maintain a... In the object header counter Counter , Object is referenced once counter +1; If quoted If it fails, the counter -1. When the counter is 0 when , The object is considered invalid .
The implementation of reference counting algorithm is simple , It's also very efficient , In most cases, it is a good Algorithm . But mainstream Java There is no reference counting algorithm in virtual machine to manage memory , Mainly because It is difficult to solve the problem of circular reference between objects .
Take a chestnut object objA and objB All have fields instance, Make objA.instance = objB also objB.instance = objA, Because they refer to each other , The reference count that caused them Not for 0, So the reference counting algorithm can't notify GC The collector recycles them .
(2) Accessibility analysis
All and GC Roots Objects that are directly or indirectly related are valid objects , and GC Roots It's not off The associated object is an invalid object .
GC Roots Refer to :
- Java Virtual machine stack ( Local variables in stack frames ) Object referenced in
- Objects referenced in the local method stack
- The object referenced by a constant in the method area
- Object referenced by a class static property in a method area
GC Roots Objects referenced by objects in the heap... Are not included , So there's no problem with circular references .
04 HotSpot Garbage collector
HotSpot Virtual machines provide a variety of garbage collectors , Each collector has its own characteristics , Although I We need to compare each collector , But not to pick out the best collector . We chose Just the most suitable collector for specific applications .
4.1 A new generation of garbage collectors
(1)Serial Garbage collector ( Single thread ) Open only one GC Thread garbage collection , And stop all user threads during garbage collection (Stop The World).
Generally, client applications need less memory , Not too many objects will be created , And the heap memory is small , So garbage The collector recovery time is short , Even if all user threads are stopped during this time , I don't feel like a bright carton . therefore Serial The garbage collector is suitable for the client .
because Serial The collector uses only one GC Threads , It avoids the overhead of thread switching , Thus simple and high effect .
(2)ParNew Garbage collector ( Multithreading )
ParNew yes Serial Multithreaded version of . By multiple GC Threads do garbage cleaning in parallel . but The cleaning process still needs Stop The World.
ParNew Pursuit “ Low pause time ”, And Serial The only difference is the use of multithreading for garbage collection Set , In a multiple CPU Performance ratio in environment Serial There will be a certain degree of improvement ; But thread switching requires an amount Extra expenses , So in the single CPU Not as good as in the environment Serial.
05 Memory allocation and recycling strategy
06 JVM performance tuning
Deploy programs on high-performance hardware , At present, there are mainly two ways :
- adopt 64 position JDK To use large memory ;
- Use several 32 Bit virtual machines build logical clusters to utilize hardware resources .
07 Class file structure
08 Class load time
09 The process of class loading
10 Class loader
10.1 Class and class loader
(1) Determine whether the class “ equal ” Any class , Is established by the class loader that loads it and the class itself Java In the virtual machine Uniqueness , Every classloader , All have a separate class namespace .
therefore , Compare two classes whether or not “ equal ”, Only before these two classes are loaded by the same class loader It makes sense to mention , otherwise , Even if the two classes come from the same Class file , By the same virtual Machine loading , As long as the classloaders that load them are different , Then these two classes must not be equal . there “ equal ”, Include... That represents the class Class Object's equals() Method 、isInstance() Fang Return result of method , It also includes the use of instanceof Keywords are used to determine the relationship of objects .
边栏推荐
- Tp5.0 applet users do not need to log in and directly obtain the user's mobile number.
- Introduction and advanced MySQL (XIV)
- 三子棋(玩家+电脑)
- 原理知识用得上
- Multiline text omission
- 一种简单通用的获取函数栈空间大小的方法
- Unity 之游戏特效
- 带你来浅聊一下,单商户功能模块汇总
- What is SOA (Service Oriented Architecture)?
- Let's talk about the summary of single merchant function modules
猜你喜欢
12_ue4进阶_换一个更好看的人物模型
2. Nodejs -- path (\dirname, \filname), URL URL, querystring module, mime module, various paths (relative paths), web page loading (interview questions *)
【FreeSwitch开发实践】media bug获取通话语音流
MySQL installation and configuration super detailed tutorial and simple database and table building method
04 | 后台登录:基于账号密码的登录方式(上)
Hangao database best practice configuration tool Hg_ BP log collection content
mycat读写分离配置
4000 多字学懂弄通 js 中 this 指向问题,顺便手写实现 call、apply 和 bind
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
MySql的安装配置超详细教程与简单的建库建表方法
随机推荐
04 | 后台登录:基于账号密码的登录方式(上)
CentOS install mysql8
[freeswitch development practice] media bug obtains call voice flow
百度副总裁李硕:数字技术加持下中国劳动力成本上升是好事
军品技术文件划分及说明
Several methods of converting object to string
国产ERP有没有机会击败SAP ?
Summary of SAP localized content in China
C陷阱与缺陷 第3章 语义“陷阱” 3.1 指针与数组
[robot learning] matlab kinematics and ADMAS dynamics analysis of manipulator gripper
kubernetes-1.24.x 特性
Incremental real-time disaster recovery notes
Flask creation process day05-06 creation project
12_ue4进阶_换一个更好看的人物模型
Introduction and advanced level of MySQL (12)
MYSQL入门与进阶(十四)
军品三大基线(功能基线、分配基线、产品基线)及基线包含的文件
vasp计算任务报错:M_divide:can not subdivide 8 nodes by 6
Summary of common hooks
My approval function of conference OA project