JVM learning (2) JVM memory description




I. class method

Class methods are static methods, which need to be decorated with static modifiers. The contents of variables cannot be involved in a class method
1. Cannot call object method of class
2. Object variables cannot be referenced
3. Class methods cannot be overridden (overridden)
4. Super and this keywords cannot be used for class methods
II. Example method

1. The object method of the class can be called
2. Object variables can be referenced
3. Instance methods can be rewritten (overwritten)
4. The instance method can use the super and this keywords

Class method is allocated memory address when loading class, so it can be called by any object after loading, and can be called directly by class name (class name. Method name), while instance method needs to be allocated memory address after creating object

Original link: https://blog.csdn.net/tiansheshouzuo/article/details/86188239




1 JVM runtime principle

The main function of Java visual machine is to divide the memory involved into different areas when executing Java programs. The objects stored in each area are created and destroyed at different times according to their uses. Among them, the method area and heap are created when the JVM is started, which are shared by all threads; while the virtual machine stack, local method stack and program counter are created and destroyed by the user thread.



The JVM is a stack based virtual machine. The JVM allocates a stack for each newly created thread. That is to say, for a java program, its operation is completed by the stack operation. The stack saves the state of the thread in frames. The JVM only performs two operations on the stack: stack pressing and stack out in frames.
The JVM executes the class bytecode. After the thread is created, the program counter will generate the program counter (PC) and stack. The program counter stores the offset of the next instruction to be executed in the method. The stack stores one stack frame, each stack frame corresponds to each call of each method. The stack frame is composed of two parts: the local variable area and the operand stack. The local variable area is used for storing The local variables and parameters in the method, and the operand stack are used to store the intermediate results generated during the execution of the method.

2 JVM memory details
2.1 method area

It holds method code (compiled Java code) and symbol tables. It stores class information, static variables, constants of final type, property and method information to be loaded. The JVM uses the permanet generation to store the method area. You can specify the minimum and maximum values through – XX: permsize and – XX: maxpermsize.

2.2 heap memory

The memory of all objects created through new is allocated in the heap, and its size can be controlled by – Xmx and – XMS.
The operating system has a linked list that records the address of free memory. When the system receives the application of the program, it will traverse the linked list to find the first heap node whose space is larger than the applied space, then delete the node from the linked list of free nodes and allocate the space of the node to the program. In addition, for most systems, it will record the book at the first address in this memory space The size of the secondary allocation, so that the delete statement in the code can correctly free this memory space. However, since the size of the heap node found is not exactly equal to the size of the application, the system will automatically put the redundant part back into the free list. At this time, the memory allocated by new is generally slow and prone to memory fragmentation, but it is the most convenient to use. In addition, under windows, the best way is to use virtualalloc to allocate memory. It is not in the heap, nor in the stack, but to directly reserve a piece of memory in the address space of the process. Although this method is the most inconvenient, it is fast and flexible. Heap memory is a data structure extended to high address, and it is a discontinuous memory area. Because the system uses linked list to store the free memory address, it is naturally discontinuous, and the traversal direction of linked list is from low address to high address. The size of the heap is limited by the virtual memory available in the computer system. It can be seen that the heap can get more flexible and large space.
The heap area is the largest in the JVM,The application objects and data exist in this area, which is also shared by threads and the main recycling area of GC. A JVM instance only has one heap class store, and the size of heap memory can be adjusted. After the class loader reads the class file, it needs to put the class, method and constant variable into the heap memory to facilitate the execution of the actuator. The heap memory is divided into three parts:



① newborn area
The new area is the area of the birth, growth and extinction of the class. A class is generated, applied, and finally collected by the garbage collector to end its life. The newborn area is divided into two parts: Eden space and survivor space. All classes are new in Eden. There are two surviving areas: 0 area (survivor 0 space) and 1 area (survivor 1 space). When the space of the Eden is used up, the program needs to create objects again. The garbage collector of the JVM will garbage collect the Eden (minor GC), and move the remaining objects in the Eden to the surviving area 0. If the remaining zone 0 is also full, garbage collection will be carried out for the zone, and then it will be moved to zone 1. What if 1 is full? Then move to the elderly care area. If the elderly care area is full, then a major GC (fullgcc) will be generated at this time to clean the memory of the elderly care area. If the elderly care area finds that it is still unable to save the object after executing the full GC, an oom exception “outofmemoryerror” will be generated.
If java.lang.outofmemoryerror: Java heap space exception occurs, the heap memory of Java virtual machine is insufficient. There are two reasons:
A. the heap memory setting of Java virtual machine is not enough, which can be adjusted by parameters – XMS, – Xmx.
B. a large number of large objects are created in the code and cannot be collected by the garbage collector for a long time (there are references).
② elderly care area
The pension area is used to store Java objects screened from the new area. Generally, pool objects are active in this area.
③ permanent area
Permanent storage area is a resident memory area, which is used to store the metadata of class and interface carried by JDK itself, that is to say, it stores the class information necessary for the running environment. The data loaded into this area will not be recycled by the garbage collector. Only when the JVM is closed can the memory occupied by this area be released.
If java.lang.outofmemoryerror: permgen space appears, it means that the Java virtual machine does not have enough memory settings for permanent generation perm. There are two reasons:
A. a large number of third-party jar packages need to be loaded for program startup. For example: too many applications have been deployed under a Tomcat.
B. a large number of classes generated by dynamic reflection are constantly loaded, resulting in the perm area being filled.
JDK1.6 and earlier: constant pool allocation in permanent generation.
Jdk1.7: Yes, but it has gradually “gone to permanent generation”.



JDK1.8 and later: none (java.lang.outofmemoryerror: permgen space, this kind of error will not appear in JDK1.8).



2.3 virtual machine stack

What is the stack
Stack is also called stack memory, which is in charge of running Java programs. It is created when a thread is created. Its life cycle follows the life cycle of the thread. When the thread ends, the stack memory will be released. For the stack, there is no garbage collection problem. As long as the thread ends the stack, the stack will be over. The life cycle is the same as the thread, and the thread is private.
Basic types of variables and object reference variables are allocated in the function’s stack memory.








What does the stack store?
Three types of data are stored in the stack frame:
Local variables: input and output parameters and variables within the method;
Operation stack: record the operations of stack out and stack in;
Frame data: including class files, methods, etc.


2.4 program counter

Each thread has a program calculator, which is a pointer to the method bytecode (the next instruction code to be executed) in the method area. It is a very small memory space for the execution engine to read the next instruction.
In Java, the reachability algorithm is used as the counter: a series of “GC roots” are used as the starting point to search down from the node, and the path is called the reference chain. When an object does not have any reference chain connected to the GC roots, that is, it is not available when the object is proved.
Objects that can be used as gcroots in Java include:
1) objects referenced in virtual machine stack (local variable table in stack frame);
2). Objects referenced by class static attributes in method area;
3). Objects referenced by constants in the method area;
4). JNI referenced objects in the local method stack
2.5 local method

It is used to support the execution of native methods and stores the state of each native method call. For the local method interface, the implementation of the JVM does not require its support, or even no support at all. Sun’s implementation of Java local interface (JNI) is for the sake of portability. Of course, we can also design other local interfaces to replace sun’s JNI. But these designs and implementations are more complex. We need to make sure that the garbage collector does not release the objects that are being called by local methods.
2.6 summary




3 example

3.1 source code


package com.spark.jvm;
 public class HelloJVM {
     //When the JVM is running, it will find the entry method main in the method area through reflection
     The public static void main (string [] args) {// main method is also placed in the method method area
          *Students (lowercase) are placed in the stack area of the main thread
          *The student object instance is placed in the heap area shared by all threads
         Student student = new Student("spark");
          *First, through the student pointer (or handle) (the pointer points directly to the object in the heap, the handle indicates that there is an intermediate, student points to the handle, and the handle points to the object)
          *Find the student object. When the object is found, it will call the specific method to execute the task by pointing to the pointer in the method area inside the object
 class Student {
     //Name itself is placed in the stack area as a member, but the string object pointed to by name is placed in heap
     private String name;
     public Student(String name) {
         this.name = name;
     //Sayhello this method is placed in the method area
     public void sayHello() {
     System.out.println("Hello, this is " + this.name);

3.2 description