前言
2022 10/12 11:25
路漫漫其修远兮,吾将上下而求索
本文是根据尚硅谷学习所做笔记
仅供学习交流使用,转载注明出处
推荐
3.你平时工作用过的JVM常用基本配置参数有哪些?
说明
本文目录前是相关视频的名字和具体视频中思维导图的名字
题目
3.你平时工作用过的JVM常用基本配置参数有哪些?
65_堆内存初始大小快速复习
基础知识复习
case
package jvm3; /** * @author CSDN@日星月云 * @date 2022/10/12 11:31 */ public class HelloGC { public static void main(String[] args) { long totalMemory = Runtime.getRuntime().totalMemory() ;//返回Java虚拟机中的内存总量。 long maxMemory = Runtime.getRuntime().maxMemory() ;//返回Java虚拟机试图使用的最大内存量。 System.out.println("TOTAL_MEMORY(-Xms) = " + totalMemory + "(字节)、" +(totalMemory / (double)1024 / 1024) + "MB"); System.out.println("MAX_MEMORY(-Xmx) = " + maxMemory + "(字节)、"+(maxMemory / (double)1024 / 1024)+ "MB"); //TOTAL_MEMORY(-Xms) = 119013376(字节)、113.5MB 1/64 //MAX_MEMORY(-Xmx) = 1757937664(字节)、1676.5MB } }
常用参数
66_常用基础参数栈内存Xss讲解
-Xms
- 初始大小内存,默认为物理内存1/64
- 等价于-xx:InitialHeapSize
-Xmx
- 最大分配内存,默认为物理内存1/4
- 等价于-xX:MaxHeapsize
–Xss
- 设置单个线程栈的大小,一般默认为512k~1024K
- 等价于-xx:ThreadStackSize
E:\IdeaProjects\JavaMS\second_2\src\jvm2>jinfo -flag ThreadStackSize 20100 -XX:ThreadStackSize=0
0代表初始值,这个值的大小是取决于平台的
-Xss128k
E:\IdeaProjects\JavaMS\second_2\src\jvm2>jinfo -flag ThreadStackSize 13640 -XX:ThreadStackSize=128
67_常用基础参数元空间MetaspaceSize讲解
-Xmn
设置年轻代大小
-XX:MetaspaceSize
- 设置元空间大小
元空间的本质和永久代类似,都是对JVM规范中方法区的实现。
不过元空间与永久代之间最大的区别在于:
元空间并不在虚拟机中,而是使用本地内存。
因此,默认情况下,元空间的大小仅受本地内存限制
-Xms10m -Xmx10m -XX:MetaspaceSize=1024m -XX:+PrintFlagsFinal
java -XX:+PrintFlagsFinal uintx MetaspaceSize = 21807104 {pd product}
典型设置案例
-Xms128m -Xmx4096m -Xss1024k -XX:MetaspaceSize=512m -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX :+UseSerialGd
-XX :+UseSerialGd 串行垃圾回收器
-XX:+UseParallelGC 并行垃圾回收器
只设置
-XX:+PrintCommandLineFlags
-XX:InitialHeapSize=123581248 -XX:MaxHeapSize=1977299968 -XX:+PrintCommandLineFlags -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
设置之后
-XX:+PrintGCDetails
HelloGC Heap PSYoungGen total 35328K, used 4334K [0x00000000d8b80000, 0x00000000db280000, 0x0000000100000000) eden space 30720K, 14% used [0x00000000d8b80000,0x00000000d8fbbbe0,0x00000000da980000) from space 4608K, 0% used [0x00000000dae00000,0x00000000dae00000,0x00000000db280000) to space 4608K, 0% used [0x00000000da980000,0x00000000da980000,0x00000000dae00000) ParOldGen total 80896K, used 0K [0x000000008a200000, 0x000000008f100000, 0x00000000d8b80000) object space 80896K, 0% used [0x000000008a200000,0x000000008a200000,0x000000008f100000) Metaspace used 3251K, capacity 4496K, committed 4864K, reserved 1056768K class space used 355K, capacity 388K, committed 512K, reserved 1048576K Process finished with exit code 0
输出详细GC收集日志信息
设置
-Xms10m -Xmx10m -XX:+PrintGCDetails
package jvm3; /** * @author CSDN@日星月云 * @date 2022/10/12 11:31 */ public class HelloGC { public static void main(String[] args) { /* long totalMemory = Runtime.getRuntime().totalMemory() ;//返回Java虚拟机中的内存总量。 long maxMemory = Runtime.getRuntime().maxMemory() ;//返回Java虚拟机试图使用的最大内存量。 System.out.println("TOTAL_MEMORY(-Xms) = " + totalMemory + "(字节)、" +(totalMemory / (double)1024 / 1024) + "MB"); System.out.println("MAX_MEMORY(-Xmx) = " + maxMemory + "(字节)、"+(maxMemory / (double)1024 / 1024)+ "MB"); */ //TOTAL_MEMORY(-Xms) = 119013376(字节)、113.5MB 1/64 //MAX_MEMORY(-Xmx) = 1757937664(字节)、1676.5MB System.out.println("***********HelloGC"); byte[] byteArray=new byte[50*1024*1024]; // Thread.sleep(Integer.MAX_VALUE); } }
***********HelloGC [GC (Allocation Failure) [PSYoungGen: 1790K->488K(2560K)] 1790K->708K(9728K), 0.0017456 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [GC (Allocation Failure) [PSYoungGen: 488K->488K(2560K)] 708K->716K(9728K), 0.0005885 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [Full GC (Allocation Failure) [PSYoungGen: 488K->0K(2560K)] [ParOldGen: 228K->633K(7168K)] 716K->633K(9728K), [Metaspace: 3258K->3258K(1056768K)], 0.0043162 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] 633K->633K(9728K), 0.0011279 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [Full GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] [ParOldGen: 633K->615K(7168K)] 633K->615K(9728K), [Metaspace: 3258K->3258K(1056768K)], 0.0044713 secs] [Times: user=0.03 sys=0.01, real=0.01 secs] Heap PSYoungGen total 2560K, used 50K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000) eden space 2048K, 2% used [0x00000000ffd00000,0x00000000ffd0c870,0x00000000fff00000) from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000) to space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000) ParOldGen total 7168K, used 615K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000) object space 7168K, 8% used [0x00000000ff600000,0x00000000ff699e30,0x00000000ffd00000) Metaspace used 3289K, capacity 4496K, committed 4864K, reserved 1056768K class space used 360K, capacity 388K, committed 512K, reserved 1048576K Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at jvm3.HelloGC.main(HelloGC.java:20) Process finished with exit code 1
GC
FullGC
规律:
[名称: GC前内存占用 -> GC后内存占用 (该区内存总大小)]
当我们出现了老年代都扛不住的时候,就会出现OOM异常
69_常用基础参数SurvivorRatio讲解
-XX:SurvivorRatio
调节新生代中 eden 和 S0、S1的空间比例,默认为 -XX:SuriviorRatio=8,Eden:S0:S1 = 8:1:1
加入设置成 -XX:SurvivorRatio=4,则为 Eden:S0:S1 = 4:1:1
SurvivorRatio值就是设置eden区的比例占多少,S0和S1相同
Java堆从GC的角度还可以细分为:新生代(Eden区,From Survivor区合To Survivor区)和老年代
eden、SurvivorFrom复制到SurvivorTo,年龄 + 1
首先,当Eden区满的时候会触发第一次GC,把还活着的对象拷贝到SurvivorFrom去,当Eden区再次触发GC的时候会扫描Eden区合From区域,对这两个区域进行垃圾回收,经过这次回收后还存活的对象,则直接复制到To区域(如果对象的年龄已经到达老年的标准,则赋值到老年代区),通知把这些对象的年龄 + 1
清空eden、SurvivorFrom
然后,清空eden,SurvivorFrom中的对象,也即复制之后有交换,谁空谁是to
SurvivorTo和SurvivorFrom互换
最后,SurvivorTo和SurvivorFrom互换,原SurvivorTo成为下一次GC时的SurvivorFrom区,部分对象会在From和To区域中复制来复制去,如此交换15次(由JVM参数MaxTenuringThreshold决定,这个参数默认为15),最终如果还是存活,就存入老年代
设置新生代中eden和so/s1空间的比例默认
-XX:SurvivorRatio=8,Eden:S0:S1 =8:1:1
假如
-Xx:SurvivorRatio=4,Eden:sQ:s1 =4:1:1
SurvivorRatio值就是设置eden区的比例占多少,so/s1相同
-XX:+PrintGCDetails -XX:+UseSerialGC -Xms10m -Xmx10m [-XX:SurvivorRatio=8]
***********HelloGC Heap def new generation total 3072K, used 1878K [0x00000000ff600000, 0x00000000ff950000, 0x00000000ff950000) eden space 2752K, 68% used [0x00000000ff600000, 0x00000000ff7d5898, 0x00000000ff8b0000) from space 320K, 0% used [0x00000000ff8b0000, 0x00000000ff8b0000, 0x00000000ff900000) to space 320K, 0% used [0x00000000ff900000, 0x00000000ff900000, 0x00000000ff950000) tenured generation total 6848K, used 0K [0x00000000ff950000, 0x0000000100000000, 0x0000000100000000) the space 6848K, 0% used [0x00000000ff950000, 0x00000000ff950000, 0x00000000ff950200, 0x0000000100000000) Metaspace used 3265K, capacity 4496K, committed 4864K, reserved 1056768K class space used 357K, capacity 388K, committed 512K, reserved 1048576K Process finished with exit code 0
-XX:+PrintGCDetails -XX:+UseSerialGC -Xms10m -Xmx10m -XX:SurvivorRatio=4
***********HelloGC Heap def new generation total 2880K, used 1840K [0x00000000ff600000, 0x00000000ff950000, 0x00000000ff950000) eden space 2368K, 77% used [0x00000000ff600000, 0x00000000ff7cc180, 0x00000000ff850000) from space 512K, 0% used [0x00000000ff850000, 0x00000000ff850000, 0x00000000ff8d0000) to space 512K, 0% used [0x00000000ff8d0000, 0x00000000ff8d0000, 0x00000000ff950000) tenured generation total 6848K, used 0K [0x00000000ff950000, 0x0000000100000000, 0x0000000100000000) the space 6848K, 0% used [0x00000000ff950000, 0x00000000ff950000, 0x00000000ff950200, 0x0000000100000000) Metaspace used 3251K, capacity 4496K, committed 4864K, reserved 1056768K class space used 355K, capacity 388K, committed 512K, reserved 1048576K Process finished with exit code 0
70_常用基础参数NewRatio讲解
-XX:NewRatio
配置年轻代new 和老年代old 在堆结构的占比
默认:
-XX:NewRatio=2 新生代占1,老年代2,年轻代占整个堆的1/3
假如
-XX:NewRatio=4:新生代占1,老年代占4,年轻代占整个堆的1/5,NewRadio值就是设置老年代的占比,剩下的1个新生代
新生代特别小,会造成频繁的进行GC收集
默认
-Xms10m -Xmx10m -XX:+PrintGCDetails -XX:+UseSerialGC -XX:NewRatio=2
***********HelloGC Heap def new generation total 3072K, used 1809K [0x00000000ff600000, 0x00000000ff950000, 0x00000000ff950000) eden space 2752K, 65% used [0x00000000ff600000, 0x00000000ff7c4640, 0x00000000ff8b0000) from space 320K, 0% used [0x00000000ff8b0000, 0x00000000ff8b0000, 0x00000000ff900000) to space 320K, 0% used [0x00000000ff900000, 0x00000000ff900000, 0x00000000ff950000) tenured generation total 6848K, used 0K [0x00000000ff950000, 0x0000000100000000, 0x0000000100000000) the space 6848K, 0% used [0x00000000ff950000, 0x00000000ff950000, 0x00000000ff950200, 0x0000000100000000) Metaspace used 3211K, capacity 4496K, committed 4864K, reserved 1056768K class space used 350K, capacity 388K, committed 512K, reserved 1048576K Process finished with exit code 0
修改
-Xms10m -Xmx10m -XX:+PrintGCDetails -XX:+UseSerialGC -XX:NewRatio=4
[GC (Allocation Failure) [DefNew: 1660K->192K(1856K), 0.0019359 secs] 1660K->603K(10048K), 0.0026938 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] ***********HelloGC Heap def new generation total 1856K, used 275K [0x00000000ff600000, 0x00000000ff800000, 0x00000000ff800000) eden space 1664K, 5% used [0x00000000ff600000, 0x00000000ff614f00, 0x00000000ff7a0000) from space 192K, 100% used [0x00000000ff7d0000, 0x00000000ff800000, 0x00000000ff800000) to space 192K, 0% used [0x00000000ff7a0000, 0x00000000ff7a0000, 0x00000000ff7d0000) tenured generation total 8192K, used 411K [0x00000000ff800000, 0x0000000100000000, 0x0000000100000000) the space 8192K, 5% used [0x00000000ff800000, 0x00000000ff866cd8, 0x00000000ff866e00, 0x0000000100000000) Metaspace used 3230K, capacity 4496K, committed 4864K, reserved 1056768K class space used 352K, capacity 388K, committed 512K, reserved 1048576K Process finished with exit code 0
71_常用基础参数MaxTenuringThreshold讲解
-XX:MaxTenuringThreshold
设置垃圾最大年龄
查看默认进入老年代年龄:15
E:\IdeaProjects\JavaMS\second_2\src\jvm2>jinfo -flag MaxTenuringThreshold 23172 -XX:MaxTenuringThreshold=15
-XX:MaxTenuringThreshold=0:设置垃圾最大年龄。如果设置为0的话,则年轻对象不经过Survivor区,直接进入老年代。对于年老代比较多的应用,可以提高效率。如果将此值设置为一个较大的值,则年轻代对象会在Survivor区进行多次复制,这样可以增加对象再年轻代的存活时间,增加在年轻代即被回收的概念
-XX:MaxTenuringThreshold=20
Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. MaxTenuringThreshold of 20 is invalid; must be between 0 and 15
-XX:MaxTenuringThreshold=12
E:\IdeaProjects\JavaMS\second_2\src\jvm2>jinfo -flag MaxTenuringThreshold 824 -XX:MaxTenuringThreshold=12
最后
2022 10/12 16:23
p65~p71
Markdown 10630 字数 431 行数
HTML 9893 字数 228 段落