Java JVM Detailed:
Related knowledge about the JVM
One, heap memory and stack memory
1, the stack memory in the JVM is mainly stored in the basic types of variables and object references
2. Heap memory in the JVM primarily stores objects and arrays created with new, variable long strings (StringBuilder and stringbuffered) are stored in heap memory
The advantage of using a heap is the dynamic allocation of storage space, more flexible, but the disadvantage is that because of the dynamic allocation of memory, so the storage speed is slow, and the use of stack speed is relatively fast, but also to achieve the sharing of data, but the disadvantage is that the data size and lifetime of the stack must be determined, lack of flexibility
3, static storage allocation is stored static variables and static code block
Second, the JVM's understanding
The JVM, the Java Virtual machine, masks the information associated with the specific operating system platform so that the Java program generates only the target code (bytecode) that runs on the Java virtual machine, so that it can be run across platforms;
The principle is that Java source files are compiled into bytecode program by Java compiler, and each instruction is translated into machine code of different platform through JVM, running through a specific platform.
The memory areas of the JVM are mainly divided into: Method area, JVM stack, heap, local method stack, program counter
Program counter: Used to record the current execution of the instruction, which is the only area where there is no oom;
JVM Stacks: Threads are private, and each thread creates a JVM stack that holds the local base variable in the current thread, partially returns the result, stack frame, and the reference address of the object;
Heap: thread-sharing, used to store objects and arrays; Since sharing, you need to lock it, so it costs a lot;
Method Area: This method area corresponds to the persistence generation, it holds the information of the class (name, modifier, etc.), static variables in the class, constants in the class with final definition, and so on;
Local method stack: Used to support the execution of the native method to store the invocation state of each native method;
Java garbage collection is mainly for heap and method areas: The heap is divided into Cenozoic and old age, generally just new objects will be put into the Cenozoic, and the Cenozoic is divided into Eden and two survivor areas;
Garbage collection mechanism is: first of all to determine which objects are garbage, that is no longer used, and then use the corresponding algorithm (tag-clearing algorithm, replication algorithm, tag-collation algorithm, the collection algorithm) to recycle garbage;
1, Mark-Clear algorithm:
The two phases, the marking stage and the scavenging stage, first mark the object that needs to be reclaimed, and then reclaim the space occupied by the tagged object;
Its implementation is relatively simple, but the disadvantage is easy to generate memory fragmentation, resulting in the subsequent need for large objects to allocate space can not find enough memory to trigger a new garbage collection action in advance;
2. Copy algorithm:
To solve the shortcomings of the tag-purge algorithm, the replication algorithm it divides memory by capacity into two areas of equal size, using only one piece at a time, and when a piece is used up, copies the surviving object to another area and then clears the used area so that it is not prone to fragmentation;
Solves the problem of memory fragmentation, but the disadvantage is to reduce the memory used to the original half, and the efficiency of replication with the number of objects to survive, when the number of large, the efficiency greatly reduced;
3, marking-finishing algorithm
In order to solve the defect of the replication algorithm, the mark-collation algorithm is born, the marking phase is the same as the mark-purge algorithm, the object that needs to be recycled is marked, but it is not directly recycled, but it moves the surviving object to the other side, then clears the memory outside the boundary.
4, the collection of generation algorithm
This is the most used algorithm, and its core idea is to divide the memory into several different regions based on the lifetime of the object. In general, the heap area is divided into Cenozoic and old era, the characteristics of the old age is that each garbage collection needs to be recycled less objects, and the new generation of more, so take a different algorithm ;
At present, most of the new generation is the replication algorithm, but in fact, is not based on the proportion of 1:1 to divide the new generation of space, in general, the Cenozoic is divided into a larger Eden space and two small survivor space, each use of Eden Space and one of the survivor space, When recycling is done, copy the surviving objects from Eden and survivor to another survivor space, and then clear out Eden and the survivor space that you just used.
And because the old age is characterized by a collection of only a small number of objects, the general use of the tag-collation (mark-compact) algorithm.
Note that there is another generation outside the heap area that is the permanent generation (permanet Generation), which is used to store class classes, constants, method descriptions, and so on. Recycling for a permanent generation mainly reclaims two parts: discarded constants and unwanted classes.
So how do we know what the object is "rubbish"?
Method One, reference counting method:
In Java, references are associated with objects, meaning that if you want to manipulate an object, you must do so by reference. So it's obvious that a simple way is to determine whether an object can be recycled by reference counting. In general, if an object does not have any references associated with it, the object is not likely to be used elsewhere, and the object becomes a recyclable object. This way becomes a reference counting method.
Advantages: Simple to achieve, high efficiency
Disadvantage: Unable to resolve circular reference problem
Method Two, 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, but it should be noted that an object that is judged to be unreachable does not necessarily become a recyclable object. Objects that are judged to be unreachable must undergo at least two markup processes, and if they still do not escape the possibility of being recyclable in the two-time markup process, they are essentially recyclable objects.
Which objects can become GC roots?
The object referenced in the 1.JVM stack (the local variable table in the stack frame).
2. The object referenced by the class static property in the method area.
3. Objects referenced by constants in the method area
4. Objects referenced by JNI (that is, the commonly said native method) in the local method stack.
For programmers, there are ways to reduce the cost of GC:
1, do not display to call the System.GC () method
2, minimize the use of temporary objects
3, the object is not used when the display is set to NULL
4, try to use StringBuilder instead of string accumulation strings
5, can use the basic type of variable (int long), do not use objects (Integer, long)
6. Use static object variables as little as possible
Thank you for reading, I hope to help you, thank you for your support for this site!