首页 > 代码库 > tomcat启动时就频繁gc和full gc

tomcat启动时就频繁gc和full gc

一个小业务,流量并不大,功能也很简单,spring framework+mybatis+quartz,一启动就看到gc的频次和full gc的频次非常高:

4.202: [Full GC 4.202: [Tenured: 0K->6653K(349568K), 0.1948910 secs] 139776K->6653K(506816K), [Perm : 26687K->26687K(26688K)], 0.1950470 secs] [Times: user=0.20 sys=0.00, real=0.19 secs] 
5.618: [GC 5.618: [ParNew: 139904K->4231K(157376K), 0.0060210 secs] 146557K->10885K(506944K), 0.0061230 secs] [Times: user=0.03 sys=0.01, real=0.01 secs] 
6.286: [GC 6.286: [ParNew: 144135K->10898K(157376K), 0.0073450 secs] 150789K->17551K(506944K), 0.0074440 secs] [Times: user=0.04 sys=0.01, real=0.01 secs] 
6.814: [GC 6.814: [ParNew: 150802K->15087K(157376K), 0.0118750 secs] 157455K->25929K(506944K), 0.0119640 secs] [Times: user=0.07 sys=0.02, real=0.01 secs] 
7.451: [Full GC 7.451: [Tenured: 10841K->25946K(349568K), 0.2066400 secs] 164311K->25946K(506944K), [Perm : 32127K->32127K(32128K)], 0.2067430 secs] [Times: user=0.23 sys=0.01, real=0.20 secs] 
8.552: [Full GC 8.553: [Tenured: 25946K->29686K(349568K), 0.2255000 secs] 55914K->29686K(506944K), [Perm : 37568K->37568K(37568K)], 0.2256070 secs]

在10s内就进行了3次full gc,而gc3次,且花的时间也较多。

jvm的启动参数是:

-server -Xms512m -Xmx1024m -Xss256m  -XX:SurvivorRatio=8 -XX:NewRatio=5 -XX:+UseParNewGC -XX:+HeapDumpOnOutOfMemoryError
-verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:/xx/gc.log

其实,仔细看gc日志不难发现,每次full gc时,tenured区和perm区的总大小和当前大小的差值:

  •    Tenured: 25946K->29686K(349568K):都不是一个数量级,所以空闲还是有丢丢的;

  •    Perm : 37568K->37568K(37568K),是相等的,这明显很有问题,内存严重不够呀。


 

最后想到设置perm的初始大小:64m:-XX:PermSize=64m

结果,真的木发生fullgc了 ,启动的时候还有gc,那是因为我的新生代设置太小了:

2.439: [GC 2.439: [ParNew: 69952K->4078K(78656K), 0.0117950 secs] 69952K->4078K(515584K), 0.0118850 secs] 
[Times: user=0.04 sys=0.02, real=0.01 secs] 4.102: [GC 4.102: [ParNew: 74030K->8704K(78656K), 0.0116460 secs] 74030K->8751K(515584K), 0.0117330 secs]
[Times: user=0.08 sys=0.00, real=0.01 secs] 4.899: [GC 4.899: [ParNew: 78656K->7203K(78656K), 0.0089280 secs] 78703K->10806K(515584K), 0.0090070 secs]
[Times: user=0.05 sys=0.01, real=0.01 secs] 5.503: [GC 5.503: [ParNew: 77155K->6532K(78656K), 0.0081250 secs] 80758K->13226K(515584K), 0.0082490 secs]
[Times: user=0.04 sys=0.01, real=0.01 secs] 5.896: [GC 5.896: [ParNew: 76484K->8704K(78656K), 0.0050150 secs] 83178K->15397K(515584K), 0.0050910 secs]
[Times: user=0.04 sys=0.00, real=0.00 secs] 6.179: [GC 6.179: [ParNew: 78656K->6929K(78656K), 0.0078420 secs] 85349K->17865K(515584K), 0.0079240 secs] [Times: user=0.03 sys=0.02, real=0.01 secs] 6.441: [GC 6.441: [ParNew: 76881K->8704K(78656K), 0.0063420 secs] 87817K->22031K(515584K), 0.0064180 secs] [Times: user=0.03 sys=0.01, real=0.01 secs] 6.693: [GC 6.693: [ParNew: 78656K->7043K(78656K), 0.0068150 secs] 91983K->22658K(515584K), 0.0068970 secs] [Times: user=0.04 sys=0.02, real=0.01 secs] 6.926: [GC 6.926: [ParNew: 76995K->7519K(78656K), 0.0087600 secs] 92610K->25735K(515584K), 0.0088390 secs] [Times: user=0.04 sys=0.03, real=0.00 secs] 7.545: [GC 7.545: [ParNew: 77471K->8203K(78656K), 0.0111190 secs] 95687K->28887K(515584K), 0.0112000 secs]

喜闻乐见呀。

将新生代区的大小设置下,gc的频率也没这么高了,-XX:NewSize=256m。