腾讯云ECS,1核1G,java程序定时爬取某些网站数据并展示,未使用数据库。
JVM运行参数:-server -Xms256m -Xmx256m -Xmn192m -XX:NewRatio=1 -XX:SurvivorRatio=5 -XX:TargetSurvivorRatio=90;运行几十分钟后如下图,进程占用总内存约355MB。
root 11002 1.1 36.1 2338368 364164 ? Sl 12:21 0:51 java -jar -server -Xms256m -Xmx256m -Xmn192m -XX:NewRatio=1 -XX:SurvivorRatio=5 -XX:TargetSurvivorRatio=90 /home/kenick/smartFinancial-manager/smartfinancialmanager.jar --jasypt.encryptor.password=kenick@2020 --spring.profiles.active=cloud
堆内存分配,新生代内存大小为192MB,老年代为64MB,新生代占堆比例为75%(G1垃圾回收器时参考)。
using thread-local object allocation.
Mark Sweep Compact GC
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 268435456 (256.0MB)
NewSize = 201326592 (192.0MB)
MaxNewSize = 201326592 (192.0MB)
OldSize = 67108864 (64.0MB)
NewRatio = 1
SurvivorRatio = 5
MetaspaceSize = 21807104 (20.796875MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
New Generation (Eden + 1 Survivor Space):
capacity = 172621824 (164.625MB)
used = 15380448 (14.667938232421875MB)
free = 157241376 (149.95706176757812MB)
8.909909328730068% used
Eden Space:
capacity = 143917056 (137.25MB)
used = 1787728 (1.7049102783203125MB)
free = 142129328 (135.5450897216797MB)
1.242193281107696% used
From Space:
capacity = 28704768 (27.375MB)
used = 13592720 (12.963027954101562MB)
free = 15112048 (14.411972045898438MB)
47.353526772973744% used
To Space:
capacity = 28704768 (27.375MB)
used = 0 (0.0MB)
free = 28704768 (27.375MB)
0.0% used
tenured generation:
capacity = 67108864 (64.0MB)
used = 14206256 (13.548141479492188MB)
free = 52902608 (50.45185852050781MB)
21.168971061706543% used
17304 interned Strings occupying 1441384 bytes.
堆内存增长如下图,Eden区域由69.04增长到98.07,增长了29.03,花费时间44秒,每秒增长0.659,达到100将花费151.74秒,一次YGC,YGCT增长了21ms,一个月YGCT将达到358.71秒;FGC未记录,按照一天2次,0.156秒来算,一个月FGCT约4.68秒;一个月总停顿时间将达到363.39秒。
[root@VM_0_12_centos logs]# jstat -gcutil 18216 4000
S0 S1 E O M CCS YGC YGCT FGC FGCT GCT
0.00 0.00 69.04 22.45 93.48 88.95 3 0.091 2 0.156 0.248
0.00 0.00 71.70 22.45 93.48 88.95 3 0.091 2 0.156 0.248
0.00 0.00 74.37 22.45 93.48 88.95 3 0.091 2 0.156 0.248
0.00 0.00 77.03 22.45 93.48 88.95 3 0.091 2 0.156 0.248
0.00 0.00 79.71 22.45 93.48 88.95 3 0.091 2 0.156 0.248
0.00 0.00 81.99 22.45 93.48 88.95 3 0.091 2 0.156 0.248
0.00 0.00 84.65 22.45 93.48 88.95 3 0.091 2 0.156 0.248
0.00 0.00 87.81 22.45 93.48 88.95 3 0.091 2 0.156 0.248
0.00 0.00 90.48 22.45 93.48 88.95 3 0.091 2 0.156 0.248
0.00 0.00 93.14 22.45 93.48 88.95 3 0.091 2 0.156 0.248
0.00 0.00 95.79 22.45 93.48 88.95 3 0.091 2 0.156 0.248
0.00 0.00 98.07 22.45 93.48 88.95 3 0.091 2 0.156 0.248
60.48 0.00 2.43 22.45 93.78 89.68 4 0.112 2 0.156 0.268
60.48 0.00 5.24 22.45 93.78 89.68 4 0.112 2 0.156 0.268
60.48 0.00 8.05 22.45 93.78 89.68 4 0.112 2 0.156 0.268
60.48 0.00 9.92 22.45 93.78 89.68 4 0.112 2 0.156 0.268
60.48 0.00 12.73 22.45 93.78 89.68 4 0.112 2 0.156 0.268
60.48 0.00 19.30 22.45 93.78 89.68 4 0.112 2 0.156 0.268
60.48 0.00 22.11 22.45 93.78 89.68 4 0.112 2 0.156 0.268
60.48 0.00 24.92 22.45 93.78 89.68 4 0.112 2 0.156 0.268
60.48 0.00 27.73 22.45 93.78 89.68 4 0.112 2 0.156 0.268
60.48 0.00 29.60 22.45 93.78 89.68 4 0.112 2 0.156 0.268
60.48 0.00 32.41 22.45 93.78 89.68 4 0.112 2 0.156 0.268
JVM运行参数:-server -Xms256m -Xmx256m -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:G1NewSizePercent=75 -XX:G1MaxNewSizePercent=90;运行几十分钟后如下图,进程占用总内存约405MB。
root 30701 1.9 41.1 2378172 415180 pts/7 Sl 11:09 0:29 java -jar -server -Xms256m -Xmx256m -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:G1NewSizePercent=75 -XX:G1MaxNewSizePercent=90 /home/kenick/smartFinancial-manager/smartfinancialmanager.jar --jasypt.encryptor.password=kenick@2020 --spring.profiles.active=cloud
堆内存分配,新生代内存大小为187+15=192MB,老年代为54MB。
JVM version is 25.232-b09
using thread-local object allocation.
Garbage-First (G1) GC with 1 thread(s)
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 268435456 (256.0MB)
NewSize = 1363144 (1.2999954223632812MB)
MaxNewSize = 241172480 (230.0MB)
OldSize = 5452592 (5.1999969482421875MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 21807104 (20.796875MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 1048576 (1.0MB)
Heap Usage:
G1 Heap:
regions = 256
capacity = 268435456 (256.0MB)
used = 56098808 (53.49999237060547MB)
free = 212336648 (202.50000762939453MB)
20.89843451976776% used
G1 Young Generation:
Eden Space:
regions = 39
capacity = 196083712 (187.0MB)
used = 40894464 (39.0MB)
free = 155189248 (148.0MB)
20.855614973262032% used
Survivor Space:
regions = 15
capacity = 15728640 (15.0MB)
used = 15728640 (15.0MB)
free = 0 (0.0MB)
100.0% used
G1 Old Generation:
regions = 0
capacity = 56623104 (54.0MB)
used = 0 (0.0MB)
free = 56623104 (54.0MB)
0.0% used
14777 interned Strings occupying 1444424 bytes.
堆内存增长如下图,Eden区域由69.52增长到92.51,增长了22.99,花费时间48秒,每秒增长0.478,达到100将花费209.20秒,一次YGC,YGCT增长了60ms,一个月YGCT将达到743.40秒;FGC为0;一个月总停顿时间将达到743.40秒。
[root@VM_0_12_centos ~]# jstat -gcutil 30701 4000
S0 S1 E O M CCS YGC YGCT FGC FGCT GCT
0.00 100.00 69.52 0.00 93.26 88.82 4 0.230 0 0.000 0.230
0.00 100.00 71.66 0.00 93.26 88.82 4 0.230 0 0.000 0.230
0.00 100.00 73.80 0.00 93.26 88.82 4 0.230 0 0.000 0.230
0.00 100.00 75.40 0.00 93.26 88.82 4 0.230 0 0.000 0.230
0.00 100.00 77.54 0.00 93.26 88.82 4 0.230 0 0.000 0.230
0.00 100.00 79.14 0.00 93.26 88.82 4 0.230 0 0.000 0.230
0.00 100.00 81.28 0.00 93.26 88.82 4 0.230 0 0.000 0.230
0.00 100.00 83.42 0.00 93.26 88.82 4 0.230 0 0.000 0.230
0.00 100.00 85.03 0.00 93.26 88.82 4 0.230 0 0.000 0.230
0.00 100.00 87.17 0.00 93.26 88.82 4 0.230 0 0.000 0.230
0.00 100.00 88.77 0.00 93.26 88.82 4 0.230 0 0.000 0.230
0.00 100.00 90.91 0.00 93.26 88.82 4 0.230 0 0.000 0.230
0.00 100.00 92.51 0.00 93.26 88.82 4 0.230 0 0.000 0.230
0.00 100.00 0.56 17.16 93.69 88.58 5 0.290 0 0.000 0.290
0.00 100.00 2.25 17.16 93.69 88.58 5 0.290 0 0.000 0.290
0.00 100.00 4.49 17.16 93.69 88.58 5 0.290 0 0.000 0.290
0.00 100.00 6.18 17.16 93.69 88.58 5 0.290 0 0.000 0.290
当前环境下,使用G1垃圾回收器的好处是发生FGC的频率比CMS频率低很多(老年代内存差不多),发生YGC的频率比CMS略低,jvm整体最大停顿时间有保障(不超过200ms);缺点是:YGC花费的时间大幅增加,单次YGC需花费60ms,而CMS只需要21ms。
从CMS角度考虑,优点:单次YGC花费时间更小,是G1的一半左右,长期节省累积的停顿时间很客观;可灵活调整堆中内存分配。缺点:FGC频率比G1高一些,不能保障最大停顿时间,需要根据业务情况配置堆内存分配才能达到最好效果。
关于G1的新生代GC说明如下:
新生代GC的主要工作是回收eden区和survivor区。一旦eden区被占满,新生代GC就会启动。新生代GC收集前后的堆数据如图5.6所示,其中E表示eden区,S表示survivor区,O表示老年代。可以看到,新生代GC只处理eden和survivor区,回收后,所有的eden区都应该被清空,而survivor区会被收集一部分数据,但是应该至少仍然存在一个survivor区,类比其他的新生代收集器,这一点似乎并没有太大变化。另一个重要的变化是老年代的区域增多,因为部分survivor区或者eden区的对象可能会晋升到老年代。
什么时候该用CMS,什么时候该用G1呢?个人建议:JVM大内存(6GB以上),使用G1,可保障停顿时间,不需要详细划分老年代和新生代比例;JVM小内存(6GB以下),使用CMS。