当前位置:网站首页>Introduction to JVM
Introduction to JVM
2022-07-05 23:21:00 【xmh-sxh-1314】
JVM Defines the control Java Five specifications of code interpretation, execution and specific implementation , So the JVM Divided into 6 Parts of :JVM Interpreter 、 Command system 、 register 、 Stack 、 Storage area and debris recycling area .
◆JVM Interpreter : That is, the virtual machine processes field codes CPU.
◆JVM Command system : The system is very similar to a computer , An instruction consists of an opcode and an operand . The operation code is 8 Bit binary number , Mainly to explain the function of an instruction , The operand can be determined as needed ,JVM At most 256 Different operating instructions . At present, it has been used 160 Multiple opcodes .
◆ register :JVM Have your own virtual register , In this way, we can quickly communicate with JVM Interpreter for data exchange . For functional needs ,JVM Set up 4 A commonly used 32 Bit register :pc( Program counter )、optop( Pointer to the top of the operand stack )、frame( Current execution environment pointer ) and vars( Pointer to the first local variable in the current execution environment ).
◆JVM Stack : The place and control center where data and information are stored when instructions are executed , It provides JVM The information needed for the operation of the interpreter . When JVM Get one Java After bytecode application , Create a stack framework for each method of a class in the code , To save the state information of the method . Each stack framework includes the following three types of information : local variable 、 execution environment 、 The stack of operands .
Local variables are used to store local variables used in the methods of a class .vars The register points to the first local variable in the variable table .
The execution environment is used to save the interpreter pair Java The information required in the interpretation of bytecode . They are : Last called method 、 Local variable pointer and top and bottom of the stack of operands . The execution environment is a control center that executes a method . for example : If the interpreter wants to execute iadd( Integer addition ), First of all, from the frame Find the current execution environment in the register , Then find the operand stack from the execution environment , Pop up two integers from the top of the stack for addition , Finally, push the result into the top of the stack .
The operand stack is used to store the operands required by the operation and the result of the operation .
◆ Storage area :JVM There are two types of storage : Constant buffer pool and method area . The constant buffer pool is used to store class names 、 Method and field names and string constants . The method area is used to store Java Bytecode of method .
◆ Debris recycling area :JVM Debris recycling refers to the use of Java The concrete instance of class is recycled from memory , This makes developers avoid the trouble and danger of programming and controlling memory . With JVM Continuous upgrading of , The technology and algorithm of fragment recovery are also more reasonable .JVM 1.4.1 After the edition, there is a technology called generational collection , Simply put, it is to divide the time of objects in the program into generations , Take this as the standard for debris recovery .
2.JAVA Garbage collection mechanism GC Determines whether an object is collected by determining whether it is referenced by the active object .
2.1 Trigger GC(Garbage Collector) Conditions
1)GC Run in the lowest priority thread , Generally, when the application is idle, that is, no application thread is called at runtime . But the following conditions are exceptions .
2)Java When the heap is low on memory ,GC Will be called . When the application thread is running , And create new objects in the run , If there is not enough memory ,JVM Will force the call GC Threads . if GC Memory allocation cannot be satisfied after one time ,JVM There will be two more GC, If still unable to meet the requirements , be JVM Will report “out of memory” Error of ,Java The app will stop .
2.2 Two important ways
2.2.1 System.gc() Method
Use System.gc() Can be either JVM What kind of garbage collection algorithm is used , Can request Java The garbage collection of . There is a parameter in the command line -verbosegc You can see Java Heap memory used , The format is as follows :java -verbosegc classfile Because this method will affect the system performance , It is not recommended to use , So don't tell me .
2.2.2 finalize() Method
stay JVM Before the garbage collector collects an object , Generally, the program is required to call appropriate methods to release resources , But without explicitly releasing resources ,Java A default mechanism is provided to terminate the object and release resources , This method is finalize(). Its prototype is :protected void finalize() throws Throwable stay finalize() After the method returns , The object disappears , Garbage collection starts . In the prototype throws Throwable Indicates that it can throw any type of exception .
Why use finalize(), There is a special case that the garbage collector cannot handle . for example :1) Because when allocating memory, you may use a similar C The practice of language , Instead of JAVA Usual new practice . This happens mainly in native method in , such as native method Called C/C++ Method malloc() Function series to allocate storage space , But unless you call free() function , Otherwise, these memory spaces will not be released , Then this time may cause memory leakage . But because of free() The method is in C/C++ The function in , therefore finalize() You can use local methods to call it . To release these “ special ” Of memory space .2) Or open file resources , These resources do not belong to the recycling scope of the garbage collector .
2.3 Reduce GC Spending measures
1) Do not explicitly call System.gc(). This function is recommended JVM Carry out the main task GC, It's just advice, not certainty , But in many cases, it will trigger the main GC, So as to increase the main GC The frequency of , That is to say, the number of intermittent pauses is increased . Greatly affect the system performance .
2) Minimize the use of temporary objects . After the temporary object jumps out of the function call , Will become rubbish , Using fewer temporary variables is equivalent to reducing the generation of garbage , Thus, the time for the occurrence of the above second trigger condition is prolonged , Reduce the main GC The opportunity of .
3) When the object is not in use, it is best to explicitly set it to Null. generally speaking , by Null The objects will be treated as garbage , So the unused objects are explicitly set to Null, advantageous to GC The collector determines that the garbage , So as to improve GC The efficiency of .
4) Use as much as possible StringBuffer, without String To accumulate strings . because String Is a fixed length string object , Add up String Object time , Not in a String Object , But to recreate a new String object , Such as Str5=Str1+Str2+Str3+Str4, Multiple garbage objects will be generated during the execution of this statement , Because of the second “+” You must create a new String object , But these transition objects have no practical significance for the system , It will only add more garbage . To avoid this situation, you can use StringBuffer To accumulate strings , because StringBuffer It's variable length , It is amplified on the original basis , No intermediate objects are generated .
5) Can use basic types such as Int,Long, You don't have to Integer,Long object . Basic type variables take up much less memory resources than the corresponding objects , If not necessary , It's best to use basic variables .
6) Try to use less static object variables . Static variables are global variables , It won't be GC Recycling , They will always occupy memory .
7) Scatter when objects are created or deleted . Focus on creating a large number of new objects in a short time , Especially large objects , It will cause a sudden need for a lot of memory ,JVM In the face of this situation , Only the main GC, To reclaim memory or consolidate memory fragments , So as to increase the main GC The frequency of . Delete objects in a set , The same is true . It makes a large number of garbage objects appear suddenly , Free space is bound to decrease , This greatly increases the force of the master when creating a new object the next time GC The opportunity of .
2.4 The object is JVM The state of the heap
1) Accessible state : There are also variable references in the program , Then this object is accessible .
2) Resurrectable state : When there is no variable reference to this object in the program , Then this object changes from touchable state to revivable state .CG The thread will be ready to call this object at a certain time finalize Method (finalize Method inherits or rewrites children Object),finalize It is possible for the code in the method to turn the object into a touchable state , Otherwise, the object becomes inaccessible .
3) Inaccessible state : Only when the object is in an inaccessible state ,GC Threads can reclaim the memory of this object .
Jvm Heap object state transition diagram
2.5 Common garbage collectors
1) Mark - Clear the collector Mark-Sweep
2) Copy collector Copying
3) Mark - Compression collector Mark-Compact
4) Generational collectors Generational
2.6 Introduction to garbage collection algorithm
2.6.1 tracing Algorithm
be based on tracing Algorithm garbage collection is also called marking and clearing (mark-and-sweep) Garbage collector .
This is the most basic garbage collection algorithm , The reason why it is the most basic is that it is the easiest to implement , Thought is also the simplest . Mark - The clearing algorithm is divided into two stages : Mark phase and clear phase . The task of the marking phase is to mark all objects that need to be recycled , The cleanup phase is to reclaim the space occupied by the marked object . The specific process is shown in the figure below :
It is easy to see the mark from the figure - The clearing algorithm is easy to implement , However, there is a serious problem that it is easy to produce memory fragments , Too many fragments may lead to the failure to find enough space for large objects in the subsequent process and trigger a new garbage collection action in advance .
2.6.2 Copying Algorithm
In order to solve Mark-Sweep Defects of algorithm ,Copying The algorithm was put forward . It divides the available memory into two equal sized blocks according to the capacity , Use only one piece at a time . When this block of memory runs out , Copy the living object to another piece , Then clean up the used memory space once again , In this way, it is not easy to have the problem of memory fragmentation . The specific process is shown in the figure below :
This algorithm is simple to implement , Runs efficiently and does not easily generate memory fragmentation , But it has made a high price for the use of memory space , Because the memory that can be used is reduced to half of the original . Obviously ,Copying The efficiency of the algorithm has a lot to do with the number of surviving objects , If there are a lot of survivors , that Copying The efficiency of the algorithm will be greatly reduced .
2.6.3 compacting Algorithm
In order to solve Copying Defects of algorithm , Make the most of memory space , Put forward Mark-Compact Algorithm . The algorithm marks stages and Mark-Sweep equally , But after marking , It's not about cleaning up recyclable objects directly , Instead, move the living objects to one end , Then clean up the memory beyond the end boundary . The specific process is shown in the figure below :
2.6.4 Generation Algorithm
Generational collection algorithms are by far the majority JVM The algorithm used by the garbage collector of . Its core idea is to divide the memory into several different regions according to the life cycle of the object . In general, the reactor area is divided into the older generation (Tenured Generation) And the new generation (Young Generation), The characteristic of the old days was that only a small amount of objects needed to be recycled each time the garbage was collected , The new generation is characterized by a large number of objects that need to be recycled every time they are recycled , Then we can take the most suitable collection algorithm according to the characteristics of different generations .
At present, most of the garbage collectors adopt Copying Algorithm , Because in the new generation, most objects are recycled every time they are recycled , That is to say, fewer operations need to be copied , But in practice, it's not in accordance with 1:1 To divide the space of the Cenozoic , Generally speaking, the Cenozoic is divided into a larger area Eden Space and two smaller ones Survivor Space , Each use Eden Space and a piece of it Survivor Space , When it comes to recycling , take Eden and Survivor Is copied to another piece Survivor In the space , And clean it up Eden And the one we just used Survivor Space .
And because of the characteristics of the old age, only a small number of objects are recycled each time , It's commonly used Mark-Compact Algorithm .
New age : All newly created objects are stored here . Because most objects quickly become unreachable , So most objects are created in the younger generation , And then disappear . When an object disappears from this memory area , We say it happened once “minor GC”.
Old age : Nothing becomes unreachable , The surviving younger generation is copied here . This memory area is generally larger than the younger generation . Because it's bigger ,GC It happens less often than in younger generations . When objects disappear from the old age , We said “major GC”( or “full GC”) It happened. .
The permanent generation in the above figure (permanent generation) Also known as “ Method area (method area)”, He stores class Object and string constants . So this memory area is definitely not a permanent storage of objects that survive from the elderly . Garbage collection is possible in this memory . And what happens here is garbage collection is also called major GC
边栏推荐
- AsyncSocket长连接棒包装问题解决
- Three. JS VR house viewing
- 2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
- 派对的最大快乐值
- JVM的简介
- 并查集实践
- Openresty ngx Lua regular expression
- Code farmers to improve productivity
- Debian 10 installation configuration
- CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
猜你喜欢
Registration of Electrical Engineering (elementary) examination in 2022 and the latest analysis of Electrical Engineering (elementary)
TVS管和ESD管的技术指标和选型指南-嘉立创推荐
Detailed explanation of pointer and array written test of C language
Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
Fix the memory structure of JVM in one article
Alibaba Tianchi SQL training camp task4 learning notes
The method and principle of viewing the last modification time of the web page
3:第一章:认识JVM规范2:JVM规范,简介;
开关电源Buck电路CCM及DCM工作模式
Yiwen gets rid of the garbage collector
随机推荐
Hcip day 11 (BGP agreement)
PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
Thoroughly understand JVM class loading subsystem
Live tiktok shop 2022 latest gameplay card slot overseas live e-commerce new traffic
VS2010 writes DLL and unit test of dynamic link library, and transfers the correctness of DLL test
Douban scoring applet Part-2
Multi view 3D reconstruction
Creative mode 1 - single case mode
Leecode learning notes
LeetCode——Add Binary
LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
Multi sensor fusion of imu/ optical mouse / wheel encoder (nonlinear Kalman filter)
判斷二叉樹是否為完全二叉樹
openresty ngx_lua正則錶達式
Using LNMP to build WordPress sites
February 13, 2022-4-symmetric binary tree
Go language implementation principle -- lock implementation principle
(4)UART應用設計及仿真驗證2 —— TX模塊設計(無狀態機)
11gR2 Database Services for "Policy" and "Administrator" Managed Databases (文件 I
Comparison between webgl and webgpu [3] - vertex buffer