In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "what is the underlying principle of JVM virtual machine". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Definition:
The first is an area shared by all Java virtual machine threads, which stores some information about the root class structure, classful variables, method information, constructor and constructor information, and some special methods (mainly class constructors). The method area is created when the virtual machine starts, which is logically part of the heap, but does not force your specific location. (to put it bluntly, different JVM vendors do not necessarily create according to the standard when implementing.) finally, for the memory definition: the method area will also throw OutOfMemoryError when there is insufficient memory.
Explanation:
After talking for a long time, some people may not understand, so explain it!
Take oracle's hospost virtual machine for example (that is, what we use every day).
Composition:
Before JDK8, the implementation of the opposite normal area of the hospost virtual machine is called permanent generation, which is implemented using part of the space of the heap.
After JDK8, the permanent generation is removed, and the implementation is replaced. The meta-space is not the heap memory, but the local memory, that is, the operating system memory.
Therefore, different implementations have different selection locations for the method area.
Overflow in the method area:
Some people may wonder, doesn't the method area just store some information and instances of the class? The size is not enough for memory overflow, ah, what is the specific situation, take an example to see
/ * * Metaspace * Demo Metaspace memory overflow java.lang.OutOfMemoryError: Metaspace *-XX:MaxMetaspaceSize=8m * / public class Demo1_8 extends ClassLoader {/ / ClassLoader can be used to load the class's binary bytecode public static void main (String [] args) {int j = 0; try {Demo1_8 test = new Demo1_8 () For (int I = 0; I < 10000; iConstructions, jacks +) {/ / ClassWriter function is to generate the binary bytecode ClassWriter cw = new ClassWriter (0) of the class / / version number, public, class name, package name, parent class, interface cw.visit (Opcodes.V1_8, Opcodes.ACC_PUBLIC, "Class" + I, null, "java/lang/Object", null) / / return byte [] byte [] code = cw.toByteArray (); / / load test.defineClass of the class ("Class" + I, code, 0, code.length) / / Class object}} finally {System.out.println (j);} code comments, self-understanding, due to the large physical memory of the computer, it is not convenient to demonstrate the effect, so add-XX:MaxMetaspaceSize=8m parameter
JVM1.6 execution result: permanent memory overflow causes OutOfMemoryError
After JVM1.8, it causes metaspace memory overflow java.lang.OutOfMemoryError: Metaspace
Although they are all OutOfMemoryError, you can see that before 1.8, there is permGen space 1.8, and then Metaspace.
There are many such scenarios, and those who have seen the spring mybatis source code should understand that improper use of the code can easily cause overflows in the method area.
Run time Pool:
Before you understand the running constant pool, let's first talk about what a constant pool is.
A constant pool is a table according to which virtual machine instructions find the class name, method name, parameter type, literal quantity and other information to be executed.
The constant pool is in the *. Class file. When this class is loaded, its constant pool information will be put into the runtime pool and the symbolic address inside will be changed to the real address.
After understanding the runtime pool, I have to say that StringTable is an important part of the runtime pool.
Say that StringTable will look at some classic interview questions first.
String S1 = "a"; String S2 = "b"; String S3 = "a" + "b"; String S4 = S1 + S2; String S5 = "ab"; String S6 = s4.intern (); / / ask System.out.println (S3 = = S4); System.out.println (S3 = = S5); System.out.println (S3 = = S6); String x2 = new String ("c") + new String ("d"); String x1 = "cd"; x2.intern () / / ask, if you change the position of [the last two lines of code], what if it is jdk1.6? System.out.println (x1 = = x2); explanation: ① S3 = = S4 / / falses3 = "a" + "b" two strings plus stringtable will have compile-time optimization. The result is that the "ab" (string ab) constant pool is not available, by the way. S4 = S1 + S2 two variables splicing at run time use stringbuilder splicing to produce a new string equivalent to new String ("ab"), so the first question in the heap is false: one in the constant pool and one in the heap memory ② S3 = = S5 / / trues5 = "ab" is an independent variable that first checks the contents of the constant pool. As a result, the constant pool already has a S3 "ab". Therefore, S5 will not create new objects, but will directly refer to the constant pool to have some objects, so S3 and S5 are both the same object, so for true ③ S3 = = S6 / / trues6, call the s4.intern () method intern () method to go back to the constant pool to see if there is this object first. If there are objects that return to the constant pool, if not, try to pool S4. Obviously, the constant pool already has ab and failed to enter the pool successfully. But he returns objects in the constant pool, so S6 and S3 are one object, so true ④ x1 = = x2 / / falsex2 is obviously the object new String ("cd") in the heap. X1 is obviously an object in the constant pool x2 calls the intern method to try to pool x2 in the heap memory, but the constant pool already exists, so it is not successful in the pool, so in the end x2 is the object in the heap memory x1 is the object in the constant pool and the result is the falseStringTable property.
A string in a constant pool is just a symbol and becomes an object the first time it is used.
Use the mechanism of string pool to avoid repeated creation of string objects
The principle of string variable concatenation is StringBuilder.
The principle of string constant splicing is compilation-time optimization.
You can use the intern method to actively put string objects that are not already in the string pool into the string pool.
1.8 try to put the string object into the string pool. If there is any, it will not be put into the string pool. If not, the object in the string pool will be returned.
1.6 try to put the string object into the string pool. If there is one, it will not. If there is no copy of the object and put it into the string pool, the object in the string pool will be returned.
StringTable location
Before JVM1.6, stringtable was in the method area, which is one of the spaces of the permanent generation, and after 1.71.8, his implementation space was moved to the heap memory.
Why did it change? What's the reason?
Because permanent generation is out of memory, and permanent generation will only perform its garbage collection when FullGC is triggered, but FullGC will not be triggered until the shortage of space throughout the old era, and the collection time will be very late, which indirectly leads to the low collection efficiency of stringtable, so the JVM manufacturers after 1.6have optimized it.
This is the end of the content of "what is the underlying principle of the JVM virtual machine". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.