One, JVM memory distribution
1. JVM memory model
The JVM memory model mainly includes: class loader, runtime data area, local method library, local method interface, execution engine and garbage collector
The runtime data area mainly includes: [thread exclusive]: virtual machine stack, local method stack, program counter, [thread sharing]: heap, method area
2. Class loader
Class loader is used to lazily load bytecode files (.class files) to prepare for program execution
If the class required by the program to perform a certain behavior has not been loaded into memory, it will go through three steps to complete the initialization of the class
- Load: load the bytecode file into the memory
- connection
- Verification: Whether the bytecode file has the correct structure
- Preparation: allocate some information of the class to the memory area
- Resolution: Replace the symbolic reference in the binary data of the class with a direct reference
- Initialization: Create an instance of the object
The class loader of JVM is divided into
- BootstrapClassLoader (startup class loader): Load the JRE runtime class, located in $JAVA_HOME/jre/lib/rt.jar
- ExtClassLoader (extended class loader): Load JRE extension classes, located in the $JAVA_HOME/jre/lib/ext directory
- AppClassLoader (application class loader): Load the class of the Java program
- User ClassLoader (Custom Loader)
Parental delegation model: When the class loader loads a class, first try to be loaded by the parent class loader, if the parent class loader cannot complete it, then the child class loader will complete it
Sandbox security mechanism: Due to the existence of the parent delegation model, we cannot create a class similar to the JDK itself (the full class name is exactly the same)
Destruction of the parent delegation model: Apache Tomcat is a web container used to deploy Web applications. It destroys the parent delegation model. The loading of classes is directly handed over to the Web application class loader (WebappClassLoader) to complete. If the Web application class is loaded The device (WebappClassLoader) cannot be completed, and it is completed by the CommonClassLoader (CommonClassLoader). The main purpose of this is to ensure that each web application is independent of each other, and will not cause confusion due to the dependence on the same class (full class name) and different versions
Tomcat's class loader is divided into
- BootstrapClassLoader: Load the basic JVM environment class, located in the $JAVA_HOME/lib directory
- SystemClassLoader: Load the Tomcat startup class, located in the $CATALINA_HOME/bin directory
- CommonClassLoader: Load Tomcat's common classes, located in the $CATALINA_HOME/lib directory
- WebappClassLoader: Load the classes of Web applications, located in the WEB-INF/lib directory and WEB-INF/classes directory. Only accessible to the current web application
3. Virtual machine stack
Java Stack, mainly used to store basic data types and instance object references, etc.
4. Local method stack
Native Method Stack, similar to Java Stack, is prepared for running native methods
5. Program counter
Program Counter Register, used to save the next instruction to be executed
6, heap
Heap, mainly used to store object instances, arrays and string constant pools, etc.
The main area of garbage collection is divided into new generation and old generation. The default ratio is 1:2, which can -XX:NewRatio=2
be adjusted by modifying JVM parameters
The Cenozoic is divided into Eden Zone, From Zone and To Zone. The default ratio is 8:1:1. Can -XX:SurvivorRatio=8
be adjusted by modifying JVM parameters
The GC that occurs in the young generation is called Minor GC, the GC that occurs in the old generation is called Major GC, and the GC that occurs in the entire heap is called Full GC (including the recovery of the permanent generation)
7. Method area
Method Area, mainly used to store class information (static variables, construction methods, method declarations, etc.) and runtime constant pool (constants, etc.)
The JVM specification describes the method area as a logical part of the heap, but it has an alias called Non-Heap (non-heap), the purpose is to distinguish it from the heap
On the HotSpot virtual machine, the method area is also called the permanent generation (Permanent Generation). This is just because HotSpot uses the permanent generation to implement the method area. On other virtual machines (BEA JRockit, IBM J9, etc.), There is no concept of permanent generation. However, this implementation approach likely to have experienced (OOM) out of memory, you can modify the JVM parameters [JDK7] -XX:PermSize=???
and -XX:MaxPermSize=???
[JDK8] -XX:MetaspaceSize=???
and -XX:MaxMetaspaceSize=???
to adjust