JVM包括如下核心组件:
1、堆
2、栈
3、持久代及方法区
4、JIT编译器
5、代码缓存
JVM内存空间分为三部分:
Permanent Space、Young Generation、Old Generation。
其中,Young Generation又分为:Eden Space、Survivor0、Survivor1;
这里Total Heap Size是不包括PermSize的,所以实际上java进程占用的内存会比设置-Xms和-Xmx更大。
设置JVM总的内存大小参数 :
-Xms1024m
-Xmx1024m
-XX:PermSize=150m
-XXMaxPermSize=150m
设置Young Generation的大小方法:
(1)-XX:NewSize=300m和
-XX:MaxNewSize=300m(Eden Space+Survivor0+Survivor1);
(2)-Xmn300m(Eden Space+两个Survivor Space的大小);
(3)-XX:NewRatio=2;
优先级:
(1)最高优先级: -XX:NewSize=300m和-XX:MaxNewSize=300m
(2)次高优先级: -Xmn300m (默认等效效果是:-XX:NewSize==-XX:MaxNewSize==300m)
(3)最低优先级:-XX:NewRatio=2
推荐使用的是-Xmn参数,原因是这个参数很简洁,相当于一次性设定NewSize和MaxNewSIze,而且两者相等。
调整各区比例:
1)-XX:SurvivorRatio=8(Eden/Suvivor)
2)-XX:NewRatio=2(Young/Old)
以下为测试情况:
1、设置JVM参数
结果:Eden Space + Old Gen = 1024M, Perm Gen = 150M, Old Gen = (Eden Space)*2 =600M, 小于1024M-Eden Space=724M,则Old Gen=724M
-Xms1024m -Xmx1024m -XX:PermSize=150m -XX:MaxPermSize=150m -XX:NewSize=300m -XX:MaxNewSize=300m -XX:NewRatio=2 -XX:SurvivorRatio=8
![](http://dl2.iteye.com/upload/attachment/0118/4240/0941717b-01cc-32c4-9ec0-edc2712b7fcb.png)
结果:Eden Space + Old Gen = 1024M, Perm Gen = 150M, Old Gen = (Eden Space)*2 =600M, 小于1024M-Eden Space=724M,则Old Gen=724M
2、
结果:Eden Space + Old Gen = 1024M, Perm Gen = 256M, Old Gen = (Eden Space + S0 + S1)*2
-Xms1024m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:NewSize=300m -XX:MaxNewSize=300m -XX:NewRatio=2 -XX:SurvivorRatio=8
![](http://dl2.iteye.com/upload/attachment/0118/4242/9cabd032-3ff0-33eb-9221-d76d8879a735.png)
结果:Eden Space + Old Gen = 1024M, Perm Gen = 256M, Old Gen = (Eden Space + S0 + S1)*2
3、
结果:Eden Space + Old Gen = 1024M, Perm Gen = 256M, Old Gen = (Eden Space + S0 + S1)*2 = 921.25M, OldGen大于 1024M- (Eden Space + S0 + S1)=563,则取1024M-Eden Space = 640M
-Xms1024m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:NewSize=384m -XX:MaxNewSize=384m -XX:NewRatio=2 -XX:SurvivorRatio=8
![](http://dl2.iteye.com/upload/attachment/0118/4256/d21dd0db-0b77-3bac-9d1c-bf2022a0353f.png)
结果:Eden Space + Old Gen = 1024M, Perm Gen = 256M, Old Gen = (Eden Space + S0 + S1)*2 = 921.25M, OldGen大于 1024M- (Eden Space + S0 + S1)=563,则取1024M-Eden Space = 640M
4、
结果:Eden Space + Old Gen = 1024M, Perm Gen = 256M, Old Gen =424M,Eden Space = 600M
-Xms1024m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:NewSize=600m -XX:MaxNewSize=600m -XX:NewRatio=2 -XX:SurvivorRatio=8
![](http://dl2.iteye.com/upload/attachment/0118/4265/ba19d279-b7d8-3b15-8949-df5ead472c0a.png)
结果:Eden Space + Old Gen = 1024M, Perm Gen = 256M, Old Gen =424M,Eden Space = 600M
结论是:Xms和Xmx设定的内存大小 = Eden Space + Old Gen, 而Perm Gen 、S0、S1的内存 不受Xmx控制。
(
结论跟大家情况不符,很是奇怪,本人不才
,不知道为什么会这样子, 哪位大拿不吝指教)
![尴尬 尴尬](http://www.iteye.com/javascripts/tinymce/plugins/emotions/img/smiley-embarassed.gif)