java

当前位置:中华考试网 >> java >> java基础 >> 文章内容

java入门教程:数据类型_设置堆的比例分配

来源:中华考试网  [2016年5月5日]  【

设置堆的比例分配

  参数-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(15));

handler.add(h1.getSubString(15));

}

}

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 [0x270100000x27a10000

0x27a10000)

  eden space 8192K, 2% used [0x270100000x2703a9780x27810000)

  from space 1024K, 0% used [0x279100000x279100000x27a10000)

  to space 1024K, 0% used [0x278100000x278100000x27910000)

  tenured generation total 251904K, used 233743K [0x27a100000x37010000

0x37010000)

  the space 251904K, 92% used [0x27a100000x35e53d000x35e53e00

0x37010000)

  compacting perm gen total 12288K, used 372K [0x370100000x37c10000

0x3b010000)

  the space 12288K, 3% used [0x370100000x3706d3100x3706d400

0x37c10000)

  ro space 10240K, 51% used [0x3b0100000x3b5430000x3b543000

0x3ba10000)

  rw space 12288K, 55% used [0x3ba100000x3c0ae4f80x3c0ae600

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 [0x270100000x27a10000

0x27a10000)

  eden space 5120K, 2% used [0x270100000x2702b3b00x27510000)

  from space 2560K, 0% used [0x275100000x275100000x27790000)

  to space 2560K, 0% used [0x277900000x277900000x27a10000)

  tenured generation total 251904K, used 233743K [0x27a100000x37010000

0x37010000)

  the space 251904K, 92% used [0x27a100000x35e53d000x35e53e00

0x37010000)

  compacting perm gen total 12288K, used 372K [0x370100000x37c10000

0x3b010000)

  the space 12288K, 3% used [0x370100000x3706d3100x3706d400

0x37c10000)

  ro space 10240K, 51% used [0x3b0100000x3b5430000x3b543000

0x3ba10000)

  rw space 12288K, 55% used [0x3ba100000x3c0ae4f80x3c0ae600

0x3c610000)

责编:ljnbset
  • 会计考试
  • 建筑工程
  • 职业资格
  • 医药考试
  • 外语考试
  • 学历考试