Java JVM: Garbage Collector and Memory Allocation Strategy (2)

  When it is necessary to troubleshoot various memory overflows and memory leaks, and when garbage collection becomes a bottleneck for the system to achieve higher concurrency, we need to monitor and adjust these

1. The subject is dead

  • Reference counting algorithm: when there is a reference, the counter is incremented by one, and when the reference is invalid, the counter is decremented by one, and mutual references cannot be recycled

  • accessibility analysis

    • The root object of GC Roots is used as the starting node set, and the path traveled by the search process is called "reference chain"
    • If there is no reference link between an object and GC Roots, and the object is unreachable, it proves that the object cannot be used anymore
    • GC Roots object
      • Objects referenced in the virtual machine stack: parameters, local variables, temporary variables, etc.
      • Objects referenced by static properties in the method area, objects referenced by constants: reference type static variables, references in the string constant pool
      • Object referenced by JNI in the native method stack
      • References inside the Java virtual machine, all objects held by synchronization locks, etc.
  • Let's talk about citations

    • Strong reference: the reference assignment that is ubiquitous in the program code, the garbage collector will never recycle the referenced object
    • Soft references: objects that are still useful but not necessary, and these objects are included in the recycling scope for the second recycling
    • Weak references: non-essential objects that survive until the next garbage collection occurs
    • Phantom reference: The weakest reference relationship, an object instance cannot be obtained through a virtual reference, and a system notification is received when recycling
  • live or die

    • The real death needs to be marked twice: the first time there is no reference chain, the finalize() method can be saved once
  • Recycling method area

    • Method area garbage collection mainly recycles two parts: obsolete constants and types that are no longer used
    • Obsolete constant: not referenced anywhere else
    • Unused types: all instances have been recycled, the class loader that loaded the class has been recycled, and the java.lang.Class object is not referenced anywhere

2. Garbage collection algorithm

  • generational collection theory
    • The Weak Generational Hypothesis: Life and Death
    • Strong Generational Hypothesis: Objects that survive more garbage collections are more difficult to perish
    • Cross-generational reference hypothesis: There are cross-generational references between objects
    • Design Principles
      • Divide different areas according to age, put them together (only pay attention to a small number of survivors), and put together objects that are difficult to perish (recycle this area with a lower frequency)
    • Minor GC (new generation collection), Major GC (old generation collection, CMS), Full GC, Mixed GC (mixed collection, G1)
    • Memory set: Divide the old generation into several small blocks, identify which piece of memory in the old generation will have cross-generational references, and add the small memory objects with cross-generational references to the GC Roots scan during Minor GC
  • Mark-clear algorithm: the most basic garbage collection algorithm, unstable execution efficiency, memory space fragmentation problem (there is not enough continuous memory for the next allocation)
  • Mark-Copy Algorithm: Divide into two pieces, copy the surviving piece to the other piece when one piece is used up, and then clean up the first piece, the memory will be reduced to half of the original
    • Most Java virtual machines give priority to using this algorithm to recycle the new generation, and generally cannot directly use this algorithm in the old generation
    • Appel-style recycling: The new generation is divided into a larger Eden space and two smaller Survivor spaces. The ratio of Eden to Survivor is 8:1. An allocation guarantee is required to avoid overcapacity
  • Mark-sorting algorithm: Surviving objects move to one end of the memory space, and then directly clean up the memory outside the boundary
    • If you move, memory recovery will be more complicated, if you don't move, memory allocation will be more complicated
  • And muddy type: usually use the mark-clear algorithm, and use the mark-sort algorithm when the fragmentation is large

3. Implementation of HotSpot algorithm details

  • root node enumeration
    • The OopMap data structure stores object references: when the class loading action is completed, HotSpot will calculate the type of data at the offset in the object, which can be used directly when scanning
  • Safety point: When GC Roots is enumerated, the thread needs to be paused. If the safety point is set, it can only stop after reaching the safety point.
    • Active interrupt: set a flag bit, each thread polls, once the interrupt flag is found, the active interrupt is suspended at the nearest safe point
  • Safe area: Threads cannot go to a safe place to interrupt and suspend themselves, so they need to introduce a safe area (the thread must leave the safe area and check whether the virtual machine has completed root node enumeration) memory set and card
    table
    • Memory set: used to avoid adding the entire old generation to the GC Roots scanning range
    • Card table: card precision (record is accurate to a memory area)
      • Dirty: There is a cross-generation pointer in the field of an object in the card page, and the flag will become 1, which means it becomes dirty
  • Write barrier: maintain the state of the card table
    • The write barrier before the assignment is called the pre-write barrier, and the post-write barrier after the assignment
  • Concurrent reachability analysis
    • three color mark
      • White: Indicates that the object has not been accessed by the garbage collector
      • Black: Indicates that the object has been visited by the garbage collector and all references have been scanned
      • Gray: Indicates that the object has been accessed by the garbage collector, and there is at least one reference that has not been scanned
    • Fix disappearing objects on concurrent scans: incremental updates, original snapshots
      • Incremental update: black object changes back to gray object after inserting reference to white object (CMS)
      • Original snapshot: Regardless of whether the reference relationship is deleted or not, the search will be performed according to the snapshot of the object graph at the moment when the scan is just started (G1, Shenandoah)

Fourth, the classic garbage collector

insert image description here

  • Serial collector
    • The most basic, oldest, single-threaded collector
    • Default young generation collector in client mode
  • ParNew collector
    • The multi-threaded parallel version of the Serial collector, the HotSpot virtual machine running in server mode
    • ParNew is the default new generation collector after activating CMS (-XX: +UseConcMarkSweepGC) (-XX: +UseParNewGC is mandatory or disabled)
    • Parallel: Parallel describes the relationship between multiple garbage collector threads
    • Concurrent: Concurrent describes the relationship between garbage collector threads and user threads
  • Parallel Scavenge Collector
    • The new generation collector, based on mark-copy algorithm, multi-threaded collector collected in parallel, achieves a controllable throughput
    • Throughput = time to run user code / (time to run user code + time to run garbage collection)
    • -XX: MaxGCPauseMillis controls the maximum garbage collection pause time
    • -XX: GCTimeRatio directly set the throughput size
  • Serial Old Collector
    • The old generation version of the Serial collector, single-threaded collection, mark-clear algorithm
  • Parallel Old Collector
    • The old version of the Parallel Scavenge collector, multi-threaded concurrent collection, based on mark-sorting
    • In situations where throughput is important or processor resources are scarce, the combination of Parallel Scavenge and Parallel Old collectors can be considered
  • CMS collector
    • Collector with the goal of obtaining the shortest recovery pause time, based on mark-sweep algorithm, concurrent collection, low pause
    • four steps
      • Initial marking: just mark the objects that GC Roots can directly relate to, fast and very sensitive to processor resources
      • Concurrent marking: start from the directly associated objects of GC Roots to traverse the entire object graph process, which can run concurrently with garbage collection threads
      • Re-marking: During the correction of concurrent marking, the mark record of the part of the object whose mark is changed due to the continued operation of the user program
      • Concurrent cleanup: clean up and delete dead objects judged by the mark phase
    • Initial tagging, re-flagging still requires "Stop The World"
    • Number of recycling threads: (number of processor cores + 3) / 4
    • Unable to handle "floating garbage", concurrent marking and concurrent cleaning phases, user threads continue to run to generate new garbage objects that cannot be processed in the current collection
      • Part of the space must be reserved for program operation during concurrent collection, and the old generation will be activated after 68% of the space is used
  • Garbage First collector
    • A full-featured garbage collector, mainly for server-side applications, separates the "behavior" and "implementation" of memory recycling
    • For any part of the heap memory to form a recycling collection for recycling, Mixed GC mode
      • The measurement standard is no longer which generation it belongs to, but which piece of memory stores the most garbage and has the greatest recycling benefits
    • Based on the heap memory layout of Region, G1 divides the Java heap into multiple independent regions (Regions) of equal size, and each Region can act as Eden and Survivor space as needed
    • Use Region as the smallest unit for a single recovery
    • Use remembered sets to avoid full heap scanning as GC Roots
    • four steps
      • Initial marking: just mark the objects that GC Roots can directly relate to
      • Concurrent marking: Reachability analysis of objects in the heap starting from GC Roots
      • final mark: another short pause on the user thread
      • Screening and recovery: responsible for updating the statistical data of the Region
    • G1 can play an advantage in large memory applications

5. Low Latency Garbage Collector

  • Important metrics for measuring garbage collectors: memory usage, throughput, latency

5.1 Shenandoah Collector

  • Implement a method that can control the pause time of garbage collection within ten milliseconds under any heap memory size
  • Not only concurrent garbage marking, but also concurrent cleaning actions after object cleaning
  • Differences from G1
    • Collation Algorithms that Support Concurrency
    • Generational collection is not used by default
    • Instead of memory sets, use the global data structure of the connection matrix to record cross-Region reference relationships
  • work process
    • Initial Mark: Objects directly associated with GC Roots
    • Concurrent marking: traverse the object graph and mark all reachable objects
    • Final markup: processing remaining SATB scans
    • Concurrent cleanup: Regions where no surviving objects are found in the entire region
    • Concurrent recycling: first copy the surviving object to other unused Regions
    • Initialization reference update: the reference to the old object in the heap is fixed to the new address after copying
    • Concurrent reference update: the real start of the reference update operation
    • Final reference update: Fix references that exist in GC Roots
    • Concurrent cleanup: all Regions in the entire collection set have no surviving objects
  • Important phases: concurrent mark, concurrent collection, concurrent reference update
  • Transfer pointer: When the object has a new copy, it only needs to modify the value of one pointer (the position of the forward pointer on the old object so that it points to the new object)

5.2 ZGC collector

  • Based on the Region memory layout, without generation, using read barriers, colored pointers, and memory multiple mapping to implement concurrent mark-sort algorithms
  • ZGC's Region is dynamic – dynamically created and destroyed, and dynamic region capacity size
    • Small Region: The capacity is fixed at 2MB, used to place small objects smaller than 256KB
    • Medium-sized Region: The capacity is fixed at 32MB, and objects larger than 256KB and smaller than 4MB are placed
    • Large Region: The capacity is not fixed and can be changed dynamically. It must be an integer multiple of 2MB, and large objects of 4MB or above are placed
  • Iconic design is the use of dyed pointer technology
  • working process
    • Concurrent marking: Concurrent marking is the stage of traversing the object graph for reachability analysis
    • Concurrent preparatory redistribution: According to specific query conditions, it is calculated which Regions to clean up in this collection process, and these Regions form a redistribution set
    • Concurrent redistribution: copy the surviving objects in the redistribution set to the new Region, and maintain a forwarding table for redistribution to each Region
    • Concurrent remapping: modify all references in the entire heap pointing to old objects in the reallocation set

6. Choose the right garbage collector

  • Epsilon: no-op collector
  • Collector Tradeoffs: Throughput, Latency, Memory Footprint
  • Virtual Machine and Garbage Collector Logs
    • The logs of all functions of HotSpot are returned to the "-Xlog" parameter
  • Parameter summary

insert image description here
insert image description here

7. Memory allocation and recovery strategy

  • Objects are first allocated in Eden
    • Objects are allocated in the Eden area of ​​the new generation. When the Eden area does not have enough space for allocation, the virtual machine will initiate a Minor GC
  • Large objects go directly to the old generation
    • Large objects: Java objects with a large number of contiguous memory spaces (very long strings, arrays with a large number of elements), large objects mean high memory copy overhead
    • HotSpot has: -XX: PretenureSizeThreshold parameter, specifying that objects larger than this setting value are directly allocated in the old generation, avoiding copying back and forth between the Eden area and the two Survivor areas
  • Long-lived objects will enter the old generation
    • The virtual machine defines an age counter for each object, and every time it survives Minor GC, the age increases by 1
    • Raise to the age threshold of the old generation -XX: MaxTenuringThreshold setting
  • Dynamic Object Age Judgment
    • If the sum of the size of all objects of the same age in the Survivor space is greater than half of the Survivor space, the objects whose age is greater than or equal to this age will directly enter the old age
  • Space Allocation Guarantee
    • Before Minor GC, the virtual machine must first check whether the maximum available continuous space in the old generation is greater than the total space of all objects in the new generation
      • If it is not established, it will continue to check whether the maximum available continuous space in the old generation is greater than the average size of objects promoted to the old generation, and then risk starting Minor GC
      • Failure triggers Full GC

Guess you like

Origin blog.csdn.net/baidu_40468340/article/details/128547957