当前位置:网站首页>Notes (V) - JVM

Notes (V) - JVM

2022-06-10 22:17:00 Li_ XiaoJin

JVM Notes on .

1. What are the parts of the program runtime data ?

Have a look JVM Run time data area

2.Java The process of creating objects

1、 Class loading check : Virtual opportunity to a new When the command , First, check whether the parameter of this instruction can locate the symbol reference of this class in the constant pool , And check whether the class represented by this symbolic reference has been loaded 、 Parsed and initialized . If not, the corresponding class loading process must be performed .

2、 Allocate memory : After class load check , The virtual opportunity allocates memory for new objects . The memory size required by the object will be known after loading , The task of allocating memory space for an object is to transfer a certain size of memory from Java Divide it up in the heap . The ways of distribution are Pointing collision and Free list Two kinds of , Select which allocation method is determined by Java The stack is all ordered ,Java Whether the heap is regular or not depends on whether the garbage collector used has compression and collation function .

  • How memory is allocated : Java The regularity of the heap depends on GC The algorithm of the collector is “ Mark - eliminate ” still “ Mark - Arrangement ”( It's also called “ Mark - Compress ”), The memory of the replication algorithm is regular .

(1) Pointer collision Suitable for the scene : Heap memory is regular ( There is no memory fragmentation ) principle : All used memory is integrated to one side , Put unused memory on the other side , There is a boundary value pointer in the middle , Just move the pointer to the memory size of the object in the direction of unused memory GC The collector :Serial、ParNew

(2) Free list Suitable for the scene : In case of irregular heap memory principle : Virtual opportunity maintains a list , The list records which memory is available , When allocating memory , Find a large enough memory to partition the object instance , Finally update the list record GC The collector :CMS

  • Concurrency of memory allocation When you create an object , There are two ways for virtual machines to ensure thread safety

(1)CAS + Failure to retry :CAS It's an implementation of optimistic lock , Optimistic lock is to complete an operation every time without lock but assuming no conflict , Try again if there are conflicts and failures , Until we succeed . Virtual machine adopts CAS In addition, the failed retry method ensures the atomicity of the update operation

(2)TLAB: In advance for each thread Eden Allocate a piece of memory ,JVN When allocating memory to a thread's object , First, in the ELAB Distribute , When the object is greater than ELAB The remaining memory or ELAB When you run out of memory , Then use CAS Memory allocation

3、 Initialize zero value : After memory allocation , The virtual machine needs to set the allocated memory space to zero ( Object headers are not included ), This step ensures that the instance of the object is in Java Code can be used directly without assignment , The program can access the zero value corresponding to the data type of these fields .

4、 Set object header : After initializing the zero value , The virtual machine should make necessary settings for the objects . For example, some objects belong to instances of which class 、 How to find the metadata information of a class 、 Object hash code, etc , This information is stored in the object header .

5、 perform init Method : front 4 After that step , From a virtual machine perspective , A new object has been created , But from Java From a procedural point of view , Object creation is just beginning , And then we'll do init Method , Initialize the object according to the programmer's wishes , Only in this way can we truly complete the creation of objects .

3. The basic strategy of allocating objects in heap memory

eden District 、s0 District 、s1 All of them belong to the new generation ,tentired District belongs to the old generation . In most cases , The object will first be in eden District Distribution , After the new generation of garbage collection , If the object is still alive , You will enter s0 or s1 District , And the age of the object will be increased 1(eden District ->survivor After the partition, the initialization age of the object becomes 1), When it reaches a certain age ( Default 15 year ), Will be promoted to the senior generation . The threshold for promotion to old age , You can use the parameter -XX:MaxTenuringThreshold Set it up . Large objects or long-lived objects will directly enter the elderly generation

4.Minor GC and Full GC What's the difference?

Most of the time , The object is eden District Distribution , When edne There is not enough space to allocate , A virtual opportunity is launched Minor GC

The new generation GC(Minor GC): What happened in the new generation of garbage collection , Very often , Recovery speed is relatively fast Old age GC(Major GC/Full GC): Garbage collection in the old days , There is Major GC Often accompanied at least once Minor GC( Not absolutely ),Major GC It's usually faster than Minor GC slow 10 More than times

5. How to judge whether the object is dead

(1) Reference counting : Adds a reference counter to the object , Whenever there is a reference to it , The counter is added 1; When the reference fails , The counter subtracts 1; Any time the counter is 0 It is impossible to use the object of .

(2) Reachability analysis algorithm : Through a series of “GC Root” As the starting point , Start with these nodes and drill down , The path the node takes is the reference chain , Be an object of GC Root There is no chain of references connected haunt, Indicates that the object is not available .

6. A brief introduction to strong references 、 Soft citation 、 Weak reference 、 Virtual reference

  • Strong citation
  • Soft citation

  • Weak reference

  • Virtual reference

7. Garbage collection algorithm

Garbage collection algorithm

8.HotSpot Why is it divided into the new generation and the old generation

Mainly to improve GC The efficiency of

Reference resources : Garbage collection algorithm

9. Common garbage collectors

After reading the book, I will analyze it carefully , Learn first

10. The process of class loading

To learn

11. Class loader

JVM Built in three important ClassLoader, except BootstrapClassLoader Other class loaders are powered by Java Implement and fully inherit java.lang.ClassLoader

1、BootstrapClassLoader( Start class loader ): The top loader , from C++ Realization , Responsible for loading %JAVA_HOME%/lib In the catalog jar Packages and classes are either -Xbootclasspath Parameter specifies all classes in the path .

2、ExtensionClassLoader( Extend the classloader ): Responsible for loading the directory %JRE_HOME%/lib/ext In the catalog jar package , Or be java.ext.dirs Under the path specified by the system variable jar package .

3、AppClassLoader( Application class loader ): User oriented loader , Responsible for loading the current application classpath All under jar Baohe class .

12. The life cycle of a class

load 、 verification 、 Get ready 、 analysis 、 initialization 、 Use 、 uninstall

Class loading process : load 、 verification 、 Get ready 、 analysis 、 initialization

12. Parent delegation model

To learn

Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/ Note 5 -jvm

原网站

版权声明
本文为[Li_ XiaoJin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102054352074.html