一、eclipse 启动优化
在日常的开发中发现eclipse 启动的时候非常慢,并且在实际的开发中也非常卡,所以尝试着优化一下。现在eclipse 是运行在jdk1.7上。
首先我们可以看到eclipse.ini配置文件中配置如下:
从配置中可以看到 初始堆大小为256m,最大堆大小为1024m,最大永久代的大小为256m。
-startup
plugins/org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.300.v20150602-1417
-product
org.eclipse.epp.package.jee.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
--launcher.appendVmargs
-vmargs
-Dosgi.requiredJavaVersion=1.7
-Xms256mm
-Xmx1024m
我们先配置上GC日志并输出到文件中
-XX:+PrintGCDetails
-Xloggc:F:/eclipse_gc.log
我们启动eclipse可以看到GC日志,通过日志我们可以看到jvm的相关的一些参数和GC的日志。我们可以看出在eclipse启动阶段进行了一些针对年轻代的垃圾回收,并没有老年代的垃圾回收。说明整个内存分配的足够大了,不至于会出现full GC
Java HotSpot(TM) 64-Bit Server VM (24.80-b11) for windows-amd64 JRE (1.7.0_80-b15), built on Apr 10 2015 11:26:34 by "java_re" with unknown MS VC++:1600
Memory: 4k page, physical 8324052k(4176904k free), swap 13797264k(7016024k free)
CommandLine flags: -XX:InitialHeapSize=268435456 -XX:MaxHeapSize=1073741824 -XX:MaxPermSize=268435456 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
0.685: [GC [PSYoungGen: 66048K->10744K(76800K)] 66048K->25787K(251904K), 0.0217812 secs] [Times: user=0.06 sys=0.00, real=0.02 secs]
1.155: [GC [PSYoungGen: 76792K->10742K(142848K)] 91835K->42832K(317952K), 0.0157041 secs] [Times: user=0.05 sys=0.02, real=0.02 secs]
2.056: [GC [PSYoungGen: 142838K->10726K(142848K)] 174928K->49489K(317952K), 0.0142720 secs] [Times: user=0.05 sys=0.02, real=0.01 secs]
4.147: [GC [PSYoungGen: 142822K->10722K(274944K)] 181585K->81084K(450048K), 0.0326797 secs] [Times: user=0.11 sys=0.00, real=0.03 secs]
7.994: [GC [PSYoungGen: 274914K->10742K(274944K)] 345276K->125392K(450048K), 0.0450933 secs] [Times: user=0.16 sys=0.03, real=0.04 secs]
10.609: [GC [PSYoungGen: 274934K->59377K(266752K)] 389826K->176369K(441856K), 0.0634943 secs] [Times: user=0.14 sys=0.06, real=0.06 secs]
499.060: [GC [PSYoungGen: 213149K->53131K(260608K)] 330142K->171159K(435712K), 0.0402832 secs] [Times: user=0.13 sys=0.00, real=0.04 secs]
但是在eclipse自动运行一段时间之后,通过GC日志发现,Eclipse 会周期性的进行full GC ,并且基本没有回收垃圾,让我困惑了很久。通过网上查询可知,eclipse会运行System.gc(),所以我们可以通过在eclipse.ini文件中添加配置 -XX:+DisableExplicitGC,来避免这种情况。
Java HotSpot(TM) 64-Bit Server VM (24.80-b11) for windows-amd64 JRE (1.7.0_80-b15), built on Apr 10 2015 11:26:34 by "java_re" with unknown MS VC++:1600
Memory: 4k page, physical 8324052k(4176904k free), swap 13797264k(7016024k free)
CommandLine flags: -XX:InitialHeapSize=268435456 -XX:MaxHeapSize=1073741824 -XX:MaxPermSize=268435456 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
0.685: [GC [PSYoungGen: 66048K->10744K(76800K)] 66048K->25787K(251904K), 0.0217812 secs] [Times: user=0.06 sys=0.00, real=0.02 secs]
1.155: [GC [PSYoungGen: 76792K->10742K(142848K)] 91835K->42832K(317952K), 0.0157041 secs] [Times: user=0.05 sys=0.02, real=0.02 secs]
2.056: [GC [PSYoungGen: 142838K->10726K(142848K)] 174928K->49489K(317952K), 0.0142720 secs] [Times: user=0.05 sys=0.02, real=0.01 secs]
4.147: [GC [PSYoungGen: 142822K->10722K(274944K)] 181585K->81084K(450048K), 0.0326797 secs] [Times: user=0.11 sys=0.00, real=0.03 secs]
7.994: [GC [PSYoungGen: 274914K->10742K(274944K)] 345276K->125392K(450048K), 0.0450933 secs] [Times: user=0.16 sys=0.03, real=0.04 secs]
10.609: [GC [PSYoungGen: 274934K->59377K(266752K)] 389826K->176369K(441856K), 0.0634943 secs] [Times: user=0.14 sys=0.06, real=0.06 secs]
499.060: [GC [PSYoungGen: 213149K->53131K(260608K)] 330142K->171159K(435712K), 0.0402832 secs] [Times: user=0.13 sys=0.00, real=0.04 secs]
499.100: [Full GC [PSYoungGen: 53131K->0K(260608K)] [ParOldGen: 118027K->150703K(175104K)] 171159K->150703K(435712K) [PSPermGen: 82684K->82633K(165376K)], 0.5155009 secs] [Times: user=1.20 sys=0.08, real=0.52 secs]
559.079: [GC [PSYoungGen: 14660K->192K(268288K)] 165364K->150895K(443392K), 0.0048175 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
559.084: [Full GC [PSYoungGen: 192K->0K(268288K)] [ParOldGen: 150703K->102396K(175104K)] 150895K->102396K(443392K) [PSPermGen: 82639K->82639K(186368K)], 0.2584187 secs] [Times: user=0.78 sys=0.00, real=0.26 secs]
619.093: [GC [PSYoungGen: 19065K->608K(195072K)] 121462K->103004K(370176K), 0.0045706 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
619.097: [Full GC [PSYoungGen: 608K->0K(195072K)] [ParOldGen: 102396K->102561K(175104K)] 103004K->102561K(370176K) [PSPermGen: 82647K->82646K(179712K)], 0.2048456 secs] [Times: user=0.61 sys=0.00, real=0.20 secs]
679.094: [GC [PSYoungGen: 13436K->192K(268288K)] 115997K->102753K(443392K), 0.0047720 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
679.099: [Full GC [PSYoungGen: 192K->0K(268288K)] [ParOldGen: 102561K->102388K(175104K)] 102753K->102388K(443392K) [PSPermGen: 82647K->82647K(171008K)], 0.1635951 secs] [Times: user=0.45 sys=0.00, real=0.16 secs]
739.107: [GC [PSYoungGen: 12725K->192K(269312K)] 115114K->102580K(444416K), 0.0034056 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
739.111: [Full GC [PSYoungGen: 192K->0K(269312K)] [ParOldGen: 102388K->102388K(175104K)] 102580K->102388K(444416K) [PSPermGen: 82647K->82647K(163328K)], 0.1415162 secs] [Times: user=0.42 sys=0.00, real=0.14 secs]
799.125: [GC [PSYoungGen: 12901K->192K(270848K)] 115289K->102580K(445952K), 0.0039268 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
799.129: [Full GC [PSYoungGen: 192K->0K(270848K)] [ParOldGen: 102388K->102388K(175104K)] 102580K->102388K(445952K) [PSPermGen: 82647K->82647K(155136K)], 0.1941489 secs] [Times: user=0.50 sys=0.00, real=0.19 secs]
通过jvisual对eclipse的gc监控来看,也可以对eclipse在启动的时候进行监控,通过监控界面我 们可以看到内存的分布情况,jvm内存中各个区域的最大大小、当前大小和当前实际使用大小,也可以知道GC了多少次,耗费多少时间。
下面是我结合自己电脑内存大小和实际情况重新修改的配置。
-startup
plugins/org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.300.v20150602-1417
-product
org.eclipse.epp.package.jee.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
--launcher.appendVmargs
-vmargs
-Dosgi.requiredJavaVersion=1.7
-Xms1024m
-Xmx1024m
-XX:+PrintGCDetails
-Xloggc:F:/eclipse_gc.log
-XX:PermSize=256m
-XX:MaxPermSize=256m
-XX:+DisableExplicitGC
再次启动GC日志如下:比上面的GC次数少了很多
Java HotSpot(TM) 64-Bit Server VM (24.80-b11) for windows-amd64 JRE (1.7.0_80-b15), built on Apr 10 2015 11:26:34 by "java_re" with unknown MS VC++:1600
Memory: 4k page, physical 8324052k(4128456k free), swap 13797264k(6847044k free)
CommandLine flags: -XX:+DisableExplicitGC -XX:InitialHeapSize=1073741824 -XX:MaxHeapSize=1073741824 -XX:MaxPermSize=268435456 -XX:PermSize=268435456 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
2.088: [GC [PSYoungGen: 262656K->43506K(306176K)] 262656K->48011K(1005568K), 0.0366001 secs] [Times: user=0.05 sys=0.03, real=0.04 secs]
6.788: [GC [PSYoungGen: 306162K->43493K(306176K)] 310667K->88544K(1005568K), 0.0615602 secs] [Times: user=0.19 sys=0.02, real=0.06 secs]
9.313: [GC [PSYoungGen: 306149K->43503K(306176K)] 351200K->121587K(1005568K), 0.0654575 secs] [Times: user=0.25 sys=0.00, real=0.07 secs]
147.957: [GC [PSYoungGen: 306159K->43510K(306176K)] 384243K->167854K(1005568K), 0.0762280 secs] [Times: user=0.20 sys=0.05, real=0.08 secs]
jvisual的监控图如下:
二、tomcat运行内存优化
eclipse中设置tomcat的jvm运行参数的位置是点击run下面的这个图标的倒三角箭头,run configurations
启动后的GC日志如下:
Java HotSpot(TM) 64-Bit Server VM (24.80-b11) for windows-amd64 JRE (1.7.0_80-b15), built on Apr 10 2015 11:26:34 by "java_re" with unknown MS VC++:1600
Memory: 4k page, physical 8324052k(3165964k free), swap 13797264k(5314404k free)
CommandLine flags: -XX:InitialHeapSize=133184832 -XX:MaxHeapSize=2130957312 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
0.854: [GC [PSYoungGen: 33280K->5112K(38400K)] 33280K->8527K(124928K), 0.0070140 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
1.203: [GC [PSYoungGen: 38392K->5116K(38400K)] 41807K->17582K(124928K), 0.0100325 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
1.738: [GC [PSYoungGen: 38396K->5100K(38400K)] 50862K->21362K(124928K), 0.0080278 secs] [Times: user=0.06 sys=0.00, real=0.01 secs]
2.020: [GC [PSYoungGen: 38380K->5113K(71680K)] 54642K->26435K(158208K), 0.0100652 secs] [Times: user=0.06 sys=0.00, real=0.01 secs]
2.471: [GC [PSYoungGen: 71673K->5116K(71680K)] 92995K->38583K(158208K), 0.0137861 secs] [Times: user=0.06 sys=0.00, real=0.01 secs]
2.878: [GC [PSYoungGen: 71676K->18419K(148480K)] 105143K->52802K(235008K), 0.0155807 secs] [Times: user=0.06 sys=0.00, real=0.02 secs]
3.620: [GC [PSYoungGen: 148467K->21483K(151552K)] 182850K->75619K(238080K), 0.0292769 secs] [Times: user=0.06 sys=0.00, real=0.03 secs]
4.268: [GC [PSYoungGen: 151531K->34292K(282112K)] 205667K->104070K(368640K), 0.0325620 secs] [Times: user=0.06 sys=0.02, real=0.03 secs]
4.301: [Full GC [PSYoungGen: 34292K->0K(282112K)] [ParOldGen: 69778K->49194K(116736K)] 104070K->49194K(398848K) [PSPermGen: 14040K->14036K(28160K)], 0.0916935 secs] [Times: user=0.28 sys=0.00, real=0.09 secs]
5.737: [GC [PSYoungGen: 247808K->46378K(294400K)] 297002K->95572K(411136K), 0.0412079 secs] [Times: user=0.03 sys=0.02, real=0.04 secs]
13.665: [GC [PSYoungGen: 294186K->37890K(379392K)] 343380K->87084K(496128K), 0.0315164 secs] [Times: user=0.05 sys=0.02, real=0.03 secs]
14.976: [GC [PSYoungGen: 368130K->8356K(386560K)] 417324K->87911K(503296K), 0.0245341 secs] [Times: user=0.13 sys=0.00, real=0.03 secs]
15.711: [GC [PSYoungGen: 338596K->6576K(480256K)] 418151K->92551K(596992K), 0.0098137 secs] [Times: user=0.05 sys=0.01, real=0.01 secs]
16.597: [GC [PSYoungGen: 437168K->8752K(486400K)] 523143K->100621K(603136K), 0.0117773 secs] [Times: user=0.03 sys=0.02, real=0.01 secs]
18.751: [GC [PSYoungGen: 439344K->6496K(582144K)] 531213K->106467K(698880K), 0.0121769 secs] [Times: user=0.05 sys=0.02, real=0.01 secs]
18.763: [Full GC [PSYoungGen: 6496K->0K(582144K)] [ParOldGen: 99971K->64139K(157696K)] 106467K->64139K(739840K) [PSPermGen: 37898K->37887K(64512K)], 0.1841780 secs] [Times: user=0.50 sys=0.00, real=0.18 secs]
23.204: [GC [PSYoungGen: 542208K->23206K(595968K)] 606347K->87345K(753664K), 0.0260487 secs] [Times: user=0.08 sys=0.00, real=0.03 secs]
57.170: [GC [PSYoungGen: 565414K->31881K(639488K)] 629553K->96020K(797184K), 0.0229753 secs] [Times: user=0.05 sys=0.02, real=0.02 secs]
101.726: [GC [PSYoungGen: 619657K->46073K(633856K)] 683796K->110212K(791552K), 0.0309377 secs] [Times: user=0.09 sys=0.03, real=0.03 secs]
136.288: [GC [PSYoungGen: 633849K->59385K(614400K)] 697988K->125132K(772096K), 0.0379309 secs] [Times: user=0.14 sys=0.02, real=0.04 secs]
170.535: [GC [PSYoungGen: 614393K->55066K(610304K)] 680140K->139236K(768000K), 0.0307549 secs] [Times: user=0.13 sys=0.00, real=0.03 secs]
215.054: [GC [PSYoungGen: 610074K->69343K(598016K)] 694244K->153513K(755712K), 0.0271087 secs] [Times: user=0.13 sys=0.00, real=0.03 secs]
249.152: [GC [PSYoungGen: 591583K->82698K(582656K)] 675753K->166868K(740352K), 0.0341814 secs] [Times: user=0.13 sys=0.00, real=0.03 secs]
283.036: [GC [PSYoungGen: 582410K->89066K(556032K)] 666580K->179644K(713728K), 0.0381066 secs] [Times: user=0.19 sys=0.00, real=0.04 secs]
306.681: [GC [PSYoungGen: 556010K->73376K(520704K)] 646588K->192071K(678400K), 0.0397264 secs] [Times: user=0.17 sys=0.02, real=0.04 secs]
340.248: [GC [PSYoungGen: 520352K->84896K(540160K)] 639047K->203591K(697856K), 0.0349724 secs] [Times: user=0.13 sys=0.00, real=0.03 secs]
372.567: [GC [PSYoungGen: 512928K->95776K(506368K)] 631623K->214471K(664064K), 0.0387908 secs] [Times: user=0.19 sys=0.00, real=0.04 secs]
396.682: [GC [PSYoungGen: 505888K->105952K(526336K)] 624583K->224647K(684032K), 0.0404028 secs] [Times: user=0.13 sys=0.00, real=0.04 secs]
419.774: [GC [PSYoungGen: 498656K->116000K(492544K)] 617351K->234695K(650240K), 0.0424934 secs] [Times: user=0.19 sys=0.00, real=0.04 secs]
442.770: [GC [PSYoungGen: 492320K->125664K(510976K)] 611015K->244359K(668672K), 0.0470037 secs] [Times: user=0.19 sys=0.00, real=0.05 secs]
465.603: [GC [PSYoungGen: 479968K->134656K(474624K)] 598663K->253351K(632320K), 0.0541681 secs] [Times: user=0.25 sys=0.00, real=0.05 secs]
488.320: [GC [PSYoungGen: 474624K->143424K(484352K)] 593319K->262119K(642048K), 0.0555928 secs] [Times: user=0.25 sys=0.00, real=0.06 secs]
510.730: [GC [PSYoungGen: 444480K->151168K(440832K)] 563175K->269863K(598528K), 0.0581312 secs] [Times: user=0.19 sys=0.00, real=0.06 secs]
533.059: [GC [PSYoungGen: 440448K->158624K(458240K)] 559143K->277319K(615936K), 0.0620442 secs] [Times: user=0.25 sys=0.00, real=0.06 secs]
545.033: [GC [PSYoungGen: 406432K->151584K(390656K)] 525127K->283871K(548352K), 0.0615471 secs] [Times: user=0.25 sys=0.00, real=0.06 secs]
566.965: [GC [PSYoungGen: 390176K->143776K(457728K)] 522463K->290263K(615424K), 0.0618922 secs] [Times: user=0.25 sys=0.00, real=0.06 secs]
567.027: [Full GC [PSYoungGen: 143776K->0K(457728K)] [ParOldGen: 146487K->79927K(210432K)] 290263K->79927K(668160K) [PSPermGen: 47253K->47238K(74752K)], 0.2267754 secs] [Times: user=0.61 sys=0.00, real=0.23 secs]
579.033: [GC [PSYoungGen: 229376K->5952K(460800K)] 309303K->85879K(671232K), 0.0046710 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
600.809: [GC [PSYoungGen: 235328K->11840K(462848K)] 315255K->91767K(673280K), 0.0075756 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
612.608: [GC [PSYoungGen: 243264K->17728K(462848K)] 323191K->97655K(673280K), 0.0095924 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
jvisual GC 监控图如下:
从GC日志中发现,很多次GC都是由于jvm内存各个区大小变动引起的,所以将各个区的初始化大小和最大大小设置相同,这样就能减少GC次数。
配置如下:
-XX:+PrintGCDetails
-Xloggc:F:/tomcat_gc.log
-Xms2048m
-Xmx2048m
-Xmn800m
-XX:PermSize=128m
-XX:MaxPermSize=128
GC日志如下:
Java HotSpot(TM) 64-Bit Server VM (24.80-b11) for windows-amd64 JRE (1.7.0_80-b15), built on Apr 10 2015 11:26:34 by "java_re" with unknown MS VC++:1600
Memory: 4k page, physical 8324052k(3399564k free), swap 13797264k(5273436k free)
CommandLine flags: -XX:InitialHeapSize=2147483648 -XX:MaxHeapSize=2147483648 -XX:MaxNewSize=838860800 -XX:MaxPermSize=128 -XX:NewSize=838860800 -XX:PermSize=134217728 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
8.233: [GC [PSYoungGen: 614400K->74979K(716800K)] 614400K->75051K(1994752K), 0.0613569 secs] [Times: user=0.09 sys=0.02, real=0.06 secs]
13.107: [GC [PSYoungGen: 689379K->80386K(716800K)] 689451K->80466K(1994752K), 0.0662119 secs] [Times: user=0.13 sys=0.03, real=0.07 secs]
15.196: [GC [PSYoungGen: 694786K->51138K(716800K)] 694866K->51226K(1994752K), 0.0289944 secs] [Times: user=0.08 sys=0.00, real=0.03 secs]
17.021: [GC [PSYoungGen: 665538K->59830K(716800K)] 665626K->59926K(1994752K), 0.0295720 secs] [Times: user=0.06 sys=0.00, real=0.03 secs]
20.149: [GC [PSYoungGen: 674230K->72352K(716800K)] 674326K->72456K(1994752K), 0.0734533 secs] [Times: user=0.19 sys=0.00, real=0.07 secs]
44.123: [GC [PSYoungGen: 686752K->84374K(719872K)] 686856K->84486K(1997824K), 0.0455815 secs] [Times: user=0.19 sys=0.00, real=0.05 secs]
78.463: [GC [PSYoungGen: 717206K->44244K(677376K)] 717318K->80965K(1955328K), 0.0471060 secs] [Times: user=0.17 sys=0.00, real=0.05 secs]
122.780: [GC [PSYoungGen: 677076K->23316K(713216K)] 713797K->81323K(1991168K), 0.0260480 secs] [Times: user=0.09 sys=0.03, real=0.03 secs]
156.981: [GC [PSYoungGen: 640788K->608K(618496K)] 698795K->81511K(1896448K), 0.0179804 secs] [Times: user=0.05 sys=0.00, real=0.02 secs]
191.125: [GC [PSYoungGen: 618080K->480K(702976K)] 698983K->81551K(1980928K), 0.0040016 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
225.338: [GC [PSYoungGen: 595424K->416K(595456K)] 676495K->81631K(1873408K), 0.0028447 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
259.398: [GC [PSYoungGen: 595360K->480K(704000K)] 676575K->81767K(1981952K), 0.0030772 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
293.426: [GC [PSYoungGen: 590304K->480K(704512K)] 671591K->81847K(1982464K), 0.0028472 secs] [Times: user=0.06 sys=0.00, real=0.00 secs]
327.418: [GC [PSYoungGen: 590304K->448K(707072K)] 671671K->81887K(1985024K), 0.0028793 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
jvisual GC 监控如下
在windows系统上tomcat作为服务器,jvm参数启动,修改startup.bat文件,然后增加一行 set JAVA_OPTS= -XX:+PrintGCDetails -Xloggc:F:/tomcat_gc.log -Xms2048m -Xmx2048m -Xmn800m -XX:PermSize=128m -XX:MaxPermSize=128
在linux服务器上,就需要修改startup.sh,位置cygwin=false前 。注意要带上引号,
# OS specific support. $var _must_ be set to either true or false.
JAVA_OPTS="-XX:+PrintGCDetails -Xloggc:F:/tomcat_gc.log -Xms2048m -Xmx2048m -Xmn800m -XX:PermSize=128m -XX:MaxPermSize=128"
cygwin=false