Java memory allocation and garbage collection

A: Java virtual machine data area consisting of

Java virtual machine data area has five major components, namely: the method area, heap area, stack area virtual machine, local area method, program counter area, which is divided into two groups of five parts, namely:

(A) threads sharing part:

1. method area: This area is shared by the various threads, used to store the virtual machine is loaded classes, constants, static variables, the instant compiled code and other information with respect to this area [area] heap in terms of much more stable, but still the existence probability memory overflow, this area will involve memory recall.

2. heap: heap area is the largest part of the virtual machines in memory footprint for a variety of non-transitory copies of objects created to store run by individual threads, because it involves the use of multiple threads at the same time (read and write into), therefore, this area is where garbage collection mechanism to play a major role, the place is also the most prone to memory leaks, this area is the core area of ​​memory management, memory management features are most healthy run around the region It designs.

(B) an exclusive thread portions:

3. virtual machine stack: This area is actually a single-threaded operating area, the equivalent of threading the console private workers.

4. A native method stacks: stack virtual machine and the like, but the use of the Java virtual machine to a local (non-virtual machine of the present method of generating or generated by other languages) method of providing service establishments, providing similar parts to vendors, and to receive other collaboration with manufacturers of parts and so on supporting vendors place.

5. Program Counter: This area is used to record a single thread during operation of the line number, jump control and the like.

Second, the object memory allocation:

When the virtual opportunities to the new instruction, the virtual method in the constant pool area to find the reference marks of this class, if memory is assignable to initialize variables; if not found, it will be checked for this class, the class produced after initialization file compiled about the class each of the symbols, numerals stored in the constant pool, then the class initialization.

The method of determining whether the object exists:

1. The reference counter:

Counter method reciting principles: adding a reference counter object itself, whenever there is a place where it is referenced, counter + 1, when referring to failure counter -1, at any time, the object counter is no longer used instructions 0, It is recycled into the process. Features of this method is simple algorithm, high efficiency, but there is a fatal flaw: references between objects can not solve mutual problems.

2. reachability analysis:

The basic idea of ​​this algorithm is that, through a series called "GC Roots" objects as a starting point, search down the object, the object can reach is called survival object, the object regarded as unreachable recyclable objects. Objects recovered into the recovery process. Can be regarded as "GC Roots" objects are:

(1) Method Static variable region, constant

(2) native method stack referenced object

(3) the object is referenced in the virtual machine stack

References between objects according to the strength of relationship, divided into four categories:

1, strong applications, such as this: Object A = new Object (); which reference object will not be recovered except by reachability analysis object has been unreachable.

2, soft references, soft reference object will be recovered when the virtual machine is not enough memory.

3, weak references, a lower probability of survival weak reference object, be reclaimed when garbage collection system in a virtual machine.

4, virtual reference, by definition very ethereal, unreal, generally to achieve a particular functionality in a virtual machine, little to do with the daily development.

Memory object alive or not need to go through two stages. The first stage is to determine reachability analysis after this object has been unreachable, this time the subject is given a suspended sentence stage, only conducted the first mark, it can not be garbage collected. Then JVM judge, according to the judgment is: whether the object covering the finalize () method or whether it is performed once finalize () method, if need be considered to perform finalize () method, the virtual opportunity to build a queue internally, in this either the queue is marked again, and destroyed, or will be re-quoted, escape the fate of death, return to a viable state.

Third, the garbage collection algorithm classification:

1. Mark sweep algorithm. This algorithm component marks, remove two stages. First mark the objects need to be recovered, all unified object is marked recovery after labeling is complete. Algorithms There are two major flaws, one of the two processes efficiency is not high, because it involves scanning the heap, this process is very long. Another is to remove the mark, it will generate a lot of discontinuous fragments, resulting in the subsequent need to allocate large capacity objects, unable to find enough contiguous memory is forced to make another garbage collection. This algorithm is the basis for the subsequent algorithm, subsequent algorithm basically revolves around the lack of improvement of the algorithm designed launch.

2.复制算法。为了解决标记清除算法的碎片问题而推出的复制算法,可以说简单、粗暴、有效(跟西哲一脉相承)。该算法将可用内存等分成两部分,每次只使用其中一部分,当进行垃圾回收时,虚拟机将这部分中还存活的对象复制到另一区域中,将本区域清空待分配。这个算法特点是垃圾回收处理有些粗糙,不够细腻,每次回收都涉及到不等的内存复制动作。本算法应用在新生代垃圾回收。

 3.标记-整理算法。标记整理算法和标记清理算法类似,不同之处在于在清理阶段,清理完垃圾后,按照开销估算小的原则,将剩余的存活对象向一端移动,整理出大片连续的空闲内存,供以后内存分配使用。

4.分代收集算法。这个算法没有什么新的思想,可以说是一个综合性的算法。该算法将内存分成新生代&老年代。在新生代区域,每次垃圾回收时,若剩余少量的存活对象,则使用标记-复制算法。若存活对象较多则进行标记清除处理或者标记整理处理。在老年代区域,则由于存活对象一般较多,则采用标记-整理算法进行回收。

jvm回收采用分带收集算法。

四、HotSpot算法实现

1.什么是安全点?首先,目前的主流Java虚拟机基本上都是准确式GC,虚拟机通过OopMap这种数据结构来得知内存中,哪些地方存放着对象引用,在类加载完成后,Hotspot就能很精确地知道内存的什么位置存放着什么类型,多大内存占用的对象。这样一来,在GC回收时就可以直接判定哪些对象需要回收与否,而不需要再进行扫描,提高了GC效率。实际上,HotSpot也不会为每一条运行指令都生成OopMap,以加快程序的运行,而只是想程序运行到特定的位置来记录、生成OopMap信息,这些位置点即是安全点。简言之,安全点就是程序中由HotSpot指定的某些特定的位置,在这些位置,HotSpot生成OopMap数据结构,同时进行GC操作,在这些点,Java线程是短暂停止运行的。

2.安全区。安全区是在一段代码中,各种引用关系不发生变化的代码区域。在这个区域,各种引用关系保持稳定,不再发生变化,这样,当线程运行到安全区时首先会通过标识,向虚拟机声明自己已经进入安全区,这时,即使线程已经被挂起或者其他状态,那么虚拟机扔可以进行GC操作,而不必让线程继续运行到安全点再执行。安全区是安全点的扩展,相当于战争中的停火区间。而安全点相当于战争中的每一个休息时间点,虽然战事在进行,然而在这些安全点,大家都忙着吃饭、休息、打扫卫生,没人会继续。

几种垃圾收集器

Serial收集器。这是一种历史悠久的单线程收集器,主要负责新生代的垃圾收集,特点是简单、高效,如果虚拟机运行在Client模式下,用Serial收集器是个不错的选择。

ParNew收集器。这个收集器相当于是Serial收集器的多线程版本,也是用在新生代垃圾收集。

Parallel Scavenge收集器,这个收集器是新生代的另一种收集器,特点是可以可以达到一个可控制的吞吐量。吞吐量=运行客户代码时间 / (运行客户代码时间 + 垃圾收集时间)。

CMS收集器。CMS收集器是一种获取最短回收时间的垃圾收集器。由于目标明确,适用面广,因此很大一部分被用在Web服务器上。CMS收集器采用标记-清除算法实现,收集过程流程复杂。

G1收集器。最新的垃圾回收研究成果。用在新生代和老年代垃圾收集,是目前比较新的收集器。

五、内存分配和回收策略

1.新生代一般分两种区域结构。其一是Eden,new Object的内存分配一般会被对应在此,另一种是Servivor区域,此区域用来存储经过垃圾回收而幸存下来的对象。具体而言,新生代有三个区域划分,即:一个Eden区、两个Servivor区。在复制算法下,三个区域的大小分配一般为:8:1:1.当回收发生时,虚拟机将Eden和Servivor区域中还存活的对象一次性复制到另外一块Servivor区域内,最后清理掉Eden和和刚刚用过的Servivor区域。

2.一般不大的对象,直接分配在新生代的Eden区域上,如果区域空间不够,也会有少量对象分配在老年代区域中。这个区域内的对象寿命都很短,一般被采用标记-清除算法回收。

备注:由于各种资料缺少,此处描述模糊。自己推测为:一般对象会被分配到Eden区域,而其中一个Servivor用来存储上次幸存的对象,而另一个Servivor区域为空,等待下次垃圾收集时,存储更幸运的对象,而当内存分配时,如果Eden区域容量不够,根据内存担保原则,将对象分配在老年代(对象还属于新生代,只是借用老年代空间)。

3.大的对象直接分配在老年代。这个区域内对象相对于新生代而言,较稳定,寿命更长。这个区域一般采用标记-整理算法实现垃圾收集。

4.新生代对象和老年代对象关系。一般对象会分配在新生代,新生代进行垃圾收集时,如果对象未被收集,并且能被Servivor容纳,那么此对象会被转移到Servivor区域,自身的年龄设置为1,以此来推,每当对象熬过一次垃圾收集,自身对象就增加1,当对象增加到一定岁数(默认为15岁),对象就会被转移到老年代中。除此之外,还有一种情况,当Servivor区域中相同年龄的所有对象的大小总和 > Sevivor空间一半时,年龄>=此年龄的所有对象就可以直接进入老年代,而无需等到设置的岁数再被转移。

 

参考资料:深入理解Java虚拟机(第二版)

Guess you like

Origin www.cnblogs.com/jizhong/p/12323378.html