A JVM
1. The stack frame comprising: a local variable table, the operand stack, constant pool, dynamic link, information for export
2. The method area: a logical part of the pile threads shared storage class information has been loaded in the virtual machine, constants,
Static variables, the real-time compiler to compile the code and so on.
3. How to determine the object is dead
Unreachable objects and objects undergo two marks.
(1) reachability analysis algorithm
The idea of the algorithm is a series of objects called "GC Root" as a starting point, to start the search down from these nodes,
Searching through the road link is called a reference (Reference Chain), when an object to GC Roots no primer
When in contact with the chain (unreachable), then the proof is not available when the object. As shown below, object5, object6,
Although the association object7 meet, but they are to GC Root is not reachable, it will be determined to be recyclable
Object.
In Java, an object can be used as GC Root include the following categories:
- the target virtual machine stack (Local Variable Table stack frame) referenced.
- Object of the JNI native method stacks (i.e., the general said method Native) cited
- object method references a static property class district of objects and constant references.
(2) reachability analysis algorithm
If garbage collection when an object is not found in the GC Root chain, you need to be twice
Marking process, if the current found no association in the GC Root chain, it will mark for the first time, if
At this time, the object finalize () method is not covered or the method has been invoked over the virtual machine, then the time will be marked
Remember that there is no need to perform, at which point the object will be placed in "will recover" collection, otherwise it will put the F-Queue
Awaiting execution object finalize () method, if any object in this process and their upper strand GC Root
Objects association (such as his own (this) assigned to the member variables of a class variable or object), then
Will be removed "will recover" collection.
4. Common garbage collection algorithm
(1) Clear labeling algorithm
It divided into two phases, marked and cleared. Mark phase mark all objects need to be recovered, the cleanup phase back
Closing the space occupied by the marked objects, the biggest problem with this algorithm is severe memory fragmentation.
(2) replication algorithm
In order to address the shortcomings algorithm algorithm memory fragmentation Mark-Sweep is proposed. Press the memory capacity of the memory
Divided into two equal-sized. Every time use only one, when the object after this one still alive memory is full of complex
System to another block up, cleared away the used memory. Although this algorithm is simple, high memory efficiency, not
Easy to produce debris, but the biggest problem is that the available memory is compressed to half of the original. And increased survival target
Then, Copying algorithm efficiency will be greatly reduced .
(3) compression algorithm labeled
Marking and labeling algorithm same stage, after marking not clean up the object, but the object is moved to the survival of memory
End, and then remove the outer boundary of the object.
(4) generational algorithm
Generational collection method is the method most used by the JVM , the core idea is based on the survival of different objects
The life cycle of the memory is divided into different domains, generally divided into the GC heap Older Generation
(Tenured / Old Generation) and the new generation (Young Generation). Older Generation is characterized by a time refuse
Only a few objects need to be recovered when the rubbish recycling, the characteristics of the new generation that has a lot of refuse each time garbage collection
Rubbish to be recovered, it is possible to select different regions according to different algorithms. The use of the new generation of replication algorithm,
Old mark's use of compression algorithms.
(5) partitioning algorithm
The entire memory space is divided into N small independent, each can be used independently of a small space, so that fine-grained control
Made a number of small space and what little space reclamation, rather than the entire space GC, thereby improving
L performance.
The garbage collector
(1) Serial collector
The new generation of collectors, the collector is a single-threaded, using the "replication algorithm." Its "single-threaded"
The significance is not only that it will only use one CPU or a collection thread to complete garbage collector
As, more importantly, when it is garbage collection, the work must suspend all other thread until it closed
Set over.
(2) ParNew collector
Is a multi-threaded version Serial collector, used the "replication algorithm." In addition to using multi-threaded garbage
Addition to collecting, all the remaining actions include control parameters available Serial collector, collection algorithm,
Stop The World, object allocation rules, and so the recovery strategy and Serial collectors exactly the same, in
To implement the two collectors also share a fair amount of code. ParNew collector in addition to multi-threaded collection
Addition, compared with other Serial collector and there is not much innovation, but it is running in Server mode
The virtual machine of choice for the new generation of collectors under the formula, which has nothing to do with performance but a very important reason is that,
In addition to Serial collector, it currently only works with the CMS collector.
(3) Parallel Scavenge collector:
It works in the new generation of the garbage collector, using a replication algorithm, but also multi-threaded exclusive (exclusive collector:
GC to halt execution when the application only GC) in the form of a collector, which is characterized by attention to the throughput of the system.
(4) Serial Old collectors:
Serial is a collector's version of the old, it is also a collector of a single thread, use the "mark - finishing"
algorithm. The main significance of this is that the collector to the virtual machine in the Client mode to use. If the
Under Server mode, it is mainly there are two major purposes:
In one use JDK1.5 and earlier in the collector with the use of Parallel Scavenge.
Another use is as a backup plan CMS collector, occur in concurrent collector
Use Mode Failure Concurrent.
(5) Parallel Old is the Parallel Scavenge collector's version of the old, multi-threading and "mark - finishing" algorithm.
The collector is provided in JDK1.6 began, before that, the new generation of Parallel Scavenge collector
It has been in a state of great embarrassment. The reason is that if the new generation chose Parallel Scavenge collector, old
In addition to Serial Old's collectors have no choice. As the old year Serial Old collectors on the server performance
Drag, using Parallel Scavenge collector may not be able to get the maximum effect on the overall throughput applications.
until.
(6) CMS collector:
Work in the collector of old age, using clear labeling law, in order to obtain a minimum recycling target pause time
Collector . The main concern is the pause time of the system . CMS is not exclusive collector, that CMS back
Process received, the application is still non-stop work, there will be new garbage continue to produce, so the use of CMS
The process should ensure that sufficient memory available to the application, CMS will not wait until saturation in the application to recover
Rubbish, but reaches a certain threshold time went recovered, recovered threshold may be set to specified parameters
-XX: CMSInitiatingOccupancyFraction be specified, the default value is 68, that's when the old usage
68% of the time CMS will perform garbage collection, if memory usage growing rapidly, appeared in the CMS process
Out of memory, CMS will fail recovery, virtual machine will enable years old serial collector garbage collection,
This can cause application outages, until after the completion of garbage collection will work, this process may GC pause time
Too long, so set -XX: CMSInitiatingOccupancyFraction according to the actual situation.
Operation of the process: the initial mark, concurrent mark and re-mark, concurrent cleared. Some versions call (CMS collect additional
Is a more detailed view of the steps: https://blog.csdn.net/zqz_zqz/article/details/70568819 )
The initial mark, re-mark these two steps still need to "Stop The World". The initial mark just mark it
GC Root objects can be directly linked to the ( excluding indirect reach Ha, not directly related to the object tag concurrency
Phase mark Kazakhstan) , very fast; the concurrent marking phase is in progress GC Roots Tracing of ; relabel
During the correction is in order to mark concurrent user program to continue because that part of the object which led to marked changes from label
Record , this time to pause time generally longer than the initial mark phase, but, far short concurrent mark. at this
Stage to suspend all user threads, re-scan objects in the heap, reachability analysis, mark the object alive, especially
Do not need to note that, at this stage is whether the new generation of the object to determine the object is the root of survival; concurrent cleared
The main stage is to remove those objects that are not marked and reclaim space , because the CMS concurrent cleanup phase subscriber line
Cheng is still running, with the program running naturally there will continue to generate new garbage, garbage appears in this part of mark
After the building process, CMS can not dispose of them when times were collected, and then had to clean out the time left to the next GC. This
Part of garbage called "floating garbage."
Disadvantages:
(A) CMS collector is very sensitive to CPU resources (in fact it should be more consumption of cpu resources). And in
Hair stage, although it does not lead to the user thread pause, but because of the occupation of part of the CPU resources
Cause the application to slow down. The number of recovery threads CMS is enabled by default (the number of CPU +3) / 4,
That is when the CPU is above 4, concurrent garbage collection thread less than 25% of CPU
Resources, and with the increase in the number of CPU decline. But when an insufficient number of CPU's (Pi
As 2), the impact on the CMS user program may become very large. To cope with this situation,
CMS virtual machine provides a collection of variant called "incremental concurrent collector" (i-CMS), and
In concurrent mark on, clean up time for GC threads, user threads run alternately, to minimize line
Time exclusive resources processes. In the current version of the i-CMS it has been declared as "deprecated", that is,
Not promote users.
(B) CMS processor can not handle floating garbage.
(C) based on the "mark - sweep" collector algorithm implemented, will produce a large amount of space debris, there will be concurrent
Case failure mode, the concurrent mode failure will perform memory consolidation (memory compression), this situation
Under conditions will lead CMS slower than the Parallel Scavenge (CMS than usual
Parallel Scavenge faster).
(7) G1 collector:
JDK1.7 is proposed based on the "mark - finishing" garbage collector algorithm, its mission is to replace CMS
Dwell time model collector, can establish a predictable part of generational garbage collector to distinguish between the old and the new generation
Years, there are still eden and from, to zone, it does not require the new generation, years old, eden, from,
District room to empty all in a row, use the partitioning algorithm. It is said that after JDK1.7 is used G1 collection algorithm,
But to be verified, it can not be determined at present, but in
JDK1.7 where it is not mature.
Collector G1 entire Java heap is divided into separate regions (Region) of equal size, although
保留着新生代和老年代的概念,但新生代和老年代不再是物理隔离的了,它
们都是一部分Region(不需要连续)的集合。
G1收集器之所以能建立可预测的停顿时间模型,是因为它可以有计划
地避免在整个Java堆中进行全区域的垃圾收集。G1跟踪各个堆里面的垃圾
堆积的价值大小(回收所获得的空间大小以及回收所需要时间的经验值),
在后台维护一个优先列表,每次根据允许的回收时间,优先回收价值最大
的Region。
特点:
并行和并发:G1充分利用多CPU、多核环境下的硬件优势,使用多个CPU
来缩短Stop-The-World停顿的时间,部分其它收集器原本需要
停顿Java线程执行的GC动作,G1收集器仍然可以通过并发的
方式让Java程序继续执行。
分代收集:存在Eden、from、to。不需要其它收集器的配合就能独立管理
整个GC堆,可以自己处理堆中的对象。
空间整合:重整体来看是基于“标记-整理”算法实现的;局部(两个Region
之间)来看是基于“复制”算法的实现的,但这两种算法都不会
产生内存碎片。
可预测停顿:能建立可预测的停顿时间模型。能让使用者明确指定一个长
度为M毫秒的时间片段内,消耗在垃圾收集上的时间不得超
过N毫秒。(这点是G1相对于CMS的另外一大优势,低停
顿是G1和CMS共同的关注点,但G1除了追求低停顿外,还
能建立可预测的停顿时间模型)
执行过程:如果不计算维护Remembered Set的操作,G1收集器的运作
大致可划分为以下几个步骤:
初始标记、并发标记、并行标记、最终标记、筛选回收。
初始标记仅仅是标记一下GC Root能直接关联到的对象,并
且修改TAMS(Next Top at Mark Start)的值,让下一阶段
用户程序并发运行时,能在正确可用的Region中创建对象,
这阶段需要停顿但时耗时短;并发标记是从GC Root开始对
堆中对象进行可达性分析,找出存活的对象,这阶段耗时较长,
但可与用户程序并发执行;最终标记是为了修正在并发标记期
间因用户程序继续运作而导致标记产生变动的那一部分标记记
录;筛选回收阶段首先要对各个Region的回收价值和成本进行
排序,根据用户所期望的GC停顿时间来制定回收计划,根据
Sun公司透露的信息来看,这个阶段其实可以做到与用户程序
一起并发执行。
6. 减少GC开销的编程技巧
http://www.importnew.com/10472.html
(1)不要显式调用System.gc()
此函数建议JVM进行主GC,虽然只是建议而非一定,但很多情况下它
会触发主GC,从而增加主GC的频率,也即增加了间歇性停顿的次数。
(2)尽量减少临时对象的使用
临时对象在跳出函数调用后,会成为垃圾,少用临时变量就相当于
减少了垃圾的产生,从而延长了出现上述第二个触发条件出现的时
间,减少了主GC的机会。
(3)尽量使用StringBuffer,而不用String来累加字符串
由于String是固定长的字符串对象,累加String对象时,并非在一个
String对象中扩增,而是重新创建新的String对象,如
Str5=Str1+Str2+Str3+Str4,这条语句执行过
程中会产生多个垃圾对象,因为对次作“+”操作时都必须创建新的
String对象,但这
些过渡对象对系统来说是没有实际意义的,只会增加更多的垃圾。
(4)计划好List的容量
像ArrayList这样的动态集合用来存储一些长度可变化数据的基本结
构。ArrayList和一些其他的集合(如HashMap、TreeMap),底层
都是通过使用Object[]数组来实现的。而String(它们自己包装在
char[]数组中),char数组的大小是不变的。那么问题就出现了,
如果它们的大小是不变的,我们怎么能放item记录到集合中去呢?
答案显而易见:分配更多的数组。所以,无论什么时候,尽可能
的给List或者Map分配一个初始容量,就像这样。
(5)能用基本类型如Int,long,就不用Integer,Long对象
基本类型变量占用的内存资源比相应对象占用的少得多,如果没有
必要,最好使用基本变量。
(6)尽量少用静态对象变量
静态变量属于全局变量,不会被GC回收,它们会一直占用内存。
(7)对象不用时最好显式置为Null
一般而言,为Null的对象都会被作为垃圾处理,所以将不用的对象显
式地设为Null,有利于GC收集器判定垃圾,从而提高了GC的效
率。
(8)分散对象创建或删除的时间(个人觉得这个似乎不太现实)
集中在短时间内大量创建新对象,特别是大对象,会导致突然需要大
量内存,JVM在面临这种情况时,只能进行主GC,以回收内存或
整合内存碎片,从而增加主GC的频率。集中删除对象,道理也是一
样的。它使得突然出现了大量的垃圾对象,空闲空间必然减少,从
而大大增加了下一次创建新对象时强制主GC的机会。