3 你平时工作用过的 JVM 常用的基本配置参数有哪些?

  • -Xms
    • 初始大小内存,默认为物理内存 1/64
    • 等价于 -XX:InitialHeapSize
  • -Xmx
    • 最大分配内存,默认为物理内存的 1/4
    • 等价于 -XX:MaxHeapSize
  • -Xss
    • 设置单个线程栈的大小,一般默认为 512-1024k
    • 等价于 -XX:ThreadStackSize
  • -Xmn
    • 设置年轻代的大小
    • 整个JVM内存大小=年轻代大小 + 年老代大小 + 持久代大小,持久代一般固定大小为64m,所以增大年轻代后,将会减小年老代大小。此值对系统性能影响较大,Sun官方推荐配置为整个堆的3/8。
  • -XX:MetaspaceSize
    • 设置元空间大小(元空间的本质和永久代类似,都是对 JVM 规范中的方法区的实现,不过元空间于永久代之间最大区别在于,元空间并不在虚拟中,而是使用本地内存,因此默认情况下,元空间的大小仅受本地内存限制)
    • 元空间默认比较小,我们可以调大一点
  • -XX:+PrintGCDetails

    • 输出详细 GC 收集日志信息

      • 设置 JVM 参数为: -Xms10m -Xmx10m -XX:+PrintGCDetails
      • 代码

        1
        2
        3
        4
        5
        
        public class HelloGC {
            public static void main(String[] args) {
                byte[] bytes = new byte[20 * 1024 * 1024];
            }
        }
        
      • 打印结果

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        
        [GC (Allocation Failure) [PSYoungGen: 1231K->448K(2560K)] 1231K->456K(9728K), 0.0015616 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
        [GC (Allocation Failure) [PSYoungGen: 448K->384K(2560K)] 456K->392K(9728K), 0.0016999 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
        [Full GC (Allocation Failure) [PSYoungGen: 384K->0K(2560K)] [ParOldGen: 8K->358K(7168K)] 392K->358K(9728K), [Metaspace: 3028K->3028K(1056768K)], 0.0066696 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
        [GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] 358K->358K(9728K), 0.0005321 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
        [Full GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] [ParOldGen: 358K->340K(7168K)] 358K->340K(9728K), [Metaspace: 3028K->3028K(1056768K)], 0.0051543 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
        Heap
         PSYoungGen      total 2560K, used 81K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
          eden space 2048K, 3% used [0x00000000ffd00000,0x00000000ffd14668,0x00000000fff00000)
          from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
          to   space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
         ParOldGen       total 7168K, used 340K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
          object space 7168K, 4% used [0x00000000ff600000,0x00000000ff655188,0x00000000ffd00000)
         Metaspace       used 3060K, capacity 4496K, committed 4864K, reserved 1056768K
          class space    used 336K, capacity 388K, committed 512K, reserved 1048576K
        Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        	at com.cuzz.jvm.HelloGC.main(HelloGC.java:12)
        
    • GC
      img

    • FullGC
      img
  • -XX:SurvivorRatio
    • 设置新生代中 eden 和 S0/S1 空间比例
    • 默认 -XX:SurvivorRatio=8,Eden : S0 : S1 = 8 : 1 : 1
  • -XX:NewRatio
    • 配置年轻代和老年代在堆结构的占比
    • 默认 -XX:NewRatio=2 新生代占1,老年代占2,年轻代占整个堆的 1/3
  • -XX:MaxTenuringThreshold
    • 设置垃圾最大年龄
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值