java 虚拟机 参数配置

堆分配参数(一)

-Xms5m 设置Java程序启动时初始化大小

-Xmx20m 设置Java程序能获得的最大堆大小

-XX:+PrintGCDetails  可以产看详细细心,包括各个区的情况

-XX:+UseSerialGC 配置窜行回收器

-XX:+PrintCommandLineFlags 可以将隐式或显示传给虚拟机的参数输出

public class Test01 {

	public static void main(String[] args) {

		//-Xms5m -Xmx20m -XX:+PrintGCDetails -XX:+UseSerialGC -XX:+PrintCommandLineFlags
		
		//查看GC信息
		System.out.println("max memory:" + Runtime.getRuntime().maxMemory());
		System.out.println("free memory:" + Runtime.getRuntime().freeMemory());
		System.out.println("total memory:" + Runtime.getRuntime().totalMemory());
		
		byte[] b1 = new byte[1*1024*1024];
		System.out.println("分配了1M");
		System.out.println("max memory:" + Runtime.getRuntime().maxMemory());
		System.out.println("free memory:" + Runtime.getRuntime().freeMemory());
		System.out.println("total memory:" + Runtime.getRuntime().totalMemory());
		
		byte[] b2 = new byte[4*1024*1024];
		System.out.println("分配了4M");
		System.out.println("max memory:" + Runtime.getRuntime().maxMemory());
		System.out.println("free memory:" + Runtime.getRuntime().freeMemory());
		System.out.println("total memory:" + Runtime.getRuntime().totalMemory());
		
	}
	
}




-XX:InitialHeapSize=5242880 -XX:MaxHeapSize=20971520 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseSerialGC 
max memory:20316160
free memory:5319624
total memory:6094848
[GC (Allocation Failure) [DefNew: 757K->191K(1856K), 0.0018204 secs] 757K->527K(5952K), 0.0022422 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
分配了1M
max memory:20316160
free memory:4472096
total memory:6094848
[GC (Allocation Failure) [DefNew: 1249K->0K(1856K), 0.0014761 secs][Tenured: 1551K->1551K(4096K), 0.0023406 secs] 1584K->1551K(5952K), [Metaspace: 2656K->2656K(1056768K)], 0.0038755 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
分配了4M
max memory:20316160
free memory:4540408
total memory:10358784
Heap
 def new generation   total 1920K, used 69K [0x00000000fec00000, 0x00000000fee10000, 0x00000000ff2a0000)//新生代
  eden space 1728K,   4% used [0x00000000fec00000, 0x00000000fec115e8, 0x00000000fedb0000) 
  from space 192K,   0% used [0x00000000fedb0000, 0x00000000fedb0000, 0x00000000fede0000)
  to   space 192K,   0% used [0x00000000fede0000, 0x00000000fede0000, 0x00000000fee10000)
 tenured generation   total 8196K, used 5647K [0x00000000ff2a0000, 0x00000000ffaa1000, 0x0000000100000000) //老年代
   the space 8196K,  68% used [0x00000000ff2a0000, 0x00000000ff823dd0, 0x00000000ff823e00, 0x00000000ffaa1000)
 Metaspace       used 2662K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 288K, capacity 386K, committed 512K, reserved 1048576K


总结:在实际工作中,我们可以直接将初始的堆大小与最大堆大小设置相等,这样的好处事可以减少程序运行时的垃圾回收次数,从而提高性能。

堆分配参数(二)

新生代的配置

-Xmn:可以设置新生代的大小,设置一个比较大的新生代会减少老年代的大小,这个参数对系统性能以及GC行为有很大的影响,新生代一般会设置整个堆空间的1/3到1/4左右。

-XX:SurvivorRation 用来设置新生代中eden空间和form/to的空间比例,含义-XX:SurvivorRatio=eden/from=eden/to

-XX:NewRatio 老年代/新生代的比例

public class Test02 {

	public static void main(String[] args) {
		
		//第一次配置
		//-Xms20m -Xmx20m -Xmn1m -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC
		
		//第二次配置
		//-Xms20m -Xmx20m -Xmn7m -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC
		
		//第三次配置
		//-XX:NewRatio=老年代/新生代
		//-Xms20m -Xmx20m -XX:NewRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC
		
		byte[] b = null;
		//连续向系统申请10MB空间
		for(int i = 0 ; i <10; i ++){
			b = new byte[1*1024*1024];
		}
	}
}



第一次配置
[GC [DefNew: 506K->256K(768K), 0.0013800 secs] 506K->429K(20224K), 0.0014078 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] Heap def new generation total 768K, used 628K [0x00000000f9a00000, 0x00000000f9b00000, 0x00000000f9b00000) eden space 512K, 72% used [0x00000000f9a00000, 0x00000000f9a5d030, 0x00000000f9a80000) from space 256K, 100% used [0x00000000f9ac0000, 0x00000000f9b00000, 0x00000000f9b00000) to space 256K, 0% used [0x00000000f9a80000, 0x00000000f9a80000, 0x00000000f9ac0000) tenured generation total 19456K, used 10413K [0x00000000f9b00000, 0x00000000fae00000, 0x00000000fae00000) the space 19456K, 53% used [0x00000000f9b00000, 0x00000000fa52b4e0, 0x00000000fa52b600, 0x00000000fae00000) compacting perm gen total 21248K, used 2619K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000) the space 21248K, 12% used [0x00000000fae00000, 0x00000000fb08edc8, 0x00000000fb08ee00, 0x00000000fc2c0000)No shared spaces configured.
第二次配置
[GC [DefNew: 2932K->1606K(5376K), 0.0026000 secs] 2932K->1606K(18688K), 0.0026235 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC [DefNew: 4868K->1024K(5376K), 0.0020513 secs] 4868K->1606K(18688K), 0.0020804 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC [DefNew: 4126K->1024K(5376K), 0.0008185 secs] 4708K->1606K(18688K), 0.0008305 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 def new generation   total 5376K, used 3163K [0x00000000f9a00000, 0x00000000fa100000, 0x00000000fa100000)
  eden space 3584K,  59% used [0x00000000f9a00000, 0x00000000f9c16df8, 0x00000000f9d80000)
  from space 1792K,  57% used [0x00000000f9f40000, 0x00000000fa040010, 0x00000000fa100000)
  to   space 1792K,   0% used [0x00000000f9d80000, 0x00000000f9d80000, 0x00000000f9f40000)
 tenured generation   total 13312K, used 582K [0x00000000fa100000, 0x00000000fae00000, 0x00000000fae00000)
   the space 13312K,   4% used [0x00000000fa100000, 0x00000000fa191b88, 0x00000000fa191c00, 0x00000000fae00000)
 compacting perm gen  total 21248K, used 2622K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
   the space 21248K,  12% used [0x00000000fae00000, 0x00000000fb08f9e8, 0x00000000fb08fa00, 0x00000000fc2c0000)
No shared spaces configured.
//第三次配置
[GC [DefNew: 5065K->582K(6144K), 0.0024174 secs] 5065K->1606K(19840K), 0.0024431 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC [DefNew: 5987K->0K(6144K), 0.0021600 secs] 7011K->2630K(19840K), 0.0021848 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 def new generation   total 6144K, used 1079K [0x00000000f9a00000, 0x00000000fa0a0000, 0x00000000fa0a0000)
  eden space 5504K,  19% used [0x00000000f9a00000, 0x00000000f9b0dca0, 0x00000000f9f60000)
  from space 640K,   0% used [0x00000000f9f60000, 0x00000000f9f60100, 0x00000000fa000000)
  to   space 640K,   0% used [0x00000000fa000000, 0x00000000fa000000, 0x00000000fa0a0000)
 tenured generation   total 13696K, used 2630K [0x00000000fa0a0000, 0x00000000fae00000, 0x00000000fae00000)
   the space 13696K,  19% used [0x00000000fa0a0000, 0x00000000fa331aa8, 0x00000000fa331c00, 0x00000000fae00000)
 compacting perm gen  total 21248K, used 2622K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
   the space 21248K,  12% used [0x00000000fae00000, 0x00000000fb08f9e8, 0x00000000fb08fa00, 0x00000000fc2c0000)
No shared spaces configured.



-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=d:/Test03.dump
在java程序的运行过程中,如果堆空间不足,则会抛出内存溢出的错误OOM,一旦这类问题发生在生产环境,可能引起严重的业务中断,java虚拟机提供了-XX:+HeapDumpOnOutOfMemoryErro,使用该参数可以在内存溢出时导出整个堆信息,与之配合使用的还有-XX:HeapDumpPath,可以设置导出堆的存放路径
内存分析工具 Memory Analyzer 1.5.0

可以与tomcat集成

http://download.eclipse.org/mat/1.0/update-site/    


public class Test04 {

	//-Xss1m  
	//-Xss5m
	
	//栈调用深度
	private static int count;
	
	public static void recursion(){
		count++;
		recursion();
	}
	public static void main(String[] args){
		try {
			recursion();
		} catch (Throwable t) {
			System.out.println("调用最大深入:" + count);
			t.printStackTrace();
		}
	}
}


方法区:

和java堆一样,方法区是一块所有线程共享的内存区域,用于保存系统的类信息,方法区可以可以保存多少信息可以对其进行配置,在默认情况下 -XX:MaxPermSize为64MB

-XX:MaxPermSize=64M -XX:PermSize=64M









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值