JVM_2_常用的基本配置参数

25 篇文章 0 订阅
6 篇文章 0 订阅

JVM_2_常用的基本配置参数

-Xms

  • 初始大小内存,默认为物理内存的1/64
  • 等价于-XX:InitialHeapSize

-Xmx

  • 最大分配内存,默认为物理内存的1/4
  • 等价于-XX:MaxHeapSize

-Xss

  • 设置单个线程栈的大小,一般默认为512k~1024k
  • 等价于-XX:ThreadStackSize

-Xmn

  • 设置年轻代大小
  • 年轻代默认大小是堆的1/3

-XX:MetaspaceSize

  • 设置元空间大小

    元空间的本质和永久代类似,都是对JVM规范中方法区的实现;不过元空间和永久代最大的区别是:元空间并不在虚拟机中,而是使用本地内存,因此,默认情况下,元空间的大小仅受本地内存限制

  • -Xms10m -Xmx10m -XX:MetaspaceSize=1024m -XX:+PrintFlagsFinal

  • 默认:21M

典型设置案例

  • -XX:+UseSerialGC :串行垃圾回收器
  • -XX:+UseParallelGC:并行垃圾回收器

未配置参数前

  • -XX:+PrintCommandLineFlags
public static void main(String[] args) throws InterruptedException {
		System.out.println("*******HelloGC*********");
		Thread.sleep(Integer.MAX_VALUE);
}

结果:

-XX:InitialHeapSize=132118912 
-XX:MaxHeapSize=2113902592 
-XX:+PrintCommandLineFlags 
-XX:+UseCompressedClassPointers 
-XX:+UseCompressedOops
-XX:-UseLargePagesIndividualAllocation 
-XX:+UseParallelGC

配置参数之后

  • -Xms128m -Xmx4096m -Xss1024k -XX:MetaspaceSize=512m -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseSerialGC
public static void main(String[] args) throws InterruptedException {
		System.out.println("*******HelloGC*********");
		Thread.sleep(Integer.MAX_VALUE);
}

结果:

-XX:InitialHeapSize=134217728  
-XX:MaxHeapSize=4294967296 
-XX:MetaspaceSize=536870912 
-XX:+PrintCommandLineFlags 
-XX:+PrintGCDetails 
-XX:ThreadStackSize=1024 
-XX:+UseCompressedClassPointers 
-XX:+UseCompressedOops 
-XX:-UseLargePagesIndividualAllocation 
-XX:+UseSerialGC 

-XX:+PrintGCDetails

收集GC日志

输出详细GC收集日志信息

  • 未超限:-XX:+PrintGCDetails
public class HelloGC {

	public static void main(String[] args) throws InterruptedException {
		System.out.println("*******HelloGC*********");
	}
}

结果:

*******HelloGC*********
Heap
 PSYoungGen      total 37888K, used 3277K [0x00000000d6000000, 0x00000000d8a00000, 0x0000000100000000)
  eden space 32768K, 10% used [0x00000000d6000000,0x00000000d63335d8,0x00000000d8000000)
  from space 5120K, 0% used [0x00000000d8500000,0x00000000d8500000,0x00000000d8a00000)
  to   space 5120K, 0% used [0x00000000d8000000,0x00000000d8000000,0x00000000d8500000)
 ParOldGen       total 86016K, used 0K [0x0000000082000000, 0x0000000087400000, 0x00000000d6000000)
  object space 86016K, 0% used [0x0000000082000000,0x0000000082000000,0x0000000087400000)
 Metaspace       used 2819K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 301K, capacity 386K, committed 512K, reserved 1048576K

  • 超限:-Xms10m -Xmx10m -XX:+PrintGCDetails
public class HelloGC {
	public static void main(String[] args) throws InterruptedException {
		System.out.println("*******HelloGC*********");
		byte[] test = new byte[50*1024*1024];
	}
}

结果:

*******HelloGC*********
[GC (Allocation Failure) [PSYoungGen: 901K->504K(2560K)] 901K->592K(9728K), 0.0006480 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 504K->488K(2560K)] 592K->616K(9728K), 0.0004558 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 488K->0K(2560K)] [ParOldGen: 128K->562K(7168K)] 616K->562K(9728K), [Metaspace: 2814K->2814K(1056768K)], 0.0046056 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] 562K->562K(9728K), 0.0001626 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] [ParOldGen: 562K->549K(7168K)] 562K->549K(9728K), [Metaspace: 2814K->2814K(1056768K)], 0.0045357 secs] [Times: user=0.06 sys=0.00, real=0.00 secs] 
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at top.ygy.jvm.HelloGC.main(HelloGC.java:13)
Heap
 PSYoungGen      total 2560K, used 61K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
  eden space 2048K, 3% used [0x00000000ffd00000,0x00000000ffd0f748,0x00000000fff00000)
  from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
  to   space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
 ParOldGen       total 7168K, used 549K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
  object space 7168K, 7% used [0x00000000ff600000,0x00000000ff689760,0x00000000ffd00000)
 Metaspace       used 2846K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 304K, capacity 386K, committed 512K, reserved 1048576K
ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):  [util.c:840]
  • 分析(未完成)
[Full GC (Allocation Failure) [PSYoungGen: 488K->0K(2560K)] [ParOldGen: 128K->562K(7168K)] 616K->562K(9728K), [Metaspace: 2814K->2814K(1056768K)], 0.0046056 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 

-XX:SurvivorRatio

-XX:NewRatio

  • 配置年轻代和老年代在堆结构的占比
  • 默认:-XX:NewRatio=2,新生代占1,老年代占2,年轻代占整个堆的1/3
  • 假如:-XX:NewRatio=4,新生代占1,老年代占4,年轻代占整个堆的1/5
  • -XX:NewRatio就是设置老年代的占比,剩下的1给新生代

-XX:MaxTenuringThreshold

  • 设置垃圾的最大年龄
  • -XX:MaxTenuringThreshold=0,直接进入老年代
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值