java入门教程:数据类型_设置堆的比例分配
来源 :中华考试网 2016-05-05
中设置堆的比例分配
参数-XX:SurvivorRatio 是用来设置新生代中 eden 空间和 s0 空间的比例关系。s0 和 s1 空间又分别称为 from 空间和 to 空间。它们的大小是相同的,职能也是相同的,并在 Minor GC 后互换角色。
清单 15. 所示示例演示不断插入字符时使用的 GC 输出
import java.util.ArrayList;
import java.util.List;
public class StringDemo {
public static void main(String[] args){
List handler = new ArrayList();
for ( int i= 0 ;i< 1000 ;i++){
HugeStr h = new HugeStr();
ImprovedHugeStr h1 = new ImprovedHugeStr();
handler.add(h.getSubString( 1 , 5 ));
handler.add(h1.getSubString( 1 , 5 ));
}
}
static class HugeStr{
private String str = new String( new char [ 800000 ]);
public String getSubString( int begin, int end){
return str.substring(begin, end);
}
}
static class ImprovedHugeStr{
private String str = new String( new char [ 10000000 ]);
public String getSubString( int begin, int end){
return new String(str.substring(begin, end));
}
}
}
|
清单 16. 设置新生代堆为 10MB,并使 eden 区是 s0 的 8
-XX:+PrintGCDetails -XX:MaxNewSize=10M -XX:SurvivorRatio= 8
|
清单 17. 运行输出
[Full GC [Tenured: 233756K->233743K(251904K), 0.0524229 secs]
233756K->233743K(261120K),
[Perm : 377K->372K(12288K)], 0.0524703 secs] [Times: user= 0.06 sys= 0.00 ,
real= 0.06 secs]
def new generation total 9216K, used 170K [ 0x27010000 , 0x27a10000 ,
0x27a10000 )
eden space 8192K, 2 % used [ 0x27010000 , 0x2703a978 , 0x27810000 )
from space 1024K, 0 % used [ 0x27910000 , 0x27910000 , 0x27a10000 )
to space 1024K, 0 % used [ 0x27810000 , 0x27810000 , 0x27910000 )
tenured generation total 251904K, used 233743K [ 0x27a10000 , 0x37010000 ,
0x37010000 )
the space 251904K, 92 % used [ 0x27a10000 , 0x35e53d00 , 0x35e53e00 ,
0x37010000 )
compacting perm gen total 12288K, used 372K [ 0x37010000 , 0x37c10000 ,
0x3b010000 )
the space 12288K, 3 % used [ 0x37010000 , 0x3706d310 , 0x3706d400 ,
0x37c10000 )
ro space 10240K, 51 % used [ 0x3b010000 , 0x3b543000 , 0x3b543000 ,
0x3ba10000 )
rw space 12288K, 55 % used [ 0x3ba10000 , 0x3c0ae4f8 , 0x3c0ae600 ,
0x3c610000 )
|
修改参数 SurvivorRatio 为 2 运行程序,相当于设置 eden 区是 s0 的 2 倍大小,由于 s1 与 s0 相同,故有 eden=[10MB/(1+1+2)]*2=5MB。
清单 18. 运行输出
[Full GC [Tenured: 233756K->233743K(251904K),
0.0546689
secs]
233756K->233743K(259584K),
[Perm : 377K->372K(12288K)],
0.0547257
secs] [Times: user=
0.05
sys=
0.00
,
real=
0.05
secs]
def
new
generation total 7680K, used 108K [
0x27010000
,
0x27a10000
,
0x27a10000
)
eden space 5120K,
2
% used [
0x27010000
,
0x2702b3b0
,
0x27510000
)
from space 2560K,
0
% used [
0x27510000
,
0x27510000
,
0x27790000
)
to space 2560K,
0
% used [
0x27790000
,
0x27790000
,
0x27a10000
)
tenured generation total 251904K, used 233743K [
0x27a10000
,
0x37010000
,
0x37010000
)
the space 251904K,
92
% used [
0x27a10000
,
0x35e53d00
,
0x35e53e00
,
0x37010000
)
compacting perm gen total 12288K, used 372K [
0x37010000
,
0x37c10000
,
0x3b010000
)
the space 12288K,
3
% used [
0x37010000
,
0x3706d310
,
0x3706d400
,
0x37c10000
)
ro space 10240K,
51
% used [
0x3b010000
,
0x3b543000
,
0x3b543000
,
0x3ba10000
)
rw space 12288K,
55
% used [
0x3ba10000
,
0x3c0ae4f8
,
0x3c0ae600
,
0x3c610000
)