Garbage collection (1) Overview

1, JVM runtime memory distribution

Garbage collection (1) Overview

Garbage collection is aimed Heap (heap), because all objects are entities exist in this area of ​​operation.

2, garbage collection algorithm

2.1, reference notation

1, it is practice to add a reference count for each object, to count the number of reference points to the object, the referenced object when the number is 0, can be recovered.
2, reference counting and a major loophole that is unable to deal with circular references an object, say A reference to B, B references A, but A and B are no other references, then this time will cause the object can not be recovered back.

2.2, reachability analysis

The essence of the algorithm is a series of GC Roots as an initial collection of live objects (live set), then from the collection, discover all the objects that can be referenced to the collection, and added to the collection, a process that we also referred mark (mark). Ultimately, the object is not to explore death, it can be recycled.

So, what is the GC roots of it? For example
1, Java stack frame method of local variables;
2, loaded class static variable;
3, started but not stop Java threads.

3、Stop-the-world

In order to avoid the process of garbage collection, because there are application threads running, leading to changes in the state of memory, caused by error recovery. In the Java virtual machine, using a simple and crude way, that is the Stop-the-world, non-stop work of other garbage garbage collection thread until the completion of garbage collection. This also resulted in the so-called pause time garbage collection (GC pause).

Stop-the-world Java virtual machine is achieved by the security point (safepoint) mechanism. When the Java Virtual Machine Stop-the-world received the request, it will wait for all the threads have reached a safe point before allowing the request Stop-the-world exclusive thread work.

4, garbage collection in three ways

4.1, direct removal

The memory occupied by dead objects marked as free memory

Garbage collection (1) Overview
1 time, cause memory fragmentation, memory is not continuous, the object memory reallocation of inefficiency.
2, in extreme cases, can cause enough total memory, but objects can not allocate memory.

4.2, compression

Clear Object death, the surviving objects gathered to the starting position memory area
Garbage collection (1) Overview
1, there is the performance overhead of compression algorithms
2, moving objects, related to change memory addresses, we need to update the reference address.

4.3 Copy

The memory area is divided into two halves, each with two pointers from and to be maintained, and only a pointer from the memory area to allocate memory, when garbage collection occurs, to copy live objects to put
pointer memory area, and to exchange the contents and the pointer from the pointer.

Garbage collection (1) Overview

1, can solve the problem of memory fragmentation
2, using extremely low efficiency of heap space

5, JVM heap division

Java Virtual Machine heap is divided into the old and the new generation's. Wherein, the new generation area are divided into Eden, and two Survivor areas the same size.
Garbage collection (1) Overview

Generally speaking, when we call the new directive, it will draw in the Eden area of memory as a storage object. Since the heap space is shared by the threads, thus directly where space is designated side needs to be synchronized.
Otherwise, it will be possible for some accident two shared memory objects appear.
Solution Java virtual machine is TLAB technology (Thread Local Allocation Buffer, the corresponding virtual machine parameters -XX: + UseTLAB, enabled by default).

1、每个线程可以向Java虚拟机申请一段连续的内存,比如2048字节,作为线程私有的TLAB。
2、这个操作需要加锁,线程需要维护两个指针,一个指向TLAB中空余内存的起始位置,一个则指向TLAB末尾。
3、new指令,便可以直接通过指针加法(bump the pointer)来实现,即把指向空余内存位置的指针加上所请求的字节数。如果加法后空余内存指针的值仍小于或等于指向末尾的指针,则代表分配成功。否则,TLAB已经没有足够的空间来满足本次新建操作。这个时候,便需要当前线程重新申请新的TLAB。

6、新生代的GC,也成Minor GC或者Young GC

1、当Eden区的空间耗尽,这个时候Java虚拟机便会触发一次Minor GC,来收集新生代的垃圾。存活下来的对象,则会被送到Survivor区。
2、新生代共有两个Survivor区,我们分别用from和to来指代。其中to指向的Survivior区是空的。当发生Minor GC时,Eden区和from指向的Survivor区中的存活对象会被复制到to指向的Survivor区中,然后交换from和to指针,以保证下一次Minor GC时,to指向的Survivor区还是空的。
3、Java虚拟机会记录Survivor区中的对象一共被来回复制了几次。如果一个对象被复制的次数为15(对应虚拟机参数-XX:+MaxTenuringThreshold),那么该对象将被晋升(promote)至老年代。
4、如果单个Survivor区已经被占用了50%(对应虚拟机参数-XX:TargetSurvivorRatio),那么较高复制次数的对象也会被晋升至老年代。
5、发生Minor GC时,应用了标记-复制算法,将Survivor区中的老存活对象晋升到老年代,然后将剩下的存活对象和Eden区的存活对象复制到另一个Survivor区中。理想情况下,Eden区中的对象基本都死亡了,那么需要复制的数据将非常少,因此采用这种标记-复制算法的效果极好。
6、Minor GC的另外一个好处是不用对整个堆进行垃圾回收。

但是,它却有一个问题,那就是老年代的对象可能引用新生代的对象。也就是说,在标记新生代存活对象的时候,我们需要扫描老年代中的对象。如果该老年代对象拥有对新生代对象的引用,那么这个老年代对象也会被作为GC Roots。
这样一来,又做了一次全堆扫描呢?

7、卡表(Card Table)

HotSpot给出的解决方案是一项叫做卡表(Card Table)的技术。
1、该技术将整个堆划分为一个个大小为512字节的卡,并且维护一个卡表,用来存储每张卡的一个标记。这个标识位代表对应的卡是否可能存有指向新生代对象的引用。如果可能存在,那么我们就认为这张卡是脏卡。
2、Minor GC的时候,可以不用扫描整个老年代,而是在卡表中寻找脏卡,并将脏卡中的对象加入到Minor GC的GC Roots里。当完成所有脏卡的扫描之后,Java虚拟机便会将所有脏卡的标识位清零。
3、Minor GC伴随着存活对象的复制,而复制需要更新指向该对象的引用。因此,在更新引用的同时,又会设置引用所在的卡的标识位。这个时候,我们可以确保脏卡中必定包含指向新生代对象的引用。
4、在Minor GC之前,我们并不能确保脏卡中包含指向新生代对象的引用。

8、垃圾回收器

新生代的垃圾回收器共有三个:
1、Serial,Parallel Scavenge和Parallel New。
2、这三个采用的都是标记-复制算法。
3、其中,Serial是一个单线程的,Parallel New可以看成Serial的多线程版本。Parallel Scavenge和Parallel New类似,但更加注重吞吐率。此外,Parallel Scavenge不能与CMS一起使用。

The garbage collector also has three years old:
1, Old Serial and Parallel Old, and CMS.
2, Serial Old and Parallel Old are marked - compression algorithm. Similarly, the former is single-threaded, while the latter can be seen as a multi-threaded version of the former.
3, CMS uses a mark - sweep algorithm, and are concurrent. In addition to the few operations required Stop-the-world, it can be garbage collected while the application is running.

Guess you like

Origin blog.51cto.com/janephp/2427547