JVM garbage collection and memory allocation strategy

Java technology system advocated in the final automatic memory management can be attributed to solve the two problems is automated:

  • To allocate memory objects
  • Reclaim the memory allocated to the object

Memory Architecture Figure heap area (after removal of JDK1.8 permanent generations, GC is only responsible for the new generation and the old district's stack)
Here Insert Picture Description
which, Eden District (Garden of Eden is the meaning, the origin of all objects behind the introduction allocation strategy the more time you will understand why so named), From and To fields are two survivor areas (survivor space).

Related tuning parameters:

  • -XX: SurvivorRatio: Eden and Survivor ratio, Default 8: 1
  • -XX: NewRatio: the old and the new generation's size ratios, such as take 2, it's old space is twice the new generation, as we on as shown in Fig.
  • -Xms / -Xmm: Chief the heap memory size, -Xms set the maximum initial value, -Xmm set dynamically extensible.

First, the garbage collection policy

 
Mainly the following two garbage collection policy:

  • Minor GC : recovery of the new generation , because the new generation of objects survival time is very short, with features raw evening toward the dead, and therefore Minor GC will be performed frequently , the pace of implementation generally will be relatively fast . (Using the copy algorithm )
  • Full GC : Recycling old year and the new generation , long the object of its old's survival, therefore Full GC rarely enforced , execution speed than Minor GC slow a lot. (Using the mark - sweep or mark - Collation Algorithm )

I drew a few pictures, simple presentation about the process Minor GC, where we do a few assumptions in order to facilitate the discussion:

  • New objects are allocated on Eden
  • And the ratio of the size of two Survivor areas are regions Eden 1: 8, but for convenience are plotted in FIG size ratio is not accurate
  • Space allocation guarantee will not happen (this later introduced to the allocation policy when you know)
  • Was promoted to the new generation's old age threshold is 3 years old (this later introduced to the allocation policy when you know)

( Note: The following figure From To two Survivor areas and logos are exchanged back and forth, drawing me to forget, but it really is too lazy to change, strange trouble, Tell me what you hold to apologize ... )

Suppose we now programs are running, this has not occurred prior to the time any node GC, objects encountered a memory to be allocated, but the lack of Eden space.

As follows

Here Insert Picture Description
At this point the trigger Minor GC, the surviving copy objects onto a Survivor, clean up after all the objects Eden area. In addition, for the survival of the object, due to its carry over a Minor GC, so it's age increases a year old (a Minor GC, equivalent to one year the human world), corresponding to the figure numbers from 0 to 1
Here Insert Picture Description
program and while running, Eden region is once again filled
Here Insert Picture Description
at this time will once again triggered Minor GC, it will Eden region and the From Survivor areas live objects copied to another area in a spare Survivor (to in FIG. Survivor), then get rid of all the objects Eden area and From Survivor region.

Follows

after, any place, but the figure above To Survivor region has a 2-year-old object is still alive, we said in the beginning of the assumption, the new generation of 3-year-old was promoted to old age identities, this object will be moved to the old space years, other processes above.

Here Insert Picture Description
Minor GC after
Here Insert Picture Description

Full GC trigger conditions

For Minor GC, which trigger conditions are very simple, when Eden space is full, it will trigger a Minor GC. The Full GC is relatively complex, the following conditions:

1. Call System.gc ()

Just recommend virtual machine executes Full GC, but not necessarily true virtual machine to execute. Not recommended for use in this way, but let the virtual machine memory management.

2. old's lack of space

Old's lack of common space for the large object directly into the scene years old, long-lived objects into the old year and so on.

Full GC in order to avoid the above causes, should try not to create too large objects and arrays. In addition, you can adjust the size of the new generation of large virtual machine by -Xmn parameters, so that the object is recovered as much as possible out in the new generation, do not enter the old era. Can also -XX: MaxTenuringThreshold transfer large objects into the age-old's, so that objects in the new generation of multi survive for some time.

3. Space allocation guarantees failure

使用复制算法的 Minor GC 需要老年代的内存空间作担保,如果担保失败会执行一次 Full GC。

4. JDK 1.7 及以前的永久代空间不足

在 JDK 1.7 及以前,HotSpot 虚拟机中的方法区是用永久代实现的,永久代中存放的为一些 Class 的信息、常量、静态变量等数据。

当系统中要加载的类、反射的类和调用的方法较多时,永久代可能会被占满,在未配置为采用 CMS GC 的情况下也会执行 Full GC。如果经过 Full GC 仍然回收不了,那么虚拟机会抛出 java.lang.OutOfMemoryError。

为避免以上原因引起的 Full GC,可采用的方法为增大永久代空间或转为使用 CMS GC。

这也是为什么 JDK1.8 之后要使用元空间替代永久代的原因(降低 Full GC 的频率)。

5. Concurrent Mode Failure

执行 CMS GC 的过程中同时有对象要放入老年代,而此时老年代空间不足(可能是 GC 过程中浮动垃圾过多导致暂时性的空间不足),便会报 Concurrent Mode Failure 错误,并触发 Full GC。

 

二、内存分配策略

 

1. 对象优先在 Eden 分配

大多数情况下,对象在新生代的 Eden 区上分配(所以取名叫 Eden,伊甸园,所有对象的起源,但在 JVM 内存分配策略中,只能说是大多数对象在此出生),当 Eden 空间不够时,发起 Minor GC。
 

2. 大对象直接进入老年代

大对象是指需要连续内存空间的对象,最典型的大对象是那种很长的字符串以及数组。

经常出现大对象会提前触发垃圾收集以获取足够的连续空间分配给大对象。

相关性能调优参数:-XX:PretenureSizeThreshold,大于此值的对象直接在老年代分配,避免在 Eden 和 Survivor 之间的大量内存复制。

 

3. 长期存活的对象进入老年代

The age defined for the object counters, the object was born in Eden and After Minor GC still alive, will move to the Survivor, the age of 1 year of age increased, to a certain age, then moved to the old era, the default 15 years old.

Related tuning parameters: -XX: MaxTenuringThreshold used to define the threshold age.
 

4. The moving object determination Age

The virtual machine is not always the object's age requirement must be met before promotion MaxTenuringThreshold years old, if the sum of the same age in Survivor all objects larger than half the size of the space Survivor, then aged greater than or equal to the target age can go directly to the old year, no wait until the age MaxTenuringThreshold in requirements.
 

The space allocation guarantees

Minor GC before it occurs, first check whether the old virtual machine's maximum available contiguous space is greater than all the objects in the new generation of total space, if the condition is satisfied, then the Minor GC can confirm that it is safe.

If, then virtual machine is not set up to view the values ​​HandlePromotionFailure of whether to allow a security failure, if allowed it will continue to check whether old's maximum available contiguous space larger than the previous promotion to the average size old's object If so, will try to conduct a Minor GC ; if less than 600 or HandlePromotionFailure is not allowed to take risks, then it would have to conduct a Full GC.

Guess you like

Origin blog.csdn.net/u013568373/article/details/94376999