This stock is in the end is not spam? GC JVM garbage collection algorithm [chapter four]

Garbage collection Garbage Collection is often called "GC", the jvm, the program counter, the virtual machine stack, native method stacks are born with the threads with the threads off the stack frame with the method of entry and exit and do push pop operations, to achieve the automatic cleanup of memory, therefore, our memory garbage collection focuses on java heap and method area , during program execution, this memory allocation and use are dynamic in!

@

Since it is a garbage collection, it will certainly have to know what garbage is, and I believe one day come down java development Yichun little brother to picking up litter to maintain life, breathed a juo, bend, opening the lid, pour one go I close my eyes , did not finish the Guangdong handsome happy home water fertilizer wrong when picking up garbage, which hit Guangdong also not handsome? Yes, the premise of the JVM garbage collection is clear what is rubbish! JVM will conduct a series of algorithms to determine the count is not spam. In the JVM garbage consider whether the criteria for the object is alive, live objects is not garbage, recycling is not feasible, whereas for recycling.

1, survival of the subject JVM determines whether two counting algorithm

JVM two counting algorithm to determine whether an object has survived mainly two, namely: the reference count, count reachability analysis algorithm.

On hearing counting algorithm in the "algorithm" word, white shoes daze: OMG, bad math, finished, not learn ... Ahem cough, not to mention how they algorithm, a quick daily lives from started, we usually something not often used, but it would not open any place, then this thing can be said is rubbish. Is actually the case in Java, if an object is no longer referenced, the object is garbage and should be recycled. It is that simple and crude, JVM two counting algorithm is like this, it really is not complicated.

1.1, reference counting algorithm

According to our idea of ​​life, we can easily think of using reference counting method to determine the trash. When an object is added to the reference and decremented when a reference is removed, so that we can whether the count is 0 determines whether an object is determined by reference as spam. In this way we generally call "reference counting." A thief really simple idea is precisely that it's simple, so flawed, so the reference counting algorithm is defined as

1, reference count: each object has a reference count attribute count is incremented when a new reference count by 1 when released, the count is 0 can be recovered by reference.
2, because of a simple method, so that a reference count algorithms can not resolve the problem the object each circular references .

White shoes may not know what is the object of a circular reference, bear dei Figure
Here Insert Picture Description

1.2, reachability analysis counting algorithm

First of all make it clear: in the actual development languages ​​such as java, C #, etc. are based on whether the reachability analysis counting algorithm to determine the object alive!

Reaching definition analysis algorithm is as follows:

Reachability analysis (Reachability Analysis): From the GC Rootsstart searching down, called search path traversed reference chain. When an object is not connected to any reference GC Roots chain, then it proves that this subject is not available. Unreachable object.

I believe it is easy to see that, in fact, are well understood, with the exception of GC Roots somewhat abstract, it is what it is GC Roots? In the Java language, GC Roots include:

1, the reference to the object, is in a virtual machine stack.
2, static reference method area.
3, reference native method stacks in the JNI. [JNI generally refers to a Native Method]

Simply put, GC Root is carefully selected some of the references

Here Insert Picture Description
Like this loop above cited reference counting algorithm can not be carried out in the GC, but the root of the search algorithm can be done, and therefore reference flawed counting algorithm! Root search algorithm better!

2, garbage collection algorithm

以上内容概述了啥垃圾,接下来就要讲讲如何进行回收垃圾!在生活中我们收集垃圾,可以一边走一边扫,也可以把垃圾全都扫到一起再一次清理。在JVM中,类似生活,提供了一系列的垃圾收集算法。

而且我们还经常能看到垃圾分类,分成可回收垃圾,和不可回收垃圾,但是从某一意义上来讲,还都是垃圾,如果人人都遵守垃圾分类规则,我估计以后收破烂的大叔大妈都天天从可回收垃圾桶下手了,其实在JVM中也类似如此,JVM采取了分区、分代收集的思想。

常见的垃圾回算法:标记清除算法、复制算法、标记整理算法、分代收集算法。

2.1、标记清除算法复制算法

标记清除算法。从名字可以看到其分为两个阶段:标记阶段和清除阶段。一种可行的实现方式是,在标记阶段,标记所有由 GC Root 触发的可达对象。此时,所有未被标记的对象就是垃圾对象。之后在清除阶段,清除所有未被标记的对象。标记清除算法最大的问题就是空间碎片问题。如果空间碎片过多,则会导致内存空间的不连续。虽说大对象也可以分配在不连续的空间中,但是效率要低于连续的内存空间。
Here Insert Picture Description

2.2、复制算法(Copying)

复制(Copying)算法。复制算法的核心思想是将原有的内存空间划分为大小相等的两块,每次只使用一块,在垃圾回收时,将正在使用的内存中的存活对象复制到未使用的内存块中。之后清除正在使用的内存块中的所有对象,之后交换两个内存块的角色,完成垃圾回收。复制算法的缺点是要将内存空间折半,极大地浪费了一半内存空间。因此现在的商业虚拟机都使用这种复制算法来进行新生代的垃圾收集!因为在对象存活率高的时候,复制算法就显得效率低下
Here Insert Picture Description
Here Insert Picture Description

2.3、标记-整理算法(Mark-Compact)

关于标记-整理算法还有一种叫法是标记压缩算法,知道其说的同一者就好了。

标记整理算法。标记整理算法可以说是标记清除算法的优化版,其同样需要经历两个阶段,分别是:标记阶段、整理阶段。在标记阶段,从 GC Root 引用集合触发去标记所有对象,和标记清除算法中的标记阶段是一样一样的。在整理阶段,其则是将所有存活的对象整理在内存的一边,之后清理边界外的所有空间。因此,标记-整理算法(Mark-Compact)不会产生内存碎片,但是会多花点时间用在整理(Compact)上面!
Here Insert Picture Description

2.4、分代收集算法(Generational Collection)

“分代收集”(Generational Collection)算法,把Java堆分为新生代和老年代,这样就可以根据各个年代的特点采用最适当的收集算法。在新生代中,每次垃圾收集时都发现有大批对象死去,只有少量存活,那新生代选用复制算法,只需要付出少量存活对象的复制成本就可以完成收集。而老年代中因为对象存活率高、没有额外空间对它进行分配担保,老年代就使用“标记-清理”或“标记-整理”算法来进行回收。
Here Insert Picture Description

2.5、四种回收算法总结

标记清除算法

分为标记阶段和清除两阶段。标记清除算法最大的问题就是空间碎片问题。比较适合在存活对象比较多的情况。

复制算法(Copying)

复制算法的缺点是浪费了一半内存空间。现在的商业虚拟机都使用这种复制算法来进行新生代的垃圾收集!因为在对象存活率高的时候,复制算法就显得效率低下比较适合存活对象比较少的情况。

Tags to organize algorithm (Mark-Compact)

Clear labeling tags to organize algorithm is optimized version of the algorithm, mark - Collation Algorithm (Mark-Compact) does not produce memory fragmentation, but will spend more time with finishing (Compact) above!

Generational collection algorithm (Generational Collection)

The new generation of replication algorithm selection , the old's use of "mark - clean-up" or "mark - finishing" algorithm to recover.

3, garbage collection algorithm Summary

Borrowing a picture Gangster finishing:
Here Insert Picture Description

If this article there is a little bit of help to you, then please point a chant praise, thank you ~

Finally, if there is insufficient or is not correct, please correct me criticism, grateful! If you have questions please leave a message, the absolute first time to reply!

I welcome you to focus on the public number, there are some java learning materials and a large wave of java e-books, such as Zhou Zhiming teacher depth java virtual machine, java programming ideas, the core technology volume, Westward design patterns, java concurrent programming combat ... .. is a java Bible, do not say fast car on Tomcat, ye who go! The main thing is to explore technology, yearning technology, the pursuit of technology, said good pots Friends is coming Oh ...

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/yichunguo/p/12012841.html