The garbage collector Study Notes

GC algorithms (counts / copy / Clear mark / finishing marker) is recovered methodology memory, the garbage collector is implemented floor specific algorithm. Garbage collection occurs in the heap and method area.

It is worth noting that so far not a perfect garbage collectors, no more universal collector, the collector can only be the most appropriate for a particular application, a generational collection.

Benpian note-taking four types of garbage collector.

1, Serial: serial garbage collector

It is a single-threaded environment design and use only one thread garbage collection, users will suspend all processes, it is not suitable for server environments.

Imagine a scenario: you're in a restaurant with friends for dinner, a waiter came over to tell you: You first pause Well, I rub under the table, you eat a good rub. Serial such a garbage collection on a similar concept. You think about it, two-eleven, you're under the single payment, the result of a server to a bit serial GC, also allows people to live without?

Serial collector is the oldest and most stable of the garbage collector, using only one thread to recovery. But in the garbage collection process may produce a longer pause time (Stop-The-World status). Although the need to suspend the work of all other threads in the process of garbage collection, but it is simple and efficient, to define a single CPU environment, without the overhead of thread interaction can obtain the highest efficiency of garbage collection, so Serial garbage collector is still java virtual the new generation of machines running the default garbage collector in Client mode.

as the picture shows:

The JVM is the corresponding parameters: -XX: + UseSerialGC , will open after use: Serial (young region used) + Serial Old (Old region using) a collector assembly.

It represents the serial garbage collector will use the new generation of years old, which use the new generation of replication algorithm, use tags to organize years old algorithm.

About its characteristics, long-winded one: it is a single-threaded garbage collector, when garbage collection, the other threads must suspend its work until the end of the collection.

2, Parallel: parallel garbage collector

Multiple threads in parallel garbage collection, then the user thread is suspended for scientific computing, big data processing and other interactive weak scene.

The top and almost turned out to be a waiter to clean the table, this is a group of attendants to wipe the table.

Note the parallel GC is happening in the new generation, as the old year is not a parallel manner, depending on which kind of parallel garbage collector specific use.

3, CMS: concurrent garbage collector

CMS: Current Mark Sweep, concurrent mark cleared, a pause for the shortest recovery time objectives collector. User threads and simultaneously execute garbage collection thread (not necessarily in parallel, may be alternately performed), the user does not need to pause the process, and more Internet companies use it for heap response time required by the scene.

This scenario is similar to a waiter say to you: you first come to the table to eat, I rub up the table you come back.

This collector for old time, multiple threads concurrent garbage, sweep algorithm using the markers.

The whole process of garbage into the following four steps:

1), the initial mark (CMS initial mark): just only marks GC Root objects can be directly associated, very fast, but need to "Stop The World"  

2), concurrent mark (CMS concurrent mark): GC Root Tracing of the process, is simply iterate live objects Initial Marking stage marked, and then recursively mark these objects reachable objects.

3) re-mark (CMS Remark): concurrent mark during the correction, because the user program continues to run and result in marked record that part of the object tag fluctuates, it is necessary "Stop The World". This time is generally longer than the initial mark, but shorter than the time of concurrent mark.

4), concurrent cleared (CMS concurrent sweep): cleanup operations on objects on the step marked.

Since the whole process is the most time-consuming operation is the second (concurrent mark), four steps (concurrent cleared), which is two steps garbage collector threads and user threads can work together. So overall, CMS garbage collection and user threads are concurrently executed together.

CMS Disadvantages:

1), sensitive to CPU resources

Because in the concurrent stages, will occupy part of the CPU resources, causing the application to slow down, overall throughput will be reduced.

2), to produce the floating garbage

Because CMS concurrent user threads are still working to clean up stage, this time the garbage generated, CMS can not be processed in this collection out of them, leaving only the next time GC and then processed it off, this part of the garbage called "floating junk" .

3) to produce memory chips garbage

因为采用的算法是标记-清除,很明显,会有空间碎片产生。

4、G1垃圾回收器

CMS垃圾收集器虽然减少了暂停应用程序的运行时间,但是它还存在着内存碎片的问题。于是,为了去除内存碎片,同时保留CMS垃圾收集器暂停时间短的优点,java7发布了一款新的垃圾收集器-G1.

G1收集器技术发展的最前沿的成果,可以实现在基本不牺牲吞吐量的前提下完成低停顿的内存回收。首发于JDK8中,是JDK9默认的垃圾回收器。

主要的改变是Eden、Survivor和Tenured等内存区域不再是连续的了,而是变成了一个个大小一样的Region。每个Region大小从1MB到32MB不等。一个Region可能属于Eden、Survivor或者Tenured内存区域。

官方对它的描述是,G1是一种服务器端应用的垃圾收集器,应用于多处理器和大容量内存环境中,在实现高吞吐量的同时,尽可能满足垃圾收集暂停时间的要求。此外,它还具有以下的特性:

  • 像CMS收集器一样,能与应用程序线程并发执行;
  • 整空闲空间更加快速;
  • 不希望牺牲大量的 吞吐性能;
  • 不需要更大的Java Heap

G1收集器的设计目标是取代CMS收集器,它与CMS相比,在以下方面的表现更加出色:

  • G1是一个有整理内存过程的垃圾收集器,不会产生很多的内存碎片;
  • G1的stop the world更加可控,G1在停顿时间上添加了预测机制,用户可以指定停顿时间。

我们先来看一下其它的垃圾收集器都各有什么特点:

  • 年轻代和老年代是各自独立且连续的内存块;
  • 年轻代收集使用单Eden+S0+S1复制算法;
  • 老年代收集必须扫描整个老年代区域;
  • 都是以尽可能少而快速的执行GC为设计原则。

它并不像前面介绍的所有垃圾收集器是区分新生代,老年代的,它作用于全区域。将整个Java堆划分为多个大小固定的独立区域(Regin),并且跟踪这些区域的垃圾堆积面积,在后台维护一个优先级列表,每次根据允许的收集时间,优先回收垃圾最多的区域,这样保证了G1收集器在有限的时间内可以获得最高的收集效率。



Guess you like

Origin www.cnblogs.com/simon-1024/p/12225793.html