对自己使用的IDE进行jvm的调优
首先进行日志输出配置
Eclipse 修改eclipse.ini
IDEA 修改 idea.exe.vmoptions
增加打印日志的配置参数
-XX:+PrintGCTimeStamps -XX:+PrintGCDetails -verbose:gc -Xloggc:cc_gc.log |
之后启动IDE,查看cc_gc.log文件
9.818: [GC 9.818: [DefNew: 139776K->17472K(157248K), 0.1119870 secs] 139776K->18556K(506816K), 0.1122030 secs] [Times: user=0.09 sys=0.02, real=0.11 secs] 14.429: [GC 14.430: [DefNew: 157248K->17472K(157248K), 0.2153986 secs] 158332K->50602K(506816K), 0.2155784 secs] [Times: user=0.17 sys=0.03, real=0.22 secs] 21.186: [Full GC 21.187: [Tenured: 33130K->64738K(349568K), 0.3896917 secs] 182739K->64738K(506816K), [Perm : 16384K->16384K(16384K)], 0.3898639 secs] [Times: user=0.31 sys=0.03, real=0.39 secs] 28.252: [Full GC 28.252: [Tenured: 64738K->72853K(349568K), 0.3850680 secs] 113468K->72853K(506816K), [Perm : 20479K->20479K(20480K)], 0.3852719 secs] [Times: user=0.39 sys=0.00, real=0.38 secs] 30.163: [Full GC 30.163: [Tenured: 72853K->74842K(349568K), 0.4226417 secs] 104230K->74842K(506816K), [Perm : 24575K->24575K(24576K)], 0.4228340 secs] [Times: user=0.41 sys=0.02, real=0.42 secs] 32.836: [Full GC 32.836: [Tenured: 74842K->65968K(349568K), 0.5620080 secs] 124311K->65968K(506816K), [Perm : 28671K->28645K(28672K)], 0.5622182 secs] [Times: user=0.56 sys=0.00, real=0.56 secs] 37.398: [Full GC 37.398: [Tenured: 65968K->68840K(349568K), 0.4841606 secs] 134377K->68840K(506816K), [Perm : 32767K->32767K(32768K)], 0.4843980 secs] [Times: user=0.48 sys=0.00, real=0.49 secs] 44.791: [GC 44.791: [DefNew: 139776K->6749K(157248K), 0.0712144 secs] 208616K->75589K(506816K), 0.0713880 secs] [Times: user=0.06 sys=0.00, real=0.07 secs] 46.151: [Full GC 46.151: [Tenured: 68840K->87682K(349568K), 0.4460944 secs] 151814K->87682K(506816K), [Perm : 36857K->36857K(36864K)], 0.4462617 secs] [Times: user=0.44 sys=0.00, real=0.45 secs] 47.559: [Full GC 47.559: [Tenured: 87682K->90045K(349568K), 0.3395155 secs] 180158K->90045K(506816K), [Perm : 40959K->40959K(40960K)], 0.3396347 secs] [Times: user=0.34 sys=0.00, real=0.34 secs] 49.159: [Full GC 49.159: [Tenured: 90045K->85954K(349568K), 0.4153938 secs] 214420K->85954K(506816K), [Perm : 45055K->45015K(45056K)], 0.4155009 secs] [Times: user=0.41 sys=0.00, real=0.41 secs] 50.697: [GC 50.697: [DefNew: 139776K->2972K(157248K), 0.0180432 secs] 225730K->88927K(506816K), 0.0181231 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 51.128: [Full GC 51.128: [Tenured: 85954K->89721K(349568K), 0.2832516 secs] 143886K->89721K(506816K), [Perm : 49151K->49151K(49152K)], 0.2833462 secs] [Times: user=0.28 sys=0.00, real=0.28 secs] 51.979: [GC 51.979: [DefNew: 139776K->2718K(157248K), 0.0172579 secs] 229497K->92440K(506816K), 0.0173547 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 52.525: [GC 52.525: [DefNew: 142494K->5601K(157248K), 0.0199793 secs] 232216K->95322K(506816K), 0.0200686 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 54.030: [Full GC 54.030: [Tenured: 89721K->100007K(349568K), 0.7484057 secs] 211409K->100007K(506816K), [Perm : 53248K->53248K(53248K)], 0.7486056 secs] [Times: user=0.75 sys=0.00, real=0.75 secs] 56.711: [GC 56.711: [DefNew: 139776K->3470K(157248K), 0.0305333 secs] 239783K->103478K(506816K), 0.0306645 secs] [Times: user=0.03 sys=0.00, real=0.03 secs] 57.267: [GC 57.267: [DefNew: 143246K->5452K(157248K), 0.0320053 secs] 243254K->105460K(506816K), 0.0321151 secs] [Times: user=0.03 sys=0.00, real=0.03 secs] 58.240: [GC 58.241: [DefNew: 145228K->4416K(157248K), 0.0507719 secs] 245236K->104424K(506816K), 0.0509615 secs] |
你会看到很多次FULL GC和更多次的MINOR GC
JVM的fullgc 实际上会造成你的应用暂停,所以会给你一种卡死的假象。
而MINOR GC 会在执行你的应用的同时执行垃圾收集,所以对你应用的影响较小,但同样效率也就没有fullgc那样快那样彻底。
而造成fullgc的原因,有可能是老年代(Tenured)满了,持久代(Perm)满了,或者显式调用system.gc()
这里可以看出啊是perm满了。至于什么是老年代新生代持久代,要开始从虚拟机的实现开始讲起了…有点长,还是看下<<深入理解java虚拟机>>吧,这个写的深入浅出。
既然是持久代满了,很简单增加下持久代的容量就好了。所以增加一下参数
#持久代的大小和最大值一样,避免重新分配 -XX:PermSize=512m #持久代最大值 -XX:MaxPermSize=512m |
重启IDE,查看打印日志,你会发现full gc的日志变少了,只是偶尔出现,但minor gc的还是一样的多,怎样去除该死的gc呢?
13.286: [GC 13.286: [DefNew: 279616K->34944K(314560K), 0.2190107 secs] 279616K->48613K(1013632K), 0.2192137 secs] [Times: user=0.17 sys=0.05, real=0.22 secs] 48.551: [GC 48.551: [DefNew: 314560K->28346K(314560K), 0.6644487 secs] 328229K->70837K(1013632K), 0.6647003 secs] [Times: user=0.36 sys=0.23, real=0.66 secs] 69.702: [GC 69.702: [DefNew: 307962K->19605K(314560K), 0.3614997 secs] 350453K->88787K(1013632K), 0.3617434 secs] [Times: user=0.36 sys=0.00, real=0.36 secs] 78.648: [GC 78.648: [DefNew: 299221K->10228K(314560K), 0.2022102 secs] 368403K->97761K(1013632K), 0.2023637 secs] [Times: user=0.19 sys=0.00, real=0.20 secs] 85.375: [GC 85.375: [DefNew: 289844K->12827K(314560K), 0.1320774 secs] 377377K->100360K(1013632K), 0.1322965 secs] [Times: user=0.12 sys=0.00, real=0.13 secs] 96.130: [GC 96.130: [DefNew: 292443K->23240K(314560K), 0.1398189 secs] 379976K->110773K(1013632K), 0.1399750 secs] [Times: user=0.14 sys=0.00, real=0.14 secs] 101.912: [GC 101.912: [DefNew: 302856K->18367K(314560K), 0.1574988 secs] 390389K->115683K(1013632K), 0.1576590 secs] [Times: user=0.16 sys=0.00, real=0.16 secs] 103.306: [Full GC (System) 103.306: [Tenured: 97315K->113265K(699072K), 0.9300948 secs] 167525K->113265K(1013632K), [Perm : 64945K->64945K(524288K)], 0.9303121 secs] [Times: user=0.92 sys=0.02, real=0.93 secs] 110.229: [GC 110.229: [DefNew: 279616K->5932K(314560K), 0.0517919 secs] 392881K->119198K(1013632K), 0.0519378 secs] [Times: user=0.05 sys=0.00, real=0.05 secs] 111.312: [GC 111.312: [DefNew: 285548K->5365K(314560K), 0.0235290 secs] 398814K->118631K(1013632K), 0.0236321 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 120.657: [GC 120.657: [DefNew: 284981K->8851K(314560K), 0.0674546 secs] 398247K->122116K(1013632K), 0.0676027 secs] |
增加年轻代的大小就好了,对应参数如下
#初始堆大小 -Xms1024m #最大堆大小 -Xmx1024m #年轻代大小 -Xmn512m |
这样初始配置就好,如果是eclipse的话,启动时间上你会有一些明显的感觉,但IDEA除了日志上有比较明显的变化,实际上感觉不到很大的区别。也就是说…你懂的
当然,上面说的是初始化,也就是java对象生的事情,而java对象死的事情呢?就有赖于java的新生代,老年代回收算法和回收实现的问题了。有兴趣的可以看下之前推荐的那本书。
参考链接中也有相关参数的具体解释
Ps:该优化配置只是对启动ide的一些优化,如果开发使用可以再修改成-server以及制定垃圾回收机制等参数
推荐链接: