JVM-related knowledge summary

JVM-related knowledge summary

1. JVM memory structure and management mechanism

  1. Program Counter : Each thread has a PC register, created when a thread is created. Point to the address of the next instruction. When performing a local method, PC is undefined.
  2. Method Area : store information loaded classes; type constant pool; field method information; Method bytecodes.
  3. Heap memory : new objects are stored in the Java heap; all the threads share the Java heap; of generational GC, the heap is generational; GC major regional management.
  4. Stack memory : thread private, life cycle and the same thread; Stack consists of a series of frames; frame a method of local variables, operand stack, constant pool pointer; every method call to create a frame and pushed onto the stack.
  5. Native method stacks : save the native method to enter the address of the area. Native support for performing the method of storing the state of each native method call.

2. JVM lifecycle

JVM instance of birth:

When starting a Java program, a JVM instance arises, anyone with a public static void main (String [] args) class function can be used as a starting point JVM instance running.

JVM instances run:

main () program as the initial starting point of thread, any other thread started by the thread. Internal JVM has two threads: thread guard and non-daemon thread, main () is non-threaded daemon, a daemon thread normally used by the JVM itself. Java programs can show that you have created a thread as a daemon thread.

JVM instance demise:

When all non-daemon threads proceedings are terminated, JVM only exit; if the security manager allows, the program can also be java.lang.Runtime class or java.lang.System.exit () to exit.

3. JVM garbage collection mechanism

First, determine the GC at what time the trigger , followed by judging whether the object survived the last corresponding use garbage collection algorithm for garbage.

GC at what time the trigger:

GC runs in the lowest priority thread, is generally called when the application is idle. When out of memory will take the initiative to call.

Because the objects are generational process, and therefore garbage collection zone, not the same time. There are two GC:

  1. GC Young : attempt to allocate objects in the young generation Under normal circumstances, if the object allocation fails, it triggers a Young GC.
  2. Full GC : carry out the entire memory consolidation, including the young generation, years old and permanent generations, so the Full GC slower than Young GC, and therefore should minimize the number of Full GC.

Determining whether an object has survived:

  1. Reference counting : heap not create an object, it will create a counter for the object, a default initial value. When other variable is assigned a reference for the object, the value is incremented. When an object or a reference to the instance of death assigned a new value, the counter is decremented.
  2. Reachability analysis algorithm : from GcRoot start, find the corresponding reference node, continues to look for this reference node after node is found, after looking all reference nodes is completed, the remaining nodes Node is considered not to be quoted, that is useless node, the node is judged as useless recyclable objects.

Java can be used as GcRoot include the following categories:

  1. VM stack referenced objects
  2. Method static property class object referenced by region
  3. The method of constant reference object region
  4. Object native method stack applications

Common garbage collection algorithm:

  1. Mark - Clear method : Start search description from GcRoot, surviving objects are marked. After marking, re-scan the entire object is not marked space, the garbage collection. The method is easy to cause memory fragmentation.
  2. Mark - Collation Algorithm : The algorithm and mark - sweep algorithm is the same, but after the completion mark, does not directly recycled to clean up the object, but the object will survive all moved to one end, then clean out the memory beyond the boundary.
  3. Mark - replication algorithm : It memory is divided into two equal size, uses only one of them. A fast when it runs out of memory, and it will also live objects copied to the upper block B, and the memory block A disposable clean out. Although this algorithm is simple, efficient operation and less memory fragmentation, but the use of memory space but then made a high price, because the space can be used to reduce by half. Obviously, the number of live objects with the efficiency of replication algorithms have great relevance, survival if many objects, then the efficiency will be greatly reduced.
  4. Generational collection algorithm : generational collection algorithm is an algorithm currently most JVM's garbage collector uses. The core idea is based on the object alive the memory of the life cycle is divided into several different areas. They will be divided into younger generations, years old and permanent generations. Then use the appropriate algorithm depending on the collection area.

4. Common garbage collector

CMS collector (mark - sweep algorithm)

CMS collection process:

  1. Initial labels (CMS-initial-mark), can cause STW;
  2. Concurrent mark (CMS-concurrent-mark), and user threads run simultaneously;
  3. Precleaning (CMS-concurrent-preclean), run concurrently with the user thread;
  4. May be terminated precleaning (CMS-concurrent-abortable-preclean) threads run concurrently with the user;
  5. Relabeled (CMS-remark), will lead swt;
  6. Concurrent Clear (CMS-concurrent-sweep), to run concurrently with the user thread;
  7. Concurrent reset state waiting for the next trigger CMS (CMS-concurrent-reset), and user threads run simultaneously;

G1 collector

G1 (Garbage-First) is a server for a garbage collector, mainly for the same time with multiple processors and large memory capacity of the machine. High probability to meet the requirements of GC pause times, further comprising a high throughput performance characteristics. . Fully supported in Oracle JDK 7 update 4 and later, the designed for the following applications:

  1. CMS collector can be like, like, operation and application of GC threads together concurrently.
  2. The compact range of free memory and no long GC pauses.
  3. We need predictable GC pause time consuming.
  4. I do not want to sacrifice too much throughput performance.
  5. After starting no need to request a larger Java heap.
Published 17 original articles · won praise 1 · views 3411

Guess you like

Origin blog.csdn.net/ZeroWdd/article/details/104815657