In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article shows you how to introduce the principles of JVM, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Class loader ClassLoader
Responsible for loading the class file, the class file has a specific file identity at the beginning of the file, loads the bytecode contents of the class file into memory, and converts these contents into the runtime data structure in the method area, and ClassLoader is only responsible for loading the class file, as to whether it runs or not is decided by Execution Engine (courier)
1. The loader included in the virtual machine:
Launch class loader (Bootstrap) C++: load the classes originally included with jdk under jdk1.8.0_201\ jre\ lib\ rt.jar, such as Object, String, ArrayList, etc. But null will be displayed if you want to use getClassLoader to get the loader name, because the loader is written by C++, not Java.
Extension class loader (Extension) Java: loads the classes extended during the jdk upgrade in the jdk1.8.0_201\ jre\ lib\ ext directory
The application class loader (AppClassLoader) java, also known as the system class loader, loads all classes of the currently applied classpath
2. User-defined loader
Java.lang.ClassLoader subclass, the user can customize the loading method
3. Frequently asked interview questions
Parent appointment mechanism:
(1) if a class loader receives a class loading request, it does not load itself but sends the request to its parent class to execute.
(2) if the parent class loader still has its parent class loader, further delegate upward.
(3) return if the parent class loader can complete the class loading task, and allow its own subclass to load if it cannot
Sandbox security: the parent delegation mechanism ensures that the factory source code of Java will not be contaminated by developers (sandbox security mechanism)
Local method interface
A Native Method is an interface for Java to call non-Java code, which is implemented in a non-Java language.
Sometimes Java applications need to interact with the environment outside the Java, which is the main reason for the existence of local methods. You can think about what happens when Java needs to exchange information with some underlying system, such as the operating system or some hardware. The local approach is such a communication mechanism: it provides us with a very concise interface, and we don't need to know the tedious details outside of Java applications.
PC register (program counter)
Each thread has a program counter, which is private to the thread, which is a pointer that stores the address to the next instruction, that is, the instruction code to be executed. Is a very small piece of memory, which is the line number indicator of the bytecode to be executed by the current thread. If you are executing a Native method, the counter is empty.
Method area
It stores structural information for each class, such as the bytecode contents of the runtime constant pool, field and method data, constructors, and common methods.
What is mentioned above is the specification, and the implementation is different in different virtual machines. The most classic is permanent generation and metaspace.
Instance variables are stored in heap memory and are independent of the method area
Stack (java stack)
Stack, also known as stack memory, is in charge of the java program running, is created when the thread is created, its life cycle is to follow the life cycle of the thread, the thread ends the stack memory will be released, there is no garbage collection problem for the stack, the life cycle is consistent with the thread, is private to the thread. The eight basic types of variables + object reference variables + instance methods are allocated in the stack memory of the function.
5.1 there are three main types of data stored in the stack frame (outside is the method):
Local variables: input and output parameters and variables within methods
Stack operation: record the operation of getting out and entering the stack
Stack frame data: including class files, methods, and so on.
5.2java.lang.StackOverflowError (stack overflow) he is a mistake, it is an error
5.3 interaction of stack + heap + method area
HotSpot (which is usually our version of jdk) uses pointers to access objects.
Addresses that access class metadata are stored in the Java heap
Reference stores the address of the object directly.
Heap reactor
There is only one heap memory in a JVM instance, and the size of the heap memory is adjustable.
First, take a look at how the JAVA heap memory is divided, as shown in the figure:
(1) simple JVM memory can be divided into heap memory and non-heap memory, heap memory is divided into Young Generation and Old Generation, non-heap memory has permanent generation (Permanent Generation), jdk1.8 and later metaspace replace permanent generation.
(2) the younger generation can be divided into Eden and Survivor regions. The Survivor region consists of FromSpace and ToSpace. Eden zone accounts for large capacity, Survivor two zones for small capacity, the default ratio is 8:1:1.
(3) Heap memory use: objects are stored, and the garbage collector collects these objects and then collects them according to the GC algorithm.
(4) non-heap memory use: permanent generation, also known as method area under general hotspot, stores long-term living objects when the program is running, such as class metadata, methods, constants, properties, etc.
Key points:
(1) first, when the Eden area is full, the first GC will be triggered to copy the living objects to the SurvivorFrom area.
Then, when the Eden area triggers GC again, the Eden+From area is scanned, and the two areas are garbage collected. The objects that are still alive after this collection are copied directly to the To area (if the age of any object has reached the old age standard, it is assigned to the old age area), and the age of these objects is + 1.
(2) clear Eden and SurvivorFrom
Then, clear the objects in Eden and SurvivorFrom, that is, there is an exchange after replication, who is empty and who is to
Old (nursing area) is full, open
Full GC = FGC (heavy GC)
If you Full GC many times and find that the old age is full, you will report OOM (heap memory overflow).
Meta-space and permanent generation
Jdk1.8 later began to put the class metadata in the Metaspace, this area belongs to the permanent belt in jdk7 and before, metaspace and permanent generation are used to store class-related information, including the Method,Field of class objects, metaspace and permanent generation are actually the implementation of the method area, but the implementation is different, so the method area is actually just a specification of JVM.
The biggest difference between the two is that the meta-space uses local memory, while the permanent generation uses JVM memory, that is to say, the amount of local memory left can theoretically be as big as metaspace, which solves the problem of insufficient space, but it is impossible to let it grow indefinitely. JVM defaults to dynamically setting its size as needed at run time.
What is GC (generational collection algorithm)
1. Frequently collect Young area on number of times
2. Less Old area is collected in the number of times
3. Basically do not move the Perm area
When JVM does GC, most of the time it is the new generation that is recycled.
Therefore, GC is divided into two types according to the reclaimed area, one is ordinary GC (minor GC), and the other is global GC (minor GC or Full GC).
For the implementation of HotSpot VM, there are only two exact categories of GC in it:
1.Partial GC: the schema of the entire GC heap is not collected, as follows:
Young GC/Minor GC: collect only the new generation of GC.
Old GC: only collect GC from the old days. Only CMS's concurrent collection is in this mode.
Mixed GC: collect the entire Cenozoic and some old GC, only the G1 has this model.
2.Full GC/Major GC: collects the patterns of the entire GC heap, including the patterns of the new generation, the old age, the permanent generation (if any), and so on.
Common garbage collection algorithms
Mark-clear algorithm
First of all, all the objects that need to be recycled are marked, and all the marked objects are recycled uniformly after the marking is completed. It has two main shortcomings: one is the efficiency problem, the two processes of marking and clearing are both inefficient; the other is the space problem, after mark removal, it will produce a large number of discontinuous memory fragments. too much space debris may cause later when the program needs to allocate larger objects, can not find enough continuous memory and have to trigger another garbage collection action in advance.
Replication algorithm
To solve the efficiency problem, a collection algorithm called Copying has emerged, which divides available memory into two equal-sized chunks of capacity, using only one of them at a time. When this piece of memory is used up, copy the surviving objects to another piece, and then clean up the used memory space at once. In this way, the memory of the whole half area is reclaimed every time, and the complex situations such as memory fragments do not have to be considered in memory allocation, as long as the pointer at the top of the stack is moved and the memory is allocated sequentially, which is simple and efficient. But the cost of this algorithm is to reduce the memory to half of the original, which is a bit too high.
Marking-finishing algorithm
The replication collection algorithm will carry out more replication operations when the object survival rate is high, and the efficiency will become lower. More crucially, if you do not want to waste 50% of the space, you need additional space for allocation guarantees to deal with the extreme situation in which all objects in the used memory are 100% alive, so this algorithm generally could not be used directly in the old days.
According to the characteristics of the old years, someone proposed another "mark-organize" (Mark-Compact) algorithm, the marking process is still the same as the "mark-clear" algorithm, but the next step is not to directly clean up the recyclable objects, but to make all the surviving objects move to one end, and then directly clean up the memory beyond the end boundary.
The above content is how to introduce the principles of JVM. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.