One article on JVM is enough
lijunsong 2021-06-04 10:43:29
Method area Stack Pile up
Person person = new Person();

Stack stack

  1. Store local variables , Operands , Method export, etc
  2. JVM Create a stack for each thread , Used to store information about thread execution methods ( The actual parameter , Local variables, etc )
  3. First in, then out , Last in, first out
  4. Stacks are automatically allocated by the system , Fast , It's a continuous memory space
  5. Thread private

Pile up heap

  1. Store created objects , Array
  2. JVM There's only one heap , Thread sharing
  3. It's a discrete memory space , Flexible distribution , Slow speed

Method area method area

  1. JVM There is only one method area , Thread sharing
  2. It's actually a pile , It's just for storing classes 、 Constant
  3. It is used to store the permanent or unique content in the program ( Class information -class object 、 Static variables 、 String constants, etc )

Memory recovery

  • Judge whether the object is ‘ Death ’
  1. Reference counting Reference Counting

Object without any references associated with it , That is, the number of references is zero , Explain the object ‘ Death ’

Insufficient : Object circular reference can't judge the problem

  1. Root search algorithm GC Roots Tracing

GC Roots As a starting point , Search down , When the object goes to any GC Roots When there is no reference chain , Explain the object ‘ Death ’

VM Stack reference , Static references in the method area ,JNI Citation in

  • Algorithm
  1. Mark clear Mark-Sweep

Marking stage : First mark all objects to be recycled

Sweep phase : Recycle all marked objects after marking

Major deficiencies : Space problem , After the tag is cleared, a large number of discontinuous memory fragments will be generated , Too much space debris may lead to the need to allocate large objects in the process of program running later , Unable to find enough contiguous memory to trigger another garbage collection action in advance .

  1. Copy Copying

Divide the available memory into two equal sized blocks according to 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 at a time . This makes every time the whole half area is recycled , Memory allocation does not need to consider memory fragmentation and other complex situations , Just allocate the memory in order , Implement a simple , Efficient operation .

Insufficient : But the cost of this algorithm is to reduce the memory to half of the original , And there's the copy step

  1. Tag to sort out Mark-Compact

First mark all objects to be recycled , After marking , The next step is not to clean up the recyclable objects directly , I'm going to move all the living objects towards one end , Then directly clean up the memory outside the end boundary .

  1. Generational recycling Use one or more of the above algorithms for recycling according to different generations

In the new generation , A large number of objects are found dead in every garbage collection , Only a few survive , Then choose the replication algorithm , Only a small amount of the cost of replication of living objects can complete the collection . But in the old generation, because of the high survival rate of the object 、 There is no additional space to guarantee its distribution , Must use “ Mark — clear ” perhaps “ Mark — Arrangement ” Algorithm to recycle

  • The new generation is available GC: Serial copying, Parallel recycling copying, parallel copying
  • Minor GC Trigger mechanism and log format
  • Old generation available GC: Serial Mark-Sweep-Compact, parallel Compacting, parallel Mark-Sweep

Memory condition analysis method

  1. jconsole
  2. visualvm
  3. jstat

Command line tool for monitoring various running state information of virtual machine . It can show class loading in local or remote virtual machine processes 、 Memory 、 garbage collection 、JIT Compile and other operation data

  1. jmap

jmap It's not just about getting dump file , It can also query finalize Perform the queue 、Java Heap and permanent generation details , Such as space utilization rate 、 What kind of collector is currently used

  1. MAT

JVM Life cycle

Class Documents are a set of 8 Bit byte is the base unit of binary stream

  1. load (Loading)

The virtual machine needs to complete the following 3 thing :

Get the binary byte stream that defines this class by using the fully qualified name of a class .

Convert the static storage structure represented by the byte stream into the runtime data structure of the method area .

Generate a representation of this class in memory java.lang.Class object , As the access to all kinds of data of this class in method area .

  1. verification (Verification)

It's the first step in the connection phase , The purpose of this phase is to ensure Class The information contained in the byte stream of the file meets the requirements of the current virtual machine , And will not endanger the security of virtual machine itself . But on the whole , The validation phase will basically complete the following 4 Three stages of test action : File format validation 、 Metadata validation 、 Bytecode verification 、 Symbol reference validation .

  1. Get ready (Preparation)

It is the stage of formally allocating memory for class variables and setting the initial value of class variables , The memory used by these variables will be allocated in the method area .

There are two confusing concepts in this stage that need to be emphasized , First , At this time, memory allocation only includes class variables ( By static Decorated variable ), Not instance variables , Instance variables will be assigned to the Java In the pile . secondly , The initial value here “ General situation ” Next is the zero value of the data type ,

Suppose a class variable is defined as : public static int value=123; That variable value The initial value after the preparation phase is 0 instead of 123, Because at this time, we haven't started any Java Method , But the value The assignment is 123 Of putstatic An instruction is when a program is compiled , Stored in class constructor <clinit>() Among methods , So the value The assignment is 123 The action will not be executed until the initialization phase . surface 7-1 Lists Java Zero value of all basic data types in . Let's say the class variable above value The definition of becomes :public static final int value=123; Compile time Javac Will be value Generate ConstantValue attribute , In the preparation phase, the virtual machine will be based on ConstantValue The settings for will value The assignment is 123.

  1. analysis (Resolution)

It is the process of virtual machine replacing symbolic reference in constant pool with direct reference

  1. initialization (Initialization)

Is the last step in the class loading process , In the previous class loading process , In addition to the user application being able to participate through a custom classloader during the load phase , The rest of the actions are completely dominated and controlled by the virtual machine . It's time to initialize , To actually start executing the Java The code is in the preparation phase , The variable has been assigned the initial value required by the system , And in the initialization phase , Class variables and other resources are initialized according to the subjective plan made by the programmer ,

Or it can be expressed in another way : The initialization phase is the execution class constructor <clinit>() Method process .<clinit>() The compiler automatically collects the assignment actions and static statement blocks of all class variables in the class (static{} block ) The combination of the statements in , The order in which the compiler collects is determined by the order in which statements appear in the source file . <clinit>() Methods are not required for classes or interfaces , If there is no static statement block in a class , There's no assignment to variables , Then the compiler can not generate for this class <clinit>() Method . Virtual opportunity guarantees a class of <clinit>() Methods are properly locked in a multithreaded environment 、 Sync , If multiple threads are initializing a class at the same time , Then only one thread will execute this class <clinit>() Method , All other threads need to block and wait , Until the active thread executes <clinit>() Methods to complete . If in a class <clinit>() There are time-consuming operations in the method , It can cause multiple processes to block .

  1. Use (Using)
  2. uninstall (Unloading)
Please bring the original link to reprint ,thank
Similar articles