In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the class loading process of JVM and the detailed introduction of parent delegation model". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the class loading process of JVM and the detailed introduction of the parent delegation model".
The main components of jvm
Class loader (ClassLoader)
Runtime data area (Runtime Data Area)
Execution engine (Execution Engine)
Local Library Interface (Native Interface)
The composition of the jvm runtime data area
Method area:
The ① method area is mainly used to store class information (constructor, interface definition), constants, static variables and runtime constant pools that have been loaded by the virtual machine.
② this area is shared by threads.
There is a pool of runtime variables in the ③ method zone that holds literals and symbolic references generated by static compilation. The constant pool is dynamic, that is, constants are not necessarily determined at compile time, and constants generated at run time also exist in this constant pool.
Virtual machine stack:
Virtual machine stack, also known as stack memory, in charge of the Java program running, is created when the thread is created, its life cycle is to follow the life of the thread, the thread ends the stack memory will be released, there is no garbage collection problem for the stack, as long as the thread ends the stack on the Over, the life cycle is the same as the thread, is thread private.
The eight basic types of variables + object reference variables + instance methods are allocated in the stack memory of the function.
① each method creates a stack frame when it is executed to store information such as local variables, Operand stacks, dynamic links, and method exits.
The ② virtual machine stack is thread-private and has the same life cycle as threads.
The ③ local variable table stores the basic data type, returnAddress type (address to a bytecode instruction), and object reference, which may be a pointer to the starting address of the object, a handle to the object, or a location associated with the object. 4. The memory space required for local variables is determined between compilers.
The function of the ④ Operand stack is mainly used to store the operation results and operands, which is different from the local variable table accessed through the index, but the way of pressing the stack and leaving the stack.
Each stack frame of ⑤ contains a reference to the method to which the stack frame belongs in the runtime constant pool. This reference is held to support dynamic connections during method calls. Dynamic linking converts symbolic references in constant pools to direct references at run time.
Local method stack
The local method stack is similar to the virtual machine stack, except that the local method stack serves Native methods.
Heap
The java heap is a piece of memory shared by all threads, created when the virtual machine starts, and almost all object instances are created here, so garbage collection operations often occur in this area.
Program counter
Because of the small memory space, the bytecode interpreter can select the next bytecode instruction to be executed by changing this count. The functions such as branching, loop, jump, exception handling and thread recovery all depend on this counter. This memory area is the only one where the java virtual machine specification does not specify any OOM (the program requests too much memory, the virtual machine cannot satisfy us, and then commits suicide).
The execution process of the program in the virtual machine
First of all, the class loader loads the class file to get the Class template, which is put into the method area (data such as class information (constructor, interface definition), constant, static variable and runtime constant pool, etc. When instantiating an object according to the class template, it will put the object in the heap (you can mention heap generation, garbage collection strategy, garbage collection algorithm, memory leak reasons), and when the method is called according to the object Methods will be pushed to the stack (8 basic types of variables + object reference variables + instance methods), native methods will be pushed into the local method stack, instructions will be sent to the operating system by jvm, commands interpreted by the execution engine will be sent to the operating system, and the operating system will call the local method interface, use the local method library, and execute local methods. The methods in the stack are out of the stack in the last-in-first-out order, and the program counter points to the next out-of-stack method. There is no garbage collection in the stack, and they are released as the thread finishes execution.
Parent delegation model based on class loading
Let's talk about the classloader before we introduce the parent delegation model. For any class, the uniqueness in JVM needs to be established by the class loader that loads it and the class itself. Each class loader has a separate class namespace. The class loader loads the class file into JVM memory according to the specified fully qualified name, and then converts it to a class object.
Class loader classification:
The boot class loader (Bootstrap ClassLoader), which is part of the virtual machine itself, is used to load class libraries in the Java_HOME/lib/ directory or in the path specified by the-Xbootclasspath parameter and recognized by the virtual machine
Extension class loader (Extension ClassLoader): responsible for loading the\ lib\ ext directory or Java. Ext. All class libraries in the path specified by the dirs system variable
Application class loader (Application ClassLoader). Responsible for loading the specified class library on the user classpath (classpath), we can use this class loader directly. In general, this loader is used by default if we do not have a custom class loader.
Custom class loader
There is a top-down relationship between them, as shown in the figure.
If a class loader receives a request for class loading, it will not load the class itself in the first place, but delegate the request to the parent class loader to complete it, as is the case with the class loader at each level. in this way, all load requests are passed to the top-level startup class loader, only if the parent load fails to complete the load request (the required class is not found in its search scope) Then let the subloader try to load the class from the top down.
So how do we test this claim?
Let's write a simple Mini Program:
Then compile the java file to generate the class file.
Let's put this file where the boot class loader can load and create a new directory classes:D:\ Program Files\ Java\ jdk1.8.0_161\ jre\ classes
Then modify the program:
Compile again and place the generated classes file in the new folder classes:D:\ Program Files\ Java\ jdk1.8.0_161\ jre\ lib\ ext\ classes where the extension class launcher can be loaded
Finally, we modify it again.
The generated class file is placed in the current directory.
So what is the result when we execute this class file?
This means that instead of loading the class file in our current directory, we used the startup class loader to scan the file in scope.
For further verification, we delete the files under D:\ Program Files\ Java\ jdk1.8.0_161\ jre\ classes
Run the result again:
The result becomes bbb, indicating that when the startup class loader does not find the class file, it is loaded by the extension class loader.
What about deleting files within the scope of the extension class loader?
Finally loaded into the class file under the current folder
At this point, I believe you have a deeper understanding of "the class loading process of JVM and the detailed introduction of the parent delegation model". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.