JVM series garbage collector

   There is no regulation on how the garbage collector should be implemented in the Java Virtual Machine Specification. Therefore, the garbage collectors provided by different manufacturers and different versions of virtual machines may be very different, and generally provide parameters for users to customize according to their own needs. The characteristics and requirements of the application combine the collectors used in each era.



The figure shows 7 collectors that act on different generations. If there is a connection between the two collectors, it means that they can be used together. The region where the virtual machine is located indicates whether it belongs to the young generation collector or the old generation collector.

1. Conceptual understanding

   1. Concurrency and parallelism

     
  • Parallel: Refers to multiple garbage collection threads working in parallel, but the user thread is still in a waiting state at this time.
  •   
  • Concurrent (Concurrent): refers to the simultaneous execution of the user thread and the garbage collection thread (but not necessarily in parallel, may be executed alternately), the user program continues to run, and the garbage collection program runs on another CPU.
  •  

  2. Minor GC and Full GC

      
  • Minor GC: Refers to the garbage collection action that occurs in the new generation. Because most Java objects have the characteristics of life and death, Minor GC is very frequent, and the recovery speed is generally faster.
  •   
  • Full GC: Refers to the GC that occurs in the old age, and there is a Major GC, which is often accompanied by at least one Minor GC (but not absolute, in the collection strategy of the Parallel Scavenge collector, there is a strategy selection process for directly conducting Major GC) . Major GC is generally more than 10 times slower than Minor GC.
  •  

  3. Throughput
     Throughput is the ratio of the time the CPU uses to run user code to the total CPU consumption time, that is, throughput = time to run user code / (time to run user code + garbage collection time).
The virtual machine ran for a total of 100 minutes, of which the garbage collection took 1 minute, and the throughput was 99%.

  4. client mode and server mode

      
  • client mode: applied to desktop applications. The -client parameter specifies to start the java program. Pros: Fast startup.
  •   
  • server: applied to server-side applications. The -server parameter specifies to start the java program. Features: start slow, run fast.
  •  

Second, the serial classifier (Serial)



 

  1. Features: This collector is a 单线程single-threaded collector, but its "single-threaded" meaning does not only mean that it will only use one CPU or one collection thread to complete garbage collection work, but more importantly, when it performs garbage collection , must suspend all other worker threads until it finishes collecting.Stop The World
  2. Application scenario: Serial collector is the default new generation collector for virtual machines running in Client mode. It is most suitable for scenarios where the memory usage of the application is less than 100MB (less than 100MB, parallel and concurrent collectors cannot provide much performance) promote).
  3. Advantages: Simple and efficient (compared to the single-threaded ratio of other collectors), for an environment limited to a single CPU, the Serial collector has no thread interaction overhead, so it is natural to concentrate on garbage collection to achieve the highest single-threaded collection efficiency.

 


3. Parallel classifier (Parallel/Throughput )

  Parallel Scavenge collector, ParNew collector, Parallel Old collector



 

  1. 特性:Throughput收集器使用多线程回收新生代空间和老年代空间,GC速度比Serial收集器快得多,Throughput收集器在Minor GC 和 Full GC时会暂停所有应用线程 ,直到它收集结束。Stop The World
  2. 应用场景:Throughput收集器是Server级虚拟机(多CPU的Unix机器以及任何64位虚拟机)的默认收集器,停顿时间越短就越适合需要与用户交互的程序,良好的响应速度能提升用户体验,而高吞吐量则可以高效率地利用CPU时间,尽快完成程序的运算任务,主要适合在后台运算而不需要太多交互的任务。
  3. 优势:使用Throughput收集器处理应用程序线程的批量任务能最大程度的利用CPU的处理能力,通常能获得更好的性能。

 

 

四、CMS收集器

  


 

  1. 特性:Cms收集器是一种以获取最短回收停顿时间为目标的收集器。
  2. 应用场景:目前很大一部分的Java应用集中在互联网站或者B/S系统的服务端上,这类应用尤其重视服务的响应速度,希望系统停顿时间最短,以给用户带来较好的体验。CMS收集器就非常符合这类应用的需求。
  3. 优势:并发收集、低停顿。应用程序只在Minor GC以及后台线程扫描老年代时发生极其短暂的停顿。
  4. 缺点:
  • CMS收集器对CPU资源非常敏感

       In fact, programs designed for concurrency are more sensitive to CPU resources. In the concurrent stage, although it will not cause the user thread to stop, it will cause the application to slow down because it occupies a part of the thread (or CPU resources), and the total throughput will be reduced.
       The number of recycling threads started by CMS by default is (number of CPUs + 3) / 4, that is, when there are more than 4 CPUs, the garbage collection threads will not be less than 25% of the CPU resources during concurrent recycling, and as the number of CPUs increases, decline. But when the CPU is less than 4 (for example, 2), the impact of the CMS on the user program may become very large.

       If the CMS's background garbage collection threads cannot get the CPU resources they need to complete their tasks, or if the heap becomes too fragmented to find contiguous space allocation objects, the CMS reverts to the behavior of the Serial collector: suspends all application threads, Use single-threaded recycling to organize old generation space.

 
  • CMS collector cannot handle floating garbage

      

The CMS collector cannot handle floating garbage, and a "Concurrent Mode Failure" may fail, resulting in another Full GC.

Since the user thread is still running in the concurrent cleaning phase of the CMS, new garbage will naturally be generated as the program runs. This part of the garbage appears after the marking process, and the CMS cannot process them in the current collection, so it has to wait for the next time. Clean up during GC. This part of the garbage is called "floating garbage".
      It is also because the user thread still needs to run during the garbage collection phase, so it is necessary to reserve enough memory space for the user thread to use, so the CMS collector cannot wait until the old age is almost completely filled before proceeding like other collectors. For collection, it is necessary to reserve a part of the space for program operation during concurrent collection. If the memory reserved during CMS operation cannot meet the needs of the program, a "Concurrent Mode Failure" failure will occur. At this time, the virtual machine will start the backup plan: temporarily enable the Serial Old collector to restart the garbage collection of the old generation, so that it stops. It's been a long time.

 

  • CMS collector generates a lot of space fragmentation

     CMS是一款基于“标记—清除”算法实现的收集器,这意味着收集结束时会有大量空间碎片产生。空间碎片过多时,将会给大对象分配带来很大麻烦,往往会出现老年代还有很大空间剩余,但是无法找到足够大的连续空间来分配当前对象,不得不提前触发一次Full GC。

 

五、G1收集器



 

  1. 特性:G1收集器也是一种并发收集器。设计初衷是为了尽量缩短处理超大堆(大于4GB)时产生的停顿。使用G1收集器时,Java堆的内存布局就与其他收集器有很大差别,它将整个Java堆划分为多个大小相等的独立区域(Region),虽然还保留有新生代和老年代的概念,但新生代和老年代不再是物理隔离的了,它们都是一部分Region(不需要连续)的集合。
  2. 应用场景:目前很大一部分的Java应用集中在互联网站或者B/S系统的服务端上,这类应用尤其重视服务的响应速度,希望系统停顿时间最短,以给用户带来较好的体验。CMS收集器就非常符合这类应用的需求。
  3. 优势:G1从整体来看是基于“标记—整理”算法实现的收集器。G1的设计理念使得它比CMS更不容易遭遇Full GC。

六、总结

  Serial收集器:最适用于应用程序的内存使用少于100MB的场景。

  Throughput收集器:如果平均响应时间比最大响应时间更重要(譬如90%的响应时间),采用Throghput收集器通常就能满足。如果目标是要尽可能的缩短响应时间,那么选择使用Concurrent收集器更合适。Concurrent收集器的前提条件是CPU足够强劲

  Concurrent收集器:一般情况下,堆空间小于4GB时,CMS收集器的性能比G1收集器好。使用大型堆或者巨型堆时,由于G1收集器可以分割工作,通常它比CMS收集器表现更好。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327033795&siteId=291194637