JVM设置Young Gen大小

JVM设置Young Gen的大小疑问
设置年轻代的3组参数有优先级区别
设置Young Generation的大小(假设设为1024m),似乎有很多参数,大概分为3组:
(1)-XX:NewSize=1024m和-XX:MaxNewSize=1024m;
(2)-Xmn1024m;
(3)-XX:NewRatio=2; (假设Heap总共是3G)

很奇怪,为什么hotspot的JVM设置Young Gen会冒出这么多组参数呢?难道是历史原因么?如果这样的话,我们3组混合着用,谁覆盖谁呢???

笔者找了些资料,也做了些实验,得出一下结论:
(1)最高优先级:  -XX:NewSize=1024m和-XX:MaxNewSize=1024m
(2)次高优先级:  -Xmn1024m  (默认等效效果是:-XX:NewSize==-XX:MaxNewSize==1024m)
(3)最低优先级:-XX:NewRatio=2

推荐使用的是-Xmn参数,原因是这个参数很简洁,相当于一次性设定NewSize和MaxNewSIze,而且两者相等。-Xmn配合-Xms堆起始大小和-Xmx堆最大大小,恰好把堆内存布局确定完了(估计设计者也是因为简洁的原因,弄出了三个简写参数)。另外,官文似乎说-Xmn是1.4才开始支持的,但是如今应该没有还在用1.4之前的JRE的吧。

官文佐证
摘要1: 1.4之后,-Xmn以一抵二(-XX:NewSize和-XX:MaxNewSize)
http://www.oracle.com/technetwork/java/hotspotfaq-138619.html#gc_pause
you can adjust the size of the eden (young generation space) with -XX:NewSize=... and -XX:MaxNewSize=... (for 1.3/1.4) or -Xmn in 1.4 and later.

在1.3/1.4版本下,通过-XX:NewSize和-XX:MaxNewSize两个参数共同设置Young Gen的大小(原文笼统地把Young Gen当做Eden了)。
然后在1.4之后的版本,可以直接通过-Xmn来设置Young Gen的大小了。

笔者感觉:1.4后,设置Young Gen更方便了。

摘要2:1.4之后,还可以用-XX:NewRatio,-XX:MaxNewSize覆盖-XX:NewRatio
If you currently invoke with something like:
-Xms384m -Xmx384m -XX:NewSize=128m -XX:MaxNewSize=128m
which will dedicate 1/3rd of the memory to eden.

For 1.3, MaxNewSize is set to 32mb on Sparc, 2.5mb on Intel based machines. NewRatio (the ratio between the young/old generations) has values of 2 on Sparc Server, 12 on client Intel, and 8 everywhere else, as you can quickly determine, this is superseded by MaxNewSize's defaults (rendering NewRatio ineffective for even moderately sized heaps). I

In 1.4 and later, MaxNewSize has been effectively set to infinity, and NewRatio can be used instead to set the value of the new generation. Using the above as an example, you can do the following in 1.4 and later:

  -Xms384m -Xmx384m -XX:NewRatio=2


笔者感觉:设置Young Gen有多个参数,大概跟历史版本原因有关系。不用去了解这些,需要了解的是这些参数一起用的时候,怎么办???做实验瞅瞅!!!
实验佐证
实验代码
public class JVMNewSize {

public static void main(String[] args) {
while(true) {
System.out.println("running ...");
try {
Thread.sleep(1000L*30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}

实验1:-Xmn以一抵二
[@zw_83_83 test]# java -Xms900m -Xmx900m -Xmn600m JVMNewSize
running ...

固定堆内存为900m,-Xmn指定Young Gen为600m。

[@zw_83_83 ~]# jps -m
5705 Jps -m
5681 JVMNewSize
[@zw_83_83 ~]# jmap -heap 5681
Attaching to process ID 5681, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 20.1-b02

using thread-local object allocation.
Parallel GC with 13 thread(s)

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 943718400 (900.0MB)  最大堆内存是900m
   NewSize          = 629145600 (600.0MB)
   MaxNewSize       = 629145600 (600.0MB)  Young 的确是600m
   OldSize          = 5439488 (5.1875MB)
   NewRatio         = 2
   SurvivorRatio    = 8
   PermSize         = 21757952 (20.75MB)
   MaxPermSize      = 85983232 (82.0MB)

Heap Usage:
PS Young Generation 年轻代:Eden+From+To 总共恰好是600m
Eden Space:
   capacity = 471859200 (450.0MB)
   used     = 9437200 (9.000015258789062MB)
   free     = 462422000 (440.99998474121094MB)
   2.000003390842014% used
From Space:
   capacity = 78643200 (75.0MB)
   used     = 0 (0.0MB)
   free     = 78643200 (75.0MB)
   0.0% used
To Space:
   capacity = 78643200 (75.0MB)
   used     = 0 (0.0MB)
   free     = 78643200 (75.0MB)
   0.0% used
PS Old Generation   年轻代占用了600m,自然老年代就300m
   capacity = 314572800 (300.0MB)
   used     = 0 (0.0MB)
   free     = 314572800 (300.0MB)
   0.0% used
PS Perm Generation
   capacity = 21757952 (20.75MB)
   used     = 2606792 (2.4860305786132812MB)
   free     = 19151160 (18.26396942138672MB)
   11.980870258377259% used


如果调整为-Xmn400呢?
[@zw_83_83 test]# java -Xms900m -Xmx900m -Xmn400m JVMNewSize

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 943718400 (900.0MB)
   NewSize          = 419430400 (400.0MB)
   MaxNewSize       = 419430400 (400.0MB)
   OldSize          = 5439488 (5.1875MB)
   NewRatio         = 2
   SurvivorRatio    = 8
   PermSize         = 21757952 (20.75MB)
   MaxPermSize      = 85983232 (82.0MB)


实验2:-Xmn被-XX:NewSize和-XX:MaxNewSize覆盖
1、 只覆盖NewSize:尽管NewSize的值被覆盖了,但是实际空间似乎没被覆盖
[@zw_83_83 test]# java -Xms900m -Xmx900m -Xmn400m -XX:NewSize=200m JVMNewSize
running ...

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 943718400 (900.0MB)
   NewSize          = 209715200 (200.0MB)  NewSize被覆盖为200m
   MaxNewSize       = 419430400 (400.0MB)
   OldSize          = 5439488 (5.1875MB)
   NewRatio         = 2
   SurvivorRatio    = 8
   PermSize         = 21757952 (20.75MB)
   MaxPermSize      = 85983232 (82.0MB)

Heap Usage:
PS Young Generation  年轻代加起来是400m,不是200m (实际分配空间没覆盖)
Eden Space:
   capacity = 314572800 (300.0MB)
   used     = 6291472 (6.0000152587890625MB)
   free     = 308281328 (293.99998474121094MB)
   2.000005086263021% used
From Space:
   capacity = 52428800 (50.0MB)
   used     = 0 (0.0MB)
   free     = 52428800 (50.0MB)
   0.0% used
To Space:
   capacity = 52428800 (50.0MB)
   used     = 0 (0.0MB)
   free     = 52428800 (50.0MB)
   0.0% used
PS Old Generation
   capacity = 524288000 (500.0MB)
   used     = 0 (0.0MB)
   free     = 524288000 (500.0MB)
   0.0% used
PS Perm Generation
   capacity = 21757952 (20.75MB)
   used     = 2606792 (2.4860305786132812MB)
   free     = 19151160 (18.26396942138672MB)
   11.980870258377259% used

2、 只覆盖MaxNewSize
[@zw_83_83 test]# java -Xms900m -Xmx900m -Xmn300m -XX:MaxNewSize=400m JVMNewSize
running ...

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 943718400 (900.0MB)
   NewSize          = 314572800 (300.0MB)
   MaxNewSize       = 419430400 (400.0MB)  数值被覆盖为400m了
   OldSize          = 5439488 (5.1875MB)
   NewRatio         = 2
   SurvivorRatio    = 8
   PermSize         = 21757952 (20.75MB)
   MaxPermSize      = 85983232 (82.0MB)

Heap Usage:
PS Young Generation  实际年轻代累加也是覆盖的400,感觉是取Max值。
Eden Space:
   capacity = 314572800 (300.0MB)
   used     = 6291472 (6.0000152587890625MB)
   free     = 308281328 (293.99998474121094MB)
   2.000005086263021% used
From Space:
   capacity = 52428800 (50.0MB)
   used     = 0 (0.0MB)
   free     = 52428800 (50.0MB)
   0.0% used
To Space:
   capacity = 52428800 (50.0MB)
   used     = 0 (0.0MB)
   free     = 52428800 (50.0MB)
   0.0% used
PS Old Generation
   capacity = 524288000 (500.0MB)
   used     = 0 (0.0MB)
   free     = 524288000 (500.0MB)
   0.0% used
PS Perm Generation
   capacity = 21757952 (20.75MB)
   used     = 2606792 (2.4860305786132812MB)
   free     = 19151160 (18.26396942138672MB)
   11.980870258377259% used

3、 两个都覆盖

[@zw_83_83 test]# java -Xms900m -Xmx900m -Xmn100m -XX:NewSize=200m  -XX:MaxNewSize=400m JVMNewSize

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 943718400 (900.0MB)
   NewSize          = 209715200 (200.0MB)
   MaxNewSize       = 419430400 (400.0MB)
   OldSize          = 5439488 (5.1875MB)
   NewRatio         = 2
   SurvivorRatio    = 8
   PermSize         = 21757952 (20.75MB)
   MaxPermSize      = 85983232 (82.0MB)

Heap Usage:
PS Young Generation  按NewSize和MaxNewSize的最大值分配
Eden Space:
   capacity = 314572800 (300.0MB)
   used     = 6291472 (6.0000152587890625MB)
   free     = 308281328 (293.99998474121094MB)
   2.000005086263021% used
From Space:
   capacity = 52428800 (50.0MB)
   used     = 0 (0.0MB)
   free     = 52428800 (50.0MB)
   0.0% used
To Space:
   capacity = 52428800 (50.0MB)
   used     = 0 (0.0MB)
   free     = 52428800 (50.0MB)
   0.0% used
PS Old Generation
   capacity = 524288000 (500.0MB)
   used     = 0 (0.0MB)
   free     = 524288000 (500.0MB)
   0.0% used
PS Perm Generation
   capacity = 21757952 (20.75MB)
   used     = 2606792 (2.4860305786132812MB)
   free     = 19151160 (18.26396942138672MB)
   11.980870258377259% used
实验3:-XX:NewRatio单独设置有效
[@zw_83_83 test]# java -Xms900m -Xmx900m -XX:NewRatio=2 JVMNewSize
running ...

NewRatio的定义是:New/Old=1:NewRatio
所以,NewRatio是2的话,New占1/3的Heap,也就是300m了。

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 943718400 (900.0MB)
   NewSize          = 1310720 (1.25MB)  设置NewRatio并不影响NewSize和MaxNewSize的数值,但是影响分配。
   MaxNewSize       = 17592186044415 MB   此时MaxNewSize的确是默认值无穷大
   OldSize          = 5439488 (5.1875MB)
   NewRatio         = 2
   SurvivorRatio    = 8
   PermSize         = 21757952 (20.75MB)
   MaxPermSize      = 85983232 (82.0MB)

Heap Usage:
PS Young Generation 新生代加起来的确是300m
Eden Space:
   capacity = 235929600 (225.0MB)
   used     = 4718608 (4.5000152587890625MB)
   free     = 231210992 (220.49998474121094MB)
   2.0000067816840277% used
From Space:
   capacity = 39321600 (37.5MB)
   used     = 0 (0.0MB)
   free     = 39321600 (37.5MB)
   0.0% used
To Space:
   capacity = 39321600 (37.5MB)
   used     = 0 (0.0MB)
   free     = 39321600 (37.5MB)
   0.0% used
PS Old Generation
   capacity = 629145600 (600.0MB)
   used     = 0 (0.0MB)
   free     = 629145600 (600.0MB)
   0.0% used
PS Perm Generation
   capacity = 21757952 (20.75MB)
   used     = 2606792 (2.4860305786132812MB)
   free     = 19151160 (18.26396942138672MB)
   11.980870258377259% used


[@zw_83_83 test]# java -Xms900m -Xmx900m -XX:NewRatio=8 JVMNewSize
running ...
NewRatio=8,则New占1/9,也就是100m

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 943718400 (900.0MB)
   NewSize          = 1310720 (1.25MB)
   MaxNewSize       = 17592186044415 MB
   OldSize          = 5439488 (5.1875MB)
   NewRatio         = 8
   SurvivorRatio    = 8
   PermSize         = 21757952 (20.75MB)
   MaxPermSize      = 85983232 (82.0MB)

Heap Usage:
PS Young Generation  年轻代累加和是:100m
Eden Space:
   capacity = 78643200 (75.0MB)
   used     = 1572880 (1.5000152587890625MB)
   free     = 77070320 (73.49998474121094MB)
   2.0000203450520835% used
From Space:
   capacity = 13107200 (12.5MB)
   used     = 0 (0.0MB)
   free     = 13107200 (12.5MB)
   0.0% used
To Space:
   capacity = 13107200 (12.5MB)
   used     = 0 (0.0MB)
   free     = 13107200 (12.5MB)
   0.0% used
PS Old Generation
   capacity = 838860800 (800.0MB)
   used     = 0 (0.0MB)
   free     = 838860800 (800.0MB)
   0.0% used
PS Perm Generation
   capacity = 21757952 (20.75MB)
   used     = 2606792 (2.4860305786132812MB)
   free     = 19151160 (18.26396942138672MB)
   11.980870258377259% used
实验4:-XX:NewRatio被-Xmn覆盖
[@zw_83_83 test]# java -Xms900m -Xmx900m -Xmn400m -XX:NewRatio=8 JVMNewSize
running ...

通过Xmn设置400m,但是NewRatio=8,表示100m,最终是400m还是100呢??

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 943718400 (900.0MB)
   NewSize          = 419430400 (400.0MB)
   MaxNewSize       = 419430400 (400.0MB)
   OldSize          = 5439488 (5.1875MB)
   NewRatio         = 8   数值上都是按设置的数值,变化了!!!
   SurvivorRatio    = 8
   PermSize         = 21757952 (20.75MB)
   MaxPermSize      = 85983232 (82.0MB)

Heap Usage:
PS Young Generation  新生代的累加和是400m
Eden Space:
   capacity = 314572800 (300.0MB)
   used     = 6291472 (6.0000152587890625MB)
   free     = 308281328 (293.99998474121094MB)
   2.000005086263021% used
From Space:
   capacity = 52428800 (50.0MB)
   used     = 0 (0.0MB)
   free     = 52428800 (50.0MB)
   0.0% used
To Space:
   capacity = 52428800 (50.0MB)
   used     = 0 (0.0MB)
   free     = 52428800 (50.0MB)
   0.0% used
PS Old Generation
   capacity = 524288000 (500.0MB)
   used     = 0 (0.0MB)
   free     = 524288000 (500.0MB)
   0.0% used
PS Perm Generation
   capacity = 21757952 (20.75MB)
   used     = 2606792 (2.4860305786132812MB)
   free     = 19151160 (18.26396942138672MB)
   11.980870258377259% used


你可能感兴趣的:(jvm,Xmn)