Java Virtual Machine knowledge -GC

First, the garbage collection algorithm

  1. Mark - sweep algorithm
      first mark all need to be recovered objects, then all objects are marked uniform recycling. The algorithm efficiency is not high , and there is memory fragmentation problems.

  2. Replication algorithm
      memory capacity divided by two of equal size, which uses only one memory allocation, when this memory runs out, it will still all live objects copied to another block of memory, and the used memory space once cleared away. The algorithm can solve the problem of efficiency mark clear algorithm. However, because of the need to divide half the memory, a higher price.

  3. Mark - sorting algorithm
      marks all objects need to be recovered, so live objects move toward one end, then clean out the memory directly outside the terminal boundary. The algorithm can solve the problem of memory fragmentation mark sweep algorithm, as well as the object replication algorithm at high survival rate, multiple replication efficiency becomes low.

  4. Generational collection algorithm for
      the new generation, each has a large number of dead objects garbage collection, only a few survive, then you have to use the copy algorithm, so long as the pay copying costs a small amount of live objects to complete the collection;
      the old era, the object high survival rate, no extra space is allocated to guarantee him, you have to use markers or tags to organize clean-up algorithm;
      generational collection algorithm will heap space is divided into the old and the young generation yang years old, the young generation has been divided into zones and Eden Survivor area, Survivor region has been divided into zones with From to area. Default by 8: 1 division of Eden area and Survivor areas. Eden region is contiguous memory space, and therefore fast in Eden allocated memory area. HotSpot virtual machine using the pointer to speed collision and TLAB memory allocation Eden area and guarantee thread safety.
    Generational collection algorithm memory division

Execution flow generational collection algorithm

  1. New object in Eden priority allocation region;
  2. When the Eden area is full, it will trigger Minor GC, live objects are moved to Eden Survivor0, Eden is cleared;
  3. Eden area is full again and so on, trigger again live objects Minor GC, Eden and Survivor0 in will be copied to the Survivor1, S0 and Eden are cleared, then the next round of S0 and S1 switch roles, so on ad infinitum.
  4. After two Survivor areas switched several times (HotSpot virtual machine default 15 times), still alive objects are copied to the old era.
Minor GC trigger conditions:
  • Fires when the Eden area is full.
Full GC trigger conditions:
  • When calling System.gc, the system will recommend the implementation of Full GC, but not necessarily implemented.
  • Trigger years old when insufficient space.
  • The method area (on behalf of the permanent / dollar space) is triggered when the lack of space.
  • The average size of contiguous memory triggered by Minor GC after entering old age old age is greater than available.
  • Region of Eden, From time to copy the To Space Space zone area, the object Space available memory size is larger than the To, the object put dump year old, old age and available continuous memory size less than the trigger time of the object.

Second, memory allocation and recovery strategies

  • Objects priority in the allocation area in Eden
      when Eden area is not enough memory space is allocated, the virtual machine will launch
    a Minor GC.

  • Large objects directly into the old year
      a large number of contiguous memory Java objects, such as a long string and array will be assigned directly to the old era, and therefore should be avoided when writing programs.

  • Long-term survival of the object will enter the old year
      when the two Survivor areas switched several times (HotSpot virtual machine default 15 times), still alive objects are copied to the old era.

  • Age dynamic object is determined
      if the sum of all of the same age S0 space is larger than half the size of the object space S0, equal to or older than the age of the subject can directly enter years old, without reaching the threshold.

  • Space allocation guarantees
      before it happens Minor GC, virtual opportunity to check whether old's maximum available contiguous space is greater than all the objects in the new generation of total space, if this condition is met, then the Minor GC to ensure that it is safe. If not satisfied, the virtual opportunity to review whether to allow HandlePromotionFailure settings guarantee failure. If allowed, it will continue to check whether old's maximum available contiguous space larger than previous years old was promoted to the average size of the object, if greater than, will attempt to conduct a Minor GC, despite the Minor GC is risky; if less than, or HandlePromotionFailure settings do not allow risk, then it should be changed once a Full GC.

Third, the garbage collector

  1. Serial : single-threaded collector. == == replication algorithm
  2. ParNew : multithreaded version Serial collector. == == replication algorithm
  3. Scavenge Parallel : Similar ParNew collector, other collectors focus on the Stop The World shorten the time as much as possible, but more concerned about the Parallel collector system throughput , support adaptive strategies. == == replication algorithm
  4. Old Serial : Serial old collector's edition. == == mark Collation Algorithm
  5. Old Parallel : old's version of the Parallel Scavenge collector. == == mark Collation Algorithm
  6. The CMS : Concurrent Mark Sweep Collector is a kind of to get the shortest recovery time objectives pause collector. == == mark sweep algorithm

 The operation of the CMS process

  • The initial mark (Initial Mark) : marked objects inside old's survival, these objects are from GC roots or point directly, or survival of the object being pointed to the young generation. It will lead to STW, the fastest.

The initial mark

  • Concurrent mark (Concurrent Mark) : find all root from the stage start to traverse the entire old era, marking live objects. Slow, however, and the program is executed concurrently.

Concurrent mark

  • Relabeled (Remark, the Final) : As the previous concurrent mark, concurrent process, may not be able to catch up with the speed of an application to modify. So it is necessary to re-mark to mark the completion of the entire Older Generation survival object marking. It will lead to STW, fast.
  • Clear concurrent (Concurrent Sweep) : Concurrent clear target of death. Slow, however, and the program is executed concurrently.

Concurrent Clear

 CMS whole process

CMS collector

 The disadvantage of CMS

  • Of CPU resources are very sensitive. The default number of threads is recovered CMS (CPU number + 3) / 4.
  • Can not handle floating garbage may appear "Concurrent Mode Failure" caused by the failure to produce another of Full GC. CMS concurrent user thread is still running clean-up stage, with the program running naturally continue to produce new garbage, garbage appear after this part of the marking process, CMS can not handle at times when the collection out of them, have to be left until the next GC and then clean up. This part of the garbage that is floating garbage. Because garbage collection phase of user threads have to run, so unlike other CMS collector's almost like the old filled in the collection, it will reserve a portion of the space . -XX: CMSInitiatingOccupancyFraction can set the percentage trigger. When the reserved memory the program can not meet the needs, there will be "Concurrent Mode Failure" failure appears, at this time, start the virtual machine backup plan: to temporarily enable Serial Old.
  • Mark - remove defects algorithm. Prone to memory fragmentation . Solution: The configuration parameters for CMS to open in Full GC merge sort of process memory fragmentation, memory consolidation process can not be concurrent, STW will lead to longer time, so there is another parameter configuration, how many times does not set the execution for compression after the Full GC, will perform compression Full GC.

7. G1 : service-oriented end applications.

 G1 features

  • Concurrent and Parallel: take full advantage of multi-CPU, multi-core hardware advantages environment, shorten STW.
  • Generational collection: Reserved generational concept, can independently manage the entire GC heap.
  • Space finishing: based on the "mark - finishing," local (two Region) point of view based on the "Copy" algorithm. So no memory space debris.
  • Predictable pause: This is a great advantage with respect to G1 CMS, allowing the user to explicitly specify a length of time segments M milliseconds, consumed in the garbage collection may not exceed N milliseconds.

 G1 of the operation process

  • The initial mark (Initial Mark): similar CMS
  • Concurrent mark (Concurrent Mark): similar CMS
  • The final mark (Remark): similar CMS
  • Filter Recovery (cleanp): At this stage the objects are copied from one or more regions into a single region, and the adjustment and free memory.

8. The a ZGC : introduction of the JDK. 11, claims to have lower latency garbage collector, using a colored pointer, barrier loading technology, the STW a control, the scanning can be realized only once garbage collection.

Fourth, the combination of garbage collector

parameter Features
-XX:+UseConcMarkSweepGC Automatically enabled -XX: + UseParNewGC
-XX:+UseParallelGC Automatically enabled -XX: + UseParallelOldGC. The default in Server mode.
-XX:+UseParallelOldGC Automatically enabled -XX: + UseParallelGC
-XX:+UseParNewGC JDK8 alone can not be enabled
-XX:+UseSerialGC Serial + Serial Old. The default in Client mode.
-XX:+UseG1GC Use G1 Garbage Collector

Guess you like

Origin www.linuxidc.com/Linux/2019-07/159649.htm