GC garbage collection

GC three issues to be addressed

1. What memory need to recycle?

2. When the recovery?

3. How to recycle?

Determining whether the object is garbage

Reference counting algorithm

Each object instance has a reference counter, whenever a reference to its place, the counter is incremented; when referring to failure, the counter is decremented by 1; reference by determining the number of objects to determine whether the object can be recovered, when the number of references to 0, the object is recoverable objects

Advantages: simple, high efficiency

Disadvantages: there will be problems with circular references, resulting in a memory leak

Reachability analysis algorithm

In order to resolve the reference cycle counting method reference problem, Java method using reachability analysis. Through a series of objects called "GC Roots" as a starting point, if there is no reachable route between the "GC roots" and an object, the object is said to be unreachable, claimed that the object is not reachable.

Reachability analysis method is unreachable objects and marks the first time to conduct a screening filter condition is whether this object is necessary to perform the finalize method. When the object is no finalize method, or finalize method has been covered call obsolete virtual machines, virtual machines, these two cases deemed unnecessary to perform.

It is determined to be the object needs to be performed will be placed in a queue for the second mark, unless the object is associated with any reference to an object on the chain, otherwise it will be really recovered.

Summary: unreachable object is not equivalent to the recyclable objects, unreachable object becomes recycled objects at least twice to go through the labeling process. After two marks are still recyclable objects, it will face recovery.

It can be called GC Roots of objects:

  • VM stack of reference objects

  • Method zone object literal references

  • District method Static variables referenced objects

  • Reference to the object in the JNI native method stacks

  • Active thread object reference

Garbage collection algorithm

Clear labeling algorithm (Mark-Sweep)

The most basic garbage collection algorithm is divided into two phases, marked and cleared. Mark phase mark all objects need to be recovered, the recovery phase is marked clear space occupied by an object. Figure
Here Insert Picture Description
we can see from the figure, the algorithm biggest problem is memory fragmentation severe , may occur subsequent large object can not find the problem available space.

Replication algorithm (copying)

In order to address the shortcomings algorithm algorithm memory fragmentation Mark-Sweep is proposed. Press memory partitioning the memory into two equal size. Every time use only one copy to another block when the object of this one is full up after the memory is still alive, the memory has been cleared for use, as shown:
Here Insert Picture Description
This algorithm, although simple, high memory efficiency, difficult to produce debris, but the biggest problem is that the available memory is compressed to half of the original. And the number of live objects, then the efficiency Copying algorithms will be greatly reduced.

Tags to organize algorithm (Mark-Compact)

A combination of the above two algorithms proposed in order to avoid defects. Mark phase and Mark-Sweep same algorithm, the tag is not clean up the object, but towards the end of the live objects in memory. Then clear the outer end of the object boundary. Figure:
Here Insert Picture Description

Generational collection algorithm

分代收集法是目前大部分 JVM 所采用的方法,其核心思想是根据对象存活的不同生命周期将内存划分为不同的域,一般情况下将 GC 堆划分为老生代 (Tenured/Old Generation) 和新生代 (Young Generation) 。老生代的特点是每次垃圾回收时只有少量对象需要被回收,新生代的特点是每次垃圾回收时都有大量垃圾需要被回收,因此可以根据不同区域选择不同的算法。

新生代与复制算法

目前大部分 JVM 的 GC 对于新生代都采取 Copying 算法,因为新生代中每次垃圾回收都要回收大部分对象,即要复制的操作比较少,但通常并不是按照 1:1 来划分新生代。一般将新生代划分为一块较大的 Eden 空间和两个较小的 Survivor 空间(From Space, To Space),每次使用 Eden 空间和其中的一块 Survivor 空间,当进行回收时,将该两块空间中还存活的对象复制到另一块 Survivor 空间中。
Here Insert Picture Description

老年代与标记整理算法

而老年代因为每次只回收少量对象,因而采用 Mark-Compact 算法。

  1. JAVA 虚拟机提到过的处于方法区的永生代 (Permanet Generation),它用来存储 class 类,常量,方法描述等。对永生代的回收主要包括废弃常量和无用的类。

  2. 对象的内存分配主要在新生代的 Eden Space 和 Survivor Space 的 From Space(Survivor 目前存放对象的那一块),少数情况会直接分配到老生代。

  3. 当新生代的 Eden Space 和 From Space 空间不足时就会发生一次 GC,进行 GC 后,Eden Space 和 From Space 区的存活对象会被挪到 To Space,然后将 Eden Space 和 From Space 进行清理。

  4. 如果 To Space 无法足够存储某个对象,则将这个对象存储到老生代。

  5. 在进行 GC 后,使用的便是 Eden Space 和 To Space 了,如此反复循环。

  6. 当对象在 Survivor 区躲过一次 GC 后,其年龄就会+1。默认情况下年龄到达 15 的对象会被移到老生代中。

JAVA 四中引用类型

强引用

在 Java 中最常见的就是强引用,把一个对象赋给一个引用变量,这个引用变量就是一个强引用。当一个对象被强引用变量引用时,它处于可达状态,它是不可能被垃圾回收机制回收的,即使该对象以后永远都不会被用到 JVM 也不会回收。因此强引用是造成 Java 内存泄漏的主要原因之一。

软引用

软引用需要用 SoftReference 类来实现,对于只有软引用的对象来说,当系统内存足够时它不会被回收,当系统内存空间不足时它会被回收。软引用通常用在对内存敏感的程序中,作为缓存来使用。

public static void main(String[] args){
	
	Integer[] a = {5, 6, 8};
    SoftReference<Integer[]> softReference = new SoftReference<>(a);
    a = null;

    System.out.println(a); // out:null
    System.out.println(Arrays.toString(softReference.get())); // out:[5, 6, 8]
}

弱引用

弱引用需要用 WeakReference 类来实现,它比软引用的生存期更短,对于只有弱引用的对象来说,只要垃圾回收机制一运行,不管 JVM 的内存空间是否足够,总会回收该对象占用的内存。

因此,弱引用与软引用的区别在于:只具有弱引用的对象拥有更短暂的生命周期。软引用关联的对象只有在内存不足时才会被回收,而被弱引用关联的对象在JVM进行垃圾回收时总会被回收。

应用场景:如果一个对象是偶尔的使用,并且希望在使用时随时就能获取到,但又不想影响此对象的垃圾收集,那么应该用 Weak Reference 来记住此对象。

public static void main(String[] args){
	WeakReference<String> sr = new WeakReference<>(new String("hello"));
	System.out.println(sr.get());
	System.gc();
	System.out.println(sr.get());
}

虚引用

虚引用和前面的软引用、弱引用不同,它并不影响对象的生命周期。

虚引用需要 PhantomReference 类来实现,它不能单独使用,必须和引用队列联合使用。虚引用的主要作用是跟踪对象被垃圾回收的状态。当垃圾回收器准备回收一个对象时,如果发现它还有虚引用,就会把这个虚引用加入到与之 关联的引用队列中。程序可以通过判断引用队列中是否已经加入了虚引用,来了解被引用的对象是否将要被垃圾回收。如果程序发现某个虚引用已经被加入到引用队列,那么就可以在所引用的对象的内存被回收之前采取必要的行动。

public static void main(String[] args){
	ReferenceQueue<String> queue = new ReferenceQueue<>();
	PhantomReference<String> pr = new PhantomReference<>(new String("hello"), queue);
	System.gc();
	System.out.println(queue.poll());
}

GC 分代收集算法 VS 分区收集算法

当前主流 VM 垃圾收集都采用 ”分代收集” (Generational Collection) 算法,这种算法会根据对象存活周期的不同将内存划分为几块,如 JVM 中的 新生代、老年代、永久代,这样就可以根据各年代特点分别采用最适当的 GC 算法

  • 在新生代 —— 复制算法
    每次垃圾收集都能发现大批对象已死,只有少量存活。因此选用复制算法,只需要付出少量存活对象的复制成本就可以完成收集。

  • 在老年代 —— 标记整理算法
    因为对象存活率高、没有额外空间对它进行分配担保,就必须采用 “标记整理” 或 “标记清除” 算法来进行回收,不必进行内存复制,且直接腾出空闲内存。

分区收集算法

分区算法则将整个堆空间划分为连续的不同小区间, 每个小区间独立使用,独立回收,这样做的好处是可以控制一次回收多少个小区间,根据目标停顿时间,每次合理地回收若干个小区间 (而不是整个堆),从而减少一次 GC 所产生的停顿。

什么时候会发生 Full GC

  1. 老年代空间不足

  2. 永久代空间不足(JDK 1.8之前)

  3. CMS GC 时出现 promotion failed,concurrent mode failure

  4. Minor GC 晋升到老年代的平均大小大于老年代的剩余空间

  5. 调用 System.gc()

  6. 使用 RMI 来进行 RPC 或管理的 JDK 应用,每小时执行一次 Full GC

垃圾收集器

Java 堆内存被划分为新生代和年老代两部分,新生代主要使用复制算法;年老代主要使用标记整理算法,因此 JVM 针对新生代和年老代分别提供了多种不同的垃圾收集器,JDK1.6 中 Sun HotSpot 虚拟机的垃圾收集器如下:
Here Insert Picture Description

年轻代垃圾收集器

Serial 垃圾收集器(单线程、复制算法)

Serial,使用复制算法,曾经是 JDK1.3.1 之前新生代唯一的垃圾收集器。

Serial 是一个单线程的收集器,它不但只会使用一个 CPU 或一条线程去完成垃圾收集工作,并且在进行垃圾收集的同时,必须暂停其他所有的工作线程,直到垃圾收集结束。

Serial 垃圾收集器虽然在收集垃圾过程中需要暂停所有其他的工作线程,但是它简单高效,对于限定单个 CPU 环境来说,没有线程交互的开销,可以获得最高的单线程垃圾收集效率,因此 Serial 垃圾收集器依然是 java 虚拟机运行在 Client 模式下默认的新生代垃圾收集器。

ParNew 垃圾收集器(Serial+多线程)

ParNew 垃圾收集器其实是 Serial 收集器的多线程版本,也使用复制算法,除了使用多线程进行垃圾收集之外,其余的行为和 Serial 收集器完全一样,ParNew 垃圾收集器在垃圾收集过程中同样也要暂停所有其他的工作线程。

ParNew 收集器默认开启和 CPU 数目相同的线程数,可以通过 -XX:ParallelGCThreads 参数来限制垃圾收集器的线程数。

ParNew虽然是除了多线程外和 Serial 收集器几乎完全一样,但是 ParNew 垃圾收集器是很多 java 虚拟机运行在 Server 模式下新生代的默认垃圾收集器。

Parallel Scavenge 收集器(多线程复制算法、高效)

Parallel Scavenge 收集器也是一个新生代垃圾收集器,同样使用复制算法,也是一个多线程的垃圾收集器,它重点关注的是程序达到一个可控制的吞吐量(Thoughput,CPU 用于运行用户代码的时间/CPU 总消耗时间,即吞吐量=运行用户代码时间/(运行用户代码时间+垃圾收集时间)),高吞吐量可以最高效率地利用 CPU 时间,尽快地完成程序的运算任务,主要适用于在后台运算而不需要太多交互的任务。自适应调节策略也是 ParallelScavenge 收集器与 ParNew 收集器的一个重要区别。

老年代垃圾收集器

Serial Old 收集器(单线程标记整理算法 )

Serial Old 是 Serial 垃圾收集器年老代版本,它同样是个单线程的收集器,使用标记整理算法,这个收集器也主要是运行在 Client 默认的 java 虚拟机默认的年老代垃圾收集器。

在 Server 模式下,主要有两个用途:

  1. 在 JDK1.5 之前版本中与新生代的 Parallel Scavenge 收集器搭配使用。

  2. 作为年老代中使用 CMS 收集器的后备垃圾收集方案。

新生代 Serial 与年老代 Serial Old 搭配垃圾收集过程图:
Here Insert Picture Description
新生代 Parallel Scavenge/ParNew 与年老代 Serial Old 搭配垃圾收集过程图:
Here Insert Picture Description

Parallel Old 收集器(多线程标记整理算法)

Parallel Old 收集器是 Parallel Scavenge 的年老代版本,使用多线程的标记整理算法,在 JDK1.6 才开始提供。

在 JDK1.6 之前,新生代使用 ParallelScavenge 收集器只能搭配年老代的 Serial Old 收集器,只能保证新生代的吞吐量优先,无法保证整体的吞吐量,Parallel Old 正是为了在年老代同样提供吞吐量优先的垃圾收集器,如果系统对吞吐量要求比较高,可以优先考虑新生代 Parallel Scavenge 和年老代 Parallel Old 收集器的搭配策略。

新生代 Parallel Scavenge 和年老代 Parallel Old 收集器搭配运行过程图:
Here Insert Picture Description

CMS 收集器(多线程标记清除算法)

Concurrent mark sweep(CMS) 收集器是一种年老代垃圾收集器,其最主要目标是获取最短垃圾回收停顿时间,和其他年老代使用标记整理算法不同,它使用多线程的标记清除算法。

最短的垃圾收集停顿时间可以为交互比较高的程序提高用户体验。

CMS 工作机制相比其他的垃圾收集器来说更复杂,整个过程分为以下 4 个阶段:

  • 初始标记
    只是标记一下 GC Roots 能直接关联的对象,速度很快,仍然需要暂停所有的工作线程

  • 重新标记
    为了修正在并发标记期间,因用户程序继续运行而导致标记产生变动的那一部分对象的标记记录,仍然需要暂停所有的工作线程

  • 并发标记
    进行 GC Roots 跟踪的过程,和用户线程一起工作,不需要暂停工作线程

  • Concurrent Clear
    Clear GC Roots unreachable objects, and user threads work together, without pausing worker threads. As the longest concurrent mark and concurrent process of clearing, garbage collection thread and a user can now work together concurrently, so the overall recovery of memory and user threads CMS collector is together concurrently.

CMS collector working process:
Here Insert Picture Description

G1 collector (copy + mark Collation Algorithm)

Garbage first garbage collector garbage collector is at the forefront of theoretical development outcomes, compared with the CMS collector, G1 collector two most prominent improvements are:

  1. Tag-based sorting algorithm, no memory fragmentation.

  2. * Can be very precise control of the dwell time, under the premise without sacrificing throughput, low garbage collection pause.

G1 collector avoid a region-wide garbage collection, it is the heap memory is divided into several separate areas of a fixed size, and track the progress of garbage collection in these areas, while maintaining a priority list in the background every time allowed to collect in accordance with most regional priorities garbage collection. Zoning and priority areas recovery mechanism to ensure that G1 collector can obtain the highest efficiency of garbage collection for a limited time.

JVM tuning parameters

-Xss: specifies the size of the virtual machine each thread stack (stack)

-Xms: the initial value of the stack

-Xmx: maximum heap attainable

-XX: + PretenuerSizeThreshold: a new generation of large objects stored in the old years

-XX: SurvivorRatio: Eden and Survivor ratio, Default 8: 1

The proportion of the old year and the new generation: -XX: NewRatio

-XX: MaxTenuringThreshold: Object promoted from the new generation to the number of times through the GC's maximum threshold of old

Published 42 original articles · won praise 11 · views 3814

Guess you like

Origin blog.csdn.net/weixin_44584387/article/details/104642214