Java jvm details:
Related knowledge about jvm
1. Heap and stack memory
1. The stack memory in jvm mainly stores references to basic types of variables and objects.
2. The heap memory in jvm mainly stores objects and arrays created with new, and variable-length strings (StringBuilder and StringBuffered) are stored in heap memory.
The advantage of using the heap is that it is dynamically allocated storage space, which is more flexible, but the disadvantage is that it is slower to dynamically allocate memory; while using the stack is faster, and data sharing can be achieved, but the disadvantage is that the data size and lifetime in the stack must be determined and lack flexibility
3. Static storage allocation is used to store static variables and static code blocks.
2. Understanding of jvm
jvm is a java virtual machine. It blocks information related to the specific operating system platform, so that the java program only generates object code (byte code) running on the java virtual machine, so that cross-platform operation can be achieved;
Its principle is: the java source file is compiled into bytecode programs through the java compiler, and each instruction is translated into machine code of different platforms through jvm, and run through a specific platform;
The memory area of jvm is mainly divided into: method area, jvm stack, heap, local method stack, program counter
Program counter: used to record the instruction currently executed, which is the only area without oom;
jvm stack: thread is private. Each thread will create a jvm stack at the same time. It stores local basic variables in the current thread, partial return results, stack frame, and object reference address;
Heap: thread sharing, used to store some objects and arrays; since it is shared, locks are required, which leads to high overhead;
Method area: This method area corresponds to a persistent generation, which stores the information of the class (name, modifier, etc.), static variables in the class, constants defined in the class with final, etc.;
Local method stack: used to support the execution of native methods and to store the call status of each native method;
Java garbage collection mainly focuses on the heap and method areas: the heap is divided into the new generation and the old generation, and generally the objects that have just been new will be placed into the new generation; and the new generation is divided into the Eden area and two Survivor areas;
The mechanism of garbage collection is: first determine which objects are garbage, that is, they are no longer used, and then use the corresponding algorithms (mark-clearing algorithm, copying algorithm, mark-tidying algorithm, generational collection algorithm) to collect the garbage;
1. Mark-clearing algorithm:
It is divided into two stages, the marking stage and the clearing stage. First, mark the object that needs to be recycled, and then recycle the space occupied by the marking object;
Its implementation is relatively simple, but its disadvantage is that it is easy to generate memory fragments, resulting in the inability to find enough memory when allocating space for large objects in the future, and triggering a new garbage collection action in advance;
2. Copy algorithm:
In order to solve the shortcomings of the mark-cleaning algorithm, the copy algorithm divides the memory into two areas of equal size according to capacity, and only one of them is used at a time. After one piece is used up, the still-surviving object is copied to another area, and then the used area is cleaned up, so that fragmentation is not easy to occur;
The problem of memory fragmentation is solved, but the disadvantage is that the memory used is reduced to half of the original, and the copying efficiency is related to the number of surviving objects. When the number is large, the efficiency is greatly reduced;
3. Marking-organization algorithm
In order to solve the defects of the copy algorithm, the mark-tidy algorithm was born, and the marking stage was also like the mark-cleaning algorithm. First, the objects that need to be recycled are marked out, but it does not directly recycle them, but moves all the surviving objects to the other side, and then cleans up the memory outside the boundary;
4. Generational collection algorithm
This is the most commonly used algorithm at present. Its core idea is to divide the memory into several different areas according to the survival cycle of the object. Generally, the heap area is divided into the new generation and the old generation. The characteristic of the old generation is that there are fewer objects that need to be recycled every time the garbage is collected, while there are more of the new generation, so different algorithms are adopted;
At present, most of the new generation use copy algorithms, but in fact, the new generation is not divided into a 1:1 ratio. Generally speaking, the new generation is divided into a larger Eden space and two smaller Survivor spaces. Each time the Eden space and one of the Survivor spaces are used, when recycled, the still-surviving objects in Eden and Survivor are copied to another Survivor space, and then Eden and the Survivor spaces that have just been used are cleaned.
Because the old age is that only a small number of objects are recycled every time, the Mark-Compact algorithm is generally used.
Note that there is another generation outside the heap area, which is Permanet Generation, which is used to store class classes, constants, method descriptions, etc. The recycling of permanent generation mainly recycles two parts: discarded constants and useless classes.
So how do we determine which object is "garbage"?
Method 1. Reference counting method:
In Java, it is associated with objects through references, that is, if you want to operate objects, it must be done through references. Then it is obvious that an easy way is to judge whether an object can be recycled by reference counting. Without losing generality, if an object has no references associated with it, it means that the object is basically unlikely to be used elsewhere, and then the object becomes a recyclable object. This method becomes the reference counting method.
Advantages: Simple implementation and high efficiency
Disadvantages: Unable to solve the problem of circular references
Method 2. Accessibility analysis method:
The basic idea of this method is to search through a series of "GC Roots" objects as the starting point. If there is no accessible path between "GC Roots" and an object, the object is said to be unreachable. However, it should be noted that the object judged to be unreachable may not necessarily become a recyclable object. An object that is judged as unreachable must go through at least two marking processes to become a recyclable object. If there is still no possibility of becoming a recyclable object during these two marking processes, it will basically become a recyclable object.
Which objects can be GC Roots?
1. Object referenced in jvm stack (local variable table in stack frame).
2. Object referenced by class static attributes in the method area.
3. Objects referenced by constants in the method area
4. Object referenced by JNI (that is, the general Native method) in the local method stack.
For programmers, we can also reduce GC overhead through some methods:
1. Do not display the system.gc() method
2. Minimize the use of temporary objects
3. When the object is not in use, the display setting is set to null
4. Try to use StringBuilder instead of String accumulated strings
5. If you can use basic types of variables (int long), don’t use objects (Integer, Long)
6. Use static object variables as little as possible
Thank you for reading, I hope it can help you. Thank you for your support for this site!