“This is the 10th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
1. Native keywords
Java does not have direct access to the bottom layer of the system (the start0() method we are familiar with is modified by native keyword), so the introduction of native keyword to Java implementation extension, The Java Native Interface (JNI) Interface is used to invoke other languages, such as C and C++, to access the underlying layer.
2. The method of area
Static variables, constants, and class information are stored in the method area, and the runtime constant pool is stored in the method area, but instance variables are stored in heap memory, independent of the method area. (static,final,Class, constant pool)
3. The stack
The stack holds references to the eight basic types, objects
4. Class loading process
The life cycle of the class is shown below
loading
During the loading phase, we can refer to the loadClass() method in java.lang.ClassLoader, which generates a java.lang.Class object representing the Class in the method area, which acts as the entry point for the various data of the Class. This phase mainly ensures that the byte stream of the Class file contains information that meets the requirements of the virtual machine and does not compromise the security of the virtual machine. Validation is important, but not necessary, and has no impact on the program runtime. 3. Preparation The preparation phase is the phase in which memory is formally allocated and initial values are set for class variables (static). The memory used by these variables is allocated in the method area. 4. The parsing phase is the process in which the VIRTUAL machine replaces symbolic references in the constant pool with direct references. 5. Initialization This stage is the last step in the class loading process. In the preparation phase, we have assigned values to class variables (initial 0 value), and in the initialization phase, we initialize variables and other resources based on the code we wrote. Or, the initialization phase is the process of executing the class constructor
() method. The
() method is generated by the compiler automatically collecting all class variable assignments in a class and assignments in static code blocks. The virtual machine ensures that a class’s class constructor
() is locked and synchronized correctly in a multithreaded environment. If multiple threads initialize a class at the same time, only one thread will execute the class’s class constructor
(), and all the other threads will block and wait. Until the active thread completes executing the
() method. It is important to note that other threads will block, but will wake up after
() because a type will only be initialized once under the same classloader.
5. Object instantiation process
Instantiating an object of a class is a typical recursive process, as shown in the figure below.
Before an Object is ready to be instantiated, the parent class of the Object is first instantiated, and if the parent class of the Object has another parent class, then the parent class is instantiated upward until the Object class is recursively instantiated. After instantiating the Object class, instantiate the classes down.
5.1 Take a small example
Student student = new Student();
Copy the code
- When the virtual machine gets a new command, check the method area first
Whether class information for the object exists
And,Check that the class is loaded, parsed, and initialized
(1) If there is no information about this class in the method area, then it will throw ClassNotFoundException (2) If all the above checks pass, then enter the next step 2. After the class is loaded, the virtual machine allocates memory for the new object. 3. Allocate a block of memory from the heap for the new object. 5. Set and save Object Header information, including which class instance the Object is, the hash code of the Object, the GC generation age of the Object, etc. 6. Normally, a new instruction is followed by a method that initializes the object according to the constructor specified by the programmer
6. The heap
The heap
Contains objects, including young generations and old generations,The permanent generation
Contains method areas in JDK1.8 (replaced by meta-spaces)
6.1 Generation Concept
When Eden space is full, Minor GC is triggered and surviving objects are moved to Survivor0. When Eden space is full again, Minor GC is triggered and surviving objects in Survivor0 are moved to Suvivor1. This ensures that one survivor zone is always empty (empty to, non-empty FROM) for a period of time. Objects that are still alive after 15 Minor GC moves to the old age. The old generation stores objects that live for a long time, and when they are Full, Major GC=Full GC is triggered. During GC, all threads are stopped waiting for GC to complete. Therefore, applications with high response requirements should minimize the occurrence of Major GC to avoid response timeout.
Minor GC
: Clean up the younger generationMajor GC
: Clean up the old daysFull GC
: Cleans the entire heap space, including young and permanent generations
6.2 Why Generation?
Objects are classified according to survival probability, and objects with long survival time are placed in a fixed area to reduce garbage scanning time and GC frequency. Different garbage collection algorithms for classification are carried out to maximize the advantages and avoid the disadvantages of the algorithm.
6.3 Why is survivor divided into two equally sized survivor Spaces?
The main purpose is to deal with fragmentation. GC is triggered if memory fragmentation is severe, that is, two objects occupy a discontinuous amount of memory and there is not enough contiguous memory for new objects.
6.4 Common Parameters in JVM heap memory
parameter | describe |
---|---|
-Xms | Heap memory initialization size |
-Xmx | Maximum allowed size of heap memory, generally not more than 80% of physical memory |
-XX:+PrintGCDetail | Prints GC details |
-XX:PermSize | The initial size of non-heap memory, usually 200M |
-XX:MaxPermSize | Maximum size allowed for non-heap memory |
-Xns | Young generation memory initialization size |
-Xmn | Maximum memory size allowed for young generation memory |
-XX:SurvivorRatio=8 | Ratio of the Eden zone to the Survivor zone. The default value is 8 |
-Xss | Stack memory size |