JVM memory region partitioning

  • Thread stack
  • Local method stack
  • PC counter
  • The heap area is common to threads, and objects are generally allocated on the heap
  • The method area is also part of the heap that holds the metadata of Class objects, such as symbol references, constant pools, etc. The jdK1.8 method area is implemented as a meta-space, which was the permanent generation before 1.8.
  • The size of the memory in the JVM is equal to the size in the heap + the size out of the heap

Garbage collection algorithm

Mark removal process: GCroot marks all unreachable objects and then clears them. Advantages: Simple and efficient implementation Disadvantages: Easy to occur memory defragmentation process: it is an optimization of the tag clearing algorithm. After the object is cleared, the worker thread will be suspended and then an object is cleared. Advantages: No memory defragmentation disadvantage: stopTheWorld will occur, affecting other worker threads copy copy algorithm flow: Memory 2 is divided into two regions, one used and one free. Objects are allocated in the used space, and then they are also used during scanning. The surviving objects are moved free. Advantages: no memory fragmentation and short object collation time disadvantages: low space utilization The JVM does not directly use the above algorithm, but divides the memory into two Spaces, one is called old and the other is called new. Objects are almost always subgendered, and some large or long-lived objects are moved to the old age. New generation: An optimized replication algorithm is used to divide the yong region into 8:1:1(Eden region: Useed: Free). Each time, mark the used and Eden region, and then move the living object to the free region, clear Eden and used region, and exchange two Pointers, used and free. old age: Because the object of the old age is the survival time is relatively long, the old age uses the mark finishing method. The promotion mechanism

  • The markword in the header of each object has an age field, which is age+1 each time GC is performed and promoted to the old age when age exceeds the threshold.
  • Large objects are allocated directly to the old age
  • Dynamic age judgment: if the age of the object is more than half of the used area, then the age >= changed age of the object is directly promoted

CMS garbage collector

  • Initialization mark
  • Concurrent tags
  • Compensation tag
  • Concurrent cleanup 2

CMS adopts the tag clearing algorithm. If the collection fails due to too much floating garbage, serialOld will be used for collection

G1 collector

The G1 collector is characterized by concurrent collection and predictable collection times.

Class object loading process

  • Load, load the class in binary
  • Verifies whether the CLASS is safe or correct
  • The class field is assigned default values
  • Parse to convert symbolic references to a method into direct references
  • ClInit, the Init method of class, executes static code blocks and assigns values to static fields

Class loader

  • Start the class loader, responsible for the javaHome\lib package
  • Extension class loader, responsible for the javaHome lib ext package
  • System class loader, which is responsible for logging jar packages in the classPath
  • Custom loader

The process of calling a method

Static call: is the common method invocation, the address of the compile time to determine, directly create stack 幁, put the parameters into the local variables of the dynamic invocation: 幁 pressure into the stack is my rewrite, call, according to the actual object of reference to find object according to the method signature traversal method table, if found, then a permission check. If the object can’t find its own method table, find the parent class.

Stack 幁

  • Local variable scale
  • The operand stack
  • Method return address

The JIT compiler

JIT uses hierarchical compilation to interpret execution first, determine if code is hot, and if so, perform a compilation of code to augment code execution. Explain execution advantages: No compile time but execution over slow compile execution advantages: compilation time and code bloat, but execution is fast