Detailed HotSpot virtual machine Java object creation process
(1) When the virtual machine encounters a new instruction, it first checks whether the parameters of this instruction can locate a symbol reference of a class in the constant pool, and check whether the class represented by this symbol reference has been loaded, connected and initialized. If not, execute the loading process of the class.
(2) Allocate memory for the object. A. Assuming that the Java heap is regular, all used memory is placed on one side, free memory is placed on the other side, and a pointer is placed in the middle as an indicator of the dividing point. Allocating memory just moves the pointer to the free space by the same distance as the size of the object. This allocation is called "pointer collision". B. Assuming that the Java heap is not regular, and the used memory and the free memory are interlaced, then There is no way to "pointer collision". The virtual machine maintains a list, records which memory blocks are available, finds out a large enough space to allocate to the object instance during allocation, and updates the records on the table. This allocation method is called "free list". C. Which allocation method is used is determined by whether the Java heap is regular. Whether the Java heap is regular or not is determined by whether the garbage collector used has a compression function. D. The method of assigning objects to ensure thread safety: The virtual machine uses CAS failure retry to ensure the atomicity of the update operation. (In fact, there is another scheme: each thread pre-allocates a small piece of memory in the Java heap, which is called the local thread allocation buffer, TLAB. Which thread allocates memory is allocated on the TLAB of which thread, only TLAB When the new TLAB is used up and a new TLAB is allocated, the synchronization lock is performed. Whether the virtual machine uses TLAB or not is determined by the -XX:+/-UseTLAB parameter).
(3) The memory space allocated by the virtual machine is initialized to zero (default value).
(4) The virtual machine performs necessary settings for the object, such as which instance of the object is, how to find the object's metadata information, the object's Hash code, and the object's GC generation age. This information is stored in the object header of the object.
(5) Execution method, initialize the object according to the programmer's wishes.
How to locate and access objects (how to locate specific objects on the heap by reference)
(1) Handle: Using the handle, a piece of memory will be divided into the Java heap as the handle pool, and the address of the object's handle is stored in the reference. The handle contains the address of object instance data and object type data.
(2) Direct pointer: Using the direct pointer method, the address of the object is stored in the reference. The layout of Java heap objects must consider how to access object type data.
(3) Both methods have their own advantages: A. The advantage of using handle access is that a stable handle address is stored in the reference. When the object is moved (for example, when the object is moved during garbage collection), only the instance data pointer in the handle will be changed, And the reference itself will not be modified. B. The use of a direct pointer saves the time cost of a pointer positioning.
Class loading mechanism
concept
The class loader reads the binary data in the class file into the memory, stores it in the method area, and then creates a java.lang.Class object in the heap area to encapsulate the data structure of the class in the method area.
The steps for class loading are as follows
1. Load: Find and load the binary data of the class (load the information in the class file into the memory).
2. Connection: Combine the binary data of the class in memory into the runtime environment of the virtual machine (1) Verification: Ensure the correctness of the loaded class. Including: A. Class file structure check: check whether it meets the fixed format of Java class file; B. Semantic check: ensure that the class itself conforms to the Java syntax specification; C. Bytecode verification: ensure that the bytecode stream can be virtualized by Java Machine safe execution; D. Binary compatibility verification: Ensure that the mutually referenced classes are coordinated. (2) Preparation: Allocate memory for static variables of the class and initialize them to default values (3) Analysis: Convert symbol references in the class into direct references (such as method symbol references, which have method names and related descriptors) In the parsing phase, JVM replaces the symbol reference with a pointer. This pointer is a direct reference, which points to the memory location of the method of the class in the method area).
3. Initialization: Assign correct initial values to static variables of the class. When the value on the right side of the equal sign of a static variable is a constant expression, the static code block will not be called for initialization. Only when the value on the right side of the equal sign is a value calculated at runtime, static initialization will be called.
Parental Delegation Model
1. When a class loader receives a class loading request, it will not first load the information of this class by itself, but forward the request to the parent class loader, and then upward. Therefore, all class loading requests will be passed to the parent class loader. Only when the required class cannot be loaded in the parent class loader, the child class loader will try to load the class itself. When the current class loader and all parent class loaders cannot load the class, a ClassNotFindException is thrown.
2. Significance: Improve the security of the system. A user-defined class loader cannot load reliable classes that should be loaded by the parent loader. (For example, the user defines a malicious code, the custom class loader first lets the system loader to load, the system loader checks that the code does not meet the specifications, so it does not continue to load).
Runtime package
(1) The runtime package is composed of classes loaded by the same class loader and with the same package name.
(2) Only classes that belong to the same runtime package can access the classes and class members visible to the package (default). The function is to restrict user-defined classes from posing as classes in the core class library to access the package visible members of the core class library.
Loading two copies of the same class object
A and B do not belong to the parent-child class loader relationship, and each load the same class.
Features
1. Overall responsibility: When a class loader loads a class, other classes that the class depends on will also be loaded into memory by this class loader.
2. Cache mechanism: All Class objects will be cached. When the program needs to use a certain Class, the class loader first looks in the cache. If it cannot find it, it reads the data from the class file and converts it into a Class object. Into the cache.
Two types of class loaders
1. JVM's own class loader (3 types): (1) Root class loader (Bootstrap): a, written in C++, programmers cannot get this class in the program b, responsible for loading the core library of the virtual machine, For example, java.lang.Object c, does not inherit the ClassLoader class (2) Extension class loader (Extension): a, written in Java, load the class library from the specified directory b, the parent loader is the root class loader c, is the ClassLoader Subclass d. If the user puts the created jar file in the specified directory, it will also be loaded by the extension loader. (3) System loader (System) or application loader (App): a, written in Java b, the parent loader is an extended class loader c, the class is loaded from environment variables or class.path d, it is user-defined The default parent loader e for class loading is a subclass of ClassLoader
2. User-defined class loader: (1) Subclass of Java.lang.ClassLoader (2) User can customize the way the class is loaded (3) Parent The class loader is the system loader (4) writing steps: A, inherit ClassLoader B, rewrite the findClass method. Load the class file from a specific location, get the byte array, and then use defineClass to convert the byte array into a Class object (5) Why do we need to customize the class loader? A. Class files can be loaded from a specified location, such as from a database or cloud. B. Encryption: Java code can be easily decompiled. Therefore, if the code needs to be encrypted, the encrypted code cannot be used. Java comes with ClassLoader to load this class, you need to customize the ClassLoader, decrypt this class, and then load it.
Question: There are several ways for Java programs to execute classes
1. Active use (6 cases): JVM must initialize these classes when each class is "actively used for the first time". (1) Create an instance of the class (2) Read and write static variables of a certain class or interface (3) Call the static method of the class (4) Obtain the class with the reflected API (Class.forName()) (5) Initialize one Subclass of the class (6) When the JVM is started, the class that is marked as the startup class (the class containing the Main method). Only when the static variable or static method used by the program is actually defined in the class, it can be considered as the class Or active use of the interface.
2. Passive use: Except for the 6 cases of active use, all other cases are passive use and will not cause the initialization of the class. 3. The JVM specification allows the class loader to pre-load a certain class when it anticipates that it will be used. If the class file is missing or there is an error, the error will only be reported when the program is "first active use". (Linkage Error). If this class has not been "actively used" by the program, no error will be reported.
Class loading mechanism and interface
1. When the Java virtual machine initializes a class, it will not initialize the interfaces implemented by the class.
2. When an interface is initialized, the parent interface of this interface will not be initialized. 3. Only when the program uses the static variables of the interface for the first time, it will cause the initialization of the interface.
ClassLoader
Calling the loadClass method of Classloader to load a class is not actively used, so the class will not be initialized.
Unloading of classes
1. The classes loaded by the three types of loaders (root, extension, system) that come with the JVM will never unload. Because the JVM always refers to these class loaders, these class loaders use to refer to the classes they load, so these Class objects are always reachable.
2. The classes loaded by the user-defined class loader can be unloaded.
Graphical java file into machine code
The JVM virtual machine first compiles java files into class files (bytecode files), and then converts the class files into machine instructions that all operating systems can run.
Comments
Post a Comment