JVM常用启动参数

官方文档:https://docs.oracle.com/en/java/javase/13/docs/specs/man/java.html

「自」IDEA如何修改JVM启动参数
「自」Eclipse如何修改JVM启动参数

日志打印

打印VM参数: -XX:+PrintCommandLineFlags (隐式传递+显式传递)
类加载顺序打印:-XX:+TraceClassLoading / -Xlog:class+load=info
导出GC日志: -Xloggc:d:/gc.log
堆内存异常时导出日志: -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:/

内存分配

指定栈内存大小:-Xss1m / -XX:ThreadStackSize=512k: -X表选项,ss代表Stack Size
指定堆内存大小:-Xmx20m -Xms5m : -X表选项,个人理解 mxMemory MaxmsMemory start
指定直接内存大小:-XX:MaxDirectMemorySize 直接内存的最大大小

年轻代

-XX:NewSize=20m -XX:MaxNewSize=40m,-Xmn30m,-XX:NewRatio=5[最大值的比例]
-XX:SurvivorRatio=8
在这里插入图片描述

老年代


什么是 TLAB?

Thread Local Allocation Buffer 即线程本地分配缓存,是一个线程专用的内存分配区域,是为了加速对象分配而生的。每一个线程都会产生一个TLAB,该线程独享的工作区域,java虚拟机使用这种TLAB区来避免多线程冲突问题,提高对象的效率。TLAB空间一般不会太大,当大对象无法在TLAB分配时,则会直接分配到堆上。

  • -XX:+UseTLAB:使用TLAB
  • -XX:-UseTLAB:禁用TLAB
  • -XX:+TLABASize:设置TLAB大小
  • -XX:+PrintTLAB:查看TLAB信息
  • -XX:TLABRefillWasteFraction:设置进入TLAB空间的单个对象大小,是个比例值,默认为64,即如果对象大于整个空间的1/64,则在堆创建对象
  • -XX:ResizeTLAB:自动调整TLABRefillWasteFraction阈值

永久代 ( JDK1.8以后废弃)

  • -XX:PermSize=64M :设置永久代(方法区叫的不太准确)的初始大小,默认情况下是64M
  • -XX:MaxPermSize=64M:设置永久代(方法区叫的不太准确)的最大大小,默认情况下是64M

元空间

永久代使用的是堆内存空间,元空间使用本级物理内存,所以元空间受到本级物理内存的限制。

  • -XX:MetaspaceSize 设置元空间的初始大小,以字节(B)为单位。如果应用程序在运行过程中需要更多元空间,JVM 会自动扩大元空间,直至达到 -XX:MaxMetaspaceSize

  • -XX:MaxMetaspaceSize:设置元空间最大值,默认是-1,即不限制,只受限于本地内存大小。当元空间的大小达到这个限制时,JVM 将停止进一步扩大元空间,并开始触发类卸载操作,试图回收不再使用的类元数据以释放空间。

  • -XX:MaxMetaspaceFreeRatio 可以控制元空间在何时进行收缩。当元空间经过垃圾收集后,空闲空间占总空间的比例超过了这个参数设定的值,说明元空间可能存在过多的未使用空间。在这种情况下,JVM 下次垃圾收集时会尝试缩小元空间的大小,释放不必要的系统内存资源。

  • -XX:MinMetaspaceFreeRatio 设置元空间在进行垃圾收集后的最小空闲比例。当元空间的空闲空间占总空间的比例低于此参数设定的值时,JVM 将触发针对元空间的垃圾收集(类卸载)。

-----------------------------------------------------------------------------读书笔记摘自 书名:深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)作者:周志明


GC日志格式

  • -XX:+PrintGC:只要遇到GC就会打印日志
-Xmx20m -Xms5m -XX:+UseSerialGC -XX:+PrintGC

[GC (Allocation Failure)  4085K->3700K(5952K), 0.0024001 secs]
[Full GC (Allocation Failure)  3700K->3699K(5952K), 0.0029992 secs]
-Xmx20m -Xms5m -XX:+UseSerialGC -XX:+PrintGCDetails

[GC (Allocation Failure)
[DefNew: 1013K->192K(1856K), 0.0018707 secs]
[Tenured: 3508K->3699K(4096K), 0.0027271 secs] 
4085K->3699K(5952K), 
[Metaspace: 2784K->2784K(1056768K)], 0.0046848 secs] 
[Times: user=0.00 sys=0.00, real=0.00 secs] 

Heap
 def new generation   total 2880K, used 79K [0x00000000fec00000, 0x00000000fef10000, 0x00000000ff2a0000)
  eden space 2624K,   3% used [0x00000000fec00000, 0x00000000fec13cd8, 0x00000000fee90000)
  from space 256K,   0% used [0x00000000fee90000, 0x00000000fee90000, 0x00000000feed0000)
  to   space 256K,   0% used [0x00000000feed0000, 0x00000000feed0000, 0x00000000fef10000)
 tenured generation   total 10268K, used 7795K [0x00000000ff2a0000, 0x00000000ffca7000, 0x0000000100000000)
   the space 10268K,  75% used [0x00000000ff2a0000, 0x00000000ffa3cf88, 0x00000000ffa3d000, 0x00000000ffca7000)
 Metaspace       used 2791K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 298K, capacity 386K, committed 512K, reserved 1048576K
  • -XX:+PrintGCTimeStamps:输出GC的时间戳信息
Arguments: -Xmx20m -Xms5m -XX:+UseSerialGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps

0.233: [GC (Allocation Failure) 
0.233: [DefNew: 1013K->192K(1856K), 0.0019655 secs]
0.235: [Tenured: 3508K->3699K(4096K), 0.0028492 secs]
4085K->3699K(5952K), 
[Metaspace: 2785K->2785K(1056768K)], 0.0049290 secs]
[Times: user=0.00 sys=0.00, real=0.01 secs] 

Heap
 def new generation   total 2880K, used 79K [0x00000000fec00000, 0x00000000fef10000, 0x00000000ff2a0000)
  eden space 2624K,   3% used [0x00000000fec00000, 0x00000000fec13cd8, 0x00000000fee90000)
  from space 256K,   0% used [0x00000000fee90000, 0x00000000fee90000, 0x00000000feed0000)
  to   space 256K,   0% used [0x00000000feed0000, 0x00000000feed0000, 0x00000000fef10000)
 tenured generation   total 10268K, used 7795K [0x00000000ff2a0000, 0x00000000ffca7000, 0x0000000100000000)
   the space 10268K,  75% used [0x00000000ff2a0000, 0x00000000ffa3cf88, 0x00000000ffa3d000, 0x00000000ffca7000)
 Metaspace       used 2793K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 298K, capacity 386K, committed 512K, reserved 1048576K
  • -XX:+PrintGCDateStamps:输出GC的时间戳信息(以日期的形式)
-Xmx20m -Xms5m -XX:+UseSerialGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps

2020-08-17T16:58:36.760+0800: [GC (Allocation Failure) 
2020-08-17T16:58:36.760+0800: [DefNew: 1013K->192K(1856K), 0.0027161 secs]
2020-08-17T16:58:36.763+0800: [Tenured: 3508K->3699K(4096K), 0.0028194 secs] 
4085K->3699K(5952K),
[Metaspace: 2785K->2785K(1056768K)], 0.0056658 secs]
[Times: user=0.00 sys=0.00, real=0.01 secs] 

Heap
 def new generation   total 2880K, used 79K [0x00000000fec00000, 0x00000000fef10000, 0x00000000ff2a0000)
  eden space 2624K,   3% used [0x00000000fec00000, 0x00000000fec13cd8, 0x00000000fee90000)
  from space 256K,   0% used [0x00000000fee90000, 0x00000000fee90000, 0x00000000feed0000)
  to   space 256K,   0% used [0x00000000feed0000, 0x00000000feed0000, 0x00000000fef10000)
 tenured generation   total 10268K, used 7795K [0x00000000ff2a0000, 0x00000000ffca7000, 0x0000000100000000)
   the space 10268K,  75% used [0x00000000ff2a0000, 0x00000000ffa3cf88, 0x00000000ffa3d000, 0x00000000ffca7000)
 Metaspace       used 2793K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 298K, capacity 386K, committed 512K, reserved 1048576K
  • -XX:+PrintHeapAtGC:在GC进行处理的前后打印堆内存信息
-Xmx20m -Xms5m -XX:+UseSerialGC -XX:+PrintGCDetails -XX:+PrintHeapAtGC

{Heap before GC invocations=0 (full 0):
 def new generation   total 1856K, used 1013K [0x00000000fec00000, 0x00000000fee00000, 0x00000000ff2a0000)
  eden space 1664K,  60% used [0x00000000fec00000, 0x00000000fecfd7b8, 0x00000000feda0000)
  from space 192K,   0% used [0x00000000feda0000, 0x00000000feda0000, 0x00000000fedd0000)
  to   space 192K,   0% used [0x00000000fedd0000, 0x00000000fedd0000, 0x00000000fee00000)
 tenured generation   total 4096K, used 3072K [0x00000000ff2a0000, 0x00000000ff6a0000, 0x0000000100000000)
   the space 4096K,  75% used [0x00000000ff2a0000, 0x00000000ff5a0010, 0x00000000ff5a0200, 0x00000000ff6a0000)
 Metaspace       used 2786K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 297K, capacity 386K, committed 512K, reserved 1048576K

[GC (Allocation Failure) 
[DefNew: 1013K->192K(1856K), 0.0023381 secs]
[Tenured: 3508K->3699K(4096K), 0.0026344 secs] 4085K->3699K(5952K), 
[Metaspace: 2786K->2786K(1056768K)], 0.0050673 secs] 
[Times: user=0.02 sys=0.00, real=0.00 secs] 

Heap after GC invocations=1 (full 1):
 def new generation   total 2880K, used 0K [0x00000000fec00000, 0x00000000fef10000, 0x00000000ff2a0000)
  eden space 2624K,   0% used [0x00000000fec00000, 0x00000000fec00000, 0x00000000fee90000)
  from space 256K,   0% used [0x00000000fee90000, 0x00000000fee90000, 0x00000000feed0000)
  to   space 256K,   0% used [0x00000000feed0000, 0x00000000feed0000, 0x00000000fef10000)
 tenured generation   total 6168K, used 3699K [0x00000000ff2a0000, 0x00000000ff8a6000, 0x0000000100000000)
   the space 6168K,  59% used [0x00000000ff2a0000, 0x00000000ff63cf78, 0x00000000ff63d000, 0x00000000ff8a6000)
 Metaspace       used 2786K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 297K, capacity 386K, committed 512K, reserved 1048576K
}

GC策略

「自」 JVM垃圾回收器,GC策略调整参数,学习笔记
在这里插入图片描述

验证用的代码

List<GarbageCollectorMXBean> l = ManagementFactory.getGarbageCollectorMXBeans();
Iterator var3 = l.iterator();
while (var3.hasNext()) {
    GarbageCollectorMXBean b = (GarbageCollectorMXBean)var3.next();
    System.out.println(b.getName());
}

-XX:+UseSerialGC

Copy
MarkSweepCompact

-XX:+UseParNewGC

ParNew
MarkSweepCompact

Java HotSpot(TM) 64-Bit Server VM warning: 
Using the ParNew young collector with the Serial old collector is deprecated and will likely be removed in a future release

-XX:+UseConcMarkSweepGC

ParNew
ConcurrentMarkSweep

-XX:+UseParallelGC

PS Scavenge
PS MarkSweep

-XX:+UseParallelOldGC

PS Scavenge
PS MarkSweep

-XX:+UseG1GC

G1 Young Generation
G1 Old Generation

收集器参数

  • -XX:ParallelGCThread=n:设置并行收集器操作使用的CPU数量
  • -XX:MaxGCPauseMillis=n:设置并行收集器最大暂停时间,单位为ms
  • -XX:GCTimeRatio=n:设置垃圾回收时间占程序运行时间的百分比

参考

JVM(Java虚拟机)优化大全和案例实战

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值