有沒有想過,本身天天都在用的Eclipse:java
這篇文章,介紹一下我是如何對Eclipse進行調優,使得它的啓動速度提高了1.5秒的。算法
你可能會說,才1.5秒,須要說的是,個人電腦性能比較牛,優化前啓動Eclipse只須要5953ms,優化結束後是4693ms,提高了21%的速度,這就像百米賽跑,第一名的博爾特和最後一名的差了不到1秒鐘,但已經徹底是不一樣的境界了!app
下面讓咱們開始提速吧!eclipse
爲了方便計算Eclipse的啓動速度,這裏使用了周志明老師寫的一個Eclipse插件,該插件能夠在Eclipse啓動完成後,計算當前時間和Eclipse開始啓動時間直接的間隔,而後在界面上打印出總的啓動時間,就像這樣:編輯器
我一開始的eclipse.ini配置是這樣的:ide
-vm C:/Program Files/Java/jdk1.7.0_51/bin/javaw.exe -startup plugins/org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar --launcher.library plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.200.v20130521-0416 -product org.eclipse.epp.package.standard.product --launcher.defaultAction openFile --launcher.XXMaxPermSize 128M -showsplash org.eclipse.platform --launcher.XXMaxPermSize 128M --launcher.defaultAction openFile --launcher.appendVmargs -vmargs -Dosgi.requiredJavaVersion=1.6 -Xms40m -Xmx128M
指定了使用JDK7,初始堆大小(-Xms)是40m,最大堆(-Xmx)是128m.
接着,我啓動了三次Eclipse,三次的啓動時間分別是5960ms、5945ms、5954ms,取平均值以後,調優前的啓動時間是5953ms.
同時我經過使用Visual VM,記錄了調優前虛擬機的運行情況:工具
從Visual VM的監控信息看,啓動時間主要有三大塊:編譯時間、類加載時間以及垃圾回收的停頓時間。
其中,編譯時間是指JVM的JIT編譯,我是64位的機器,只能採用server[^footnote]模式,所以在即時編譯上沒有什麼優化的餘地。性能
所以,下面主要對耗時3.468秒的類加載時間,以及新生代(Eden Space)總耗時107ms的13次GC和老年代(Old Gen)總耗時156ms的Full GC,進行調優。優化
JVM的類加載,是指將Class文件,加載到虛擬機中。類加載的過程,包括加載、驗證、準備、解析、使用、卸載等階段。ui
其中驗證,是因爲Class文件不全都是由Java源碼編譯而來,Class文件可使用任何途徑產生,甚至能夠直接使用十六進制編輯器來編寫。所以虛擬機要對Class文件進行驗證。而對於Eclipse來講,虛擬機要加載的文件,基本都是咱們本身編寫的源碼,是值得信任的,所以,能夠加入參數-Xverify:none將類加載時的驗證階段去掉。
去掉以後,再來看Visual VM裏的類加載時間,能夠看到類加載時間一會兒降低到2s:
一樣的,再來啓動三次Eclipse,時間分別是4687ms、4700ms、4695ms,取平均值,去掉驗證階段後的Eclipse的啓動時間爲4694ms。
再來對垃圾收集進行調優,首先,咱們打印一下GC日誌,來看看如今Eclipse採用的是什麼垃圾收集器,在eclipse.ini中加入:
-XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:gc.log
啓動後查看gc.log:
0.373: [GC [PSYoungGen: 10752K->1528K(12288K)] 10752K->2853K(39936K), 0.0087200 secs] [Times: user=0.05 sys=0.01, real=0.01 secs] 0.641: [GC [PSYoungGen: 12280K->1512K(23040K)] 13605K->5449K(50688K), 0.0053889 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 1.205: [GC [PSYoungGen: 23016K->1522K(23040K)] 26953K->13258K(50688K), 0.0089067 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 1.667: [GC [PSYoungGen: 23026K->1515K(42496K)] 34762K->16585K(70144K), 0.0061016 secs] [Times: user=0.00 sys=0.02, real=0.01 secs] 1.809: [GC [PSYoungGen: 42475K->1515K(42496K)] 57545K->16937K(70144K), 0.0049001 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 2.372: [GC [PSYoungGen: 42475K->1528K(23040K)] 57897K->24396K(50688K), 0.0109632 secs] [Times: user=0.05 sys=0.02, real=0.01 secs] 2.383: [Full GC [PSYoungGen: 1528K->0K(23040K)] [ParOldGen: 22868K->22517K(51200K)] 24396K->22517K(74240K) [PSPermGen: 23460K->23448K(47104K)], 0.1348976 secs] [Times: user=0.38 sys=0.00, real=0.13 secs] 2.970: [GC [PSYoungGen: 21504K->8905K(30720K)] 44021K->31422K(81920K), 0.0067912 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 3.309: [GC [PSYoungGen: 30409K->9243K(30720K)] 52926K->31760K(81920K), 0.0084435 secs] [Times: user=0.05 sys=0.00, real=0.01 secs] 3.715: [GC [PSYoungGen: 27675K->12787K(31232K)] 50192K->36581K(82432K), 0.0124344 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 4.478: [GC [PSYoungGen: 31219K->12778K(28160K)] 55013K->36741K(79360K), 0.0183642 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] 4.976: [GC [PSYoungGen: 28138K->12745K(29696K)] 52101K->36707K(80896K), 0.0122746 secs] [Times: user=0.06 sys=0.00, real=0.01 secs]
經過其中的PSYoungGen和PSPermGen,能夠知道,採用的是新生代Parallel New + 老年代Parallell Old的吞吐量優先的組合,讓咱們來看一下下面這張圖:
兩個收集器之間有連線,表明他們能夠組合使用,考慮到Eclipse常常須要進行交互,所以吞吐量優先,這種適合後臺運行的應用的組合,顯得沒什麼必要,咱們能夠採用新生代ParNew + 老年代CMS的下降停頓優先組合。
咱們往eclipse.ini加入以下配置:
-XX:+UseConcMarkSweepGC -XX:+UseParNewGC
接着啓動Eclipse,觀察Visual VM:
有點懵逼了,新生代的GC次數一會兒去到了22次,老年代的GC也從一次暴漲到了10次,不過,雖然GC次數暴漲了,可是垃圾收集的時間並無增長太多,看來這個組合的收集器仍是很給力的,如今咱們看一下GC日誌,分析一下發生那麼屢次GC的緣由:
0.404: [GC0.404: [ParNew: 10944K->1344K(12288K), 0.0061580 secs] 10944K->2856K(39616K), 0.0062909 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 0.698: [GC0.698: [ParNew: 12288K->1344K(12288K), 0.0072989 secs] 13800K->5854K(39616K), 0.0073625 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 1.051: [GC1.051: [ParNew: 12288K->1344K(12288K), 0.0083077 secs] 16798K->9368K(39616K), 0.0083701 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 1.369: [GC1.369: [ParNew: 12288K->1344K(12288K), 0.0082115 secs] 20312K->13721K(39616K), 0.0082795 secs] [Times: user=0.05 sys=0.00, real=0.01 secs] 1.699: [GC1.699: [ParNew: 12288K->1344K(12288K), 0.0066972 secs] 24665K->16288K(39616K), 0.0067814 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 1.706: [GC [1 CMS-initial-mark: 14944K(27328K)] 16460K(39616K), 0.0011819 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 1.707: [CMS-concurrent-mark-start] 1.728: [CMS-concurrent-mark: 0.021/0.021 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 1.729: [CMS-concurrent-preclean-start] 1.729: [CMS-concurrent-preclean: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 1.729: [CMS-concurrent-abortable-preclean-start] 1.786: [GC1.786: [ParNew: 12288K->934K(12288K), 0.0031670 secs] 27232K->16666K(39616K), 0.0032384 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 1.832: [GC1.832: [ParNew: 11878K->490K(12288K), 0.0012284 secs] 27610K->16222K(39616K), 0.0013088 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 1.869: [GC1.869: [ParNew: 11434K->622K(12288K), 0.0008751 secs] 27166K->16354K(39616K), 0.0009302 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 1.902: [GC1.902: [ParNew: 11566K->754K(12288K), 0.0008866 secs] 27298K->16486K(39616K), 0.0009452 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 1.934: [GC1.934: [ParNew: 11698K->891K(12288K), 0.0009875 secs] 27430K->16622K(39616K), 0.0010413 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 1.966: [GC1.966: [ParNew: 11835K->699K(12288K), 0.0010225 secs] 27566K->16431K(39616K), 0.0011007 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 2.079: [CMS-concurrent-abortable-preclean: 0.048/0.350 secs] [Times: user=0.53 sys=0.02, real=0.35 secs] 2.079: [GC[YG occupancy: 6449 K (12288 K)]2.079: [Rescan (parallel) , 0.0031854 secs]2.082: [weak refs processing, 0.0001384 secs]2.083: [scrub string table, 0.0004222 secs] [1 CMS-remark: 15731K(27328K)] 22180K(39616K), 0.0038528 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 2.083: [CMS-concurrent-sweep-start] 2.088: [CMS-concurrent-sweep: 0.005/0.005 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 2.088: [CMS-concurrent-reset-start] 2.089: [CMS-concurrent-reset: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 2.269: [GC2.269: [ParNew: 11643K->1344K(12288K), 0.0078219 secs] 20634K->13437K(39616K), 0.0078808 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 2.484: [GC2.484: [ParNew: 12288K->1344K(12288K), 0.0076834 secs] 24381K->15864K(39616K), 0.0079210 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 2.718: [GC2.718: [ParNew: 12095K->1344K(12288K), 0.0085918 secs] 26615K->20205K(39616K), 0.0086568 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 2.727: [GC [1 CMS-initial-mark: 18861K(27328K)] 20455K(39616K), 0.0009695 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 2.728: [CMS-concurrent-mark-start] 2.777: [CMS-concurrent-mark: 0.048/0.049 secs] [Times: user=0.13 sys=0.00, real=0.05 secs] 2.777: [CMS-concurrent-preclean-start] 2.778: [CMS-concurrent-preclean: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 2.779: [GC[YG occupancy: 3358 K (12288 K)]2.779: [Rescan (parallel) , 0.0007499 secs]2.779: [weak refs processing, 0.0001568 secs]2.780: [scrub string table, 0.0004098 secs] [1 CMS-remark: 18861K(27328K)] 22219K(39616K), 0.0013938 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 2.780: [CMS-concurrent-sweep-start] 2.790: [CMS-concurrent-sweep: 0.010/0.010 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 2.790: [CMS-concurrent-reset-start] 2.791: [CMS-concurrent-reset: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 3.027: [GC3.027: [ParNew: 12288K->1344K(12288K), 0.0070788 secs] 26519K->20052K(39616K), 0.0071626 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 3.034: [GC [1 CMS-initial-mark: 18708K(27328K)] 20069K(39616K), 0.0016822 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 3.036: [CMS-concurrent-mark-start] 3.080: [CMS-concurrent-mark: 0.044/0.044 secs] [Times: user=0.13 sys=0.00, real=0.04 secs] 3.080: [CMS-concurrent-preclean-start] 3.081: [CMS-concurrent-preclean: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 3.081: [GC[YG occupancy: 3035 K (12288 K)]3.081: [Rescan (parallel) , 0.0014130 secs]3.082: [weak refs processing, 0.0000222 secs]3.082: [scrub string table, 0.0008106 secs] [1 CMS-remark: 18708K(27328K)] 21744K(39616K), 0.0023372 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 3.083: [CMS-concurrent-sweep-start] 3.093: [CMS-concurrent-sweep: 0.009/0.009 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 3.093: [CMS-concurrent-reset-start] 3.093: [CMS-concurrent-reset: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 3.367: [GC3.367: [ParNew: 12288K->1344K(12288K), 0.0092673 secs] 29323K->21524K(40684K), 0.0093494 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 3.376: [GC [1 CMS-initial-mark: 20180K(28396K)] 21539K(40684K), 0.0022484 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 3.379: [CMS-concurrent-mark-start] 3.440: [CMS-concurrent-mark: 0.061/0.061 secs] [Times: user=0.23 sys=0.02, real=0.06 secs] 3.440: [CMS-concurrent-preclean-start] 3.442: [CMS-concurrent-preclean: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 3.442: [CMS-concurrent-abortable-preclean-start] 3.493: [GC3.493: [ParNew: 12274K->1343K(12288K), 0.0055261 secs] 32455K->23901K(40684K), 0.0056034 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 3.696: [GC3.696: [ParNew: 12286K->1344K(12288K), 0.0069481 secs] 34843K->27462K(40876K), 0.0070168 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 3.703: [CMS-concurrent-abortable-preclean: 0.060/0.262 secs] [Times: user=0.50 sys=0.01, real=0.26 secs] 3.704: [GC[YG occupancy: 1572 K (12288 K)]3.704: [Rescan (parallel) , 0.0025590 secs]3.706: [weak refs processing, 0.0001688 secs]3.706: [scrub string table, 0.0008541 secs] [1 CMS-remark: 26118K(28588K)] 27690K(40876K), 0.0036849 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 3.707: [CMS-concurrent-sweep-start] 3.717: [CMS-concurrent-sweep: 0.009/0.010 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 3.717: [CMS-concurrent-reset-start] 3.717: [CMS-concurrent-reset: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 4.034: [GC4.034: [ParNew: 12288K->1344K(12288K), 0.0080462 secs] 36118K->28963K(52008K), 0.0081184 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 4.553: [GC4.553: [ParNew: 12288K->1344K(12288K), 0.0073523 secs] 39907K->31479K(52008K), 0.0074360 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 4.560: [GC [1 CMS-initial-mark: 30135K(39720K)] 31479K(52008K), 0.0031662 secs] [Times: user=0.03 sys=0.00, real=0.00 secs] 4.564: [CMS-concurrent-mark-start] 4.676: [CMS-concurrent-mark: 0.100/0.112 secs] [Times: user=0.39 sys=0.00, real=0.11 secs] 4.676: [CMS-concurrent-preclean-start] 4.677: [CMS-concurrent-preclean: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 4.677: [CMS-concurrent-abortable-preclean-start] 5.050: [GC5.050: [ParNew: 12288K->1343K(12288K), 0.0058341 secs] 42423K->32844K(52008K), 0.0059602 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 5.425: [GC5.425: [ParNew: 12287K->1344K(12288K), 0.0041361 secs] 43788K->33574K(52008K), 0.0041972 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 5.429: [CMS-concurrent-abortable-preclean: 0.144/0.752 secs] [Times: user=2.28 sys=0.27, real=0.75 secs] 5.429: [GC[YG occupancy: 1443 K (12288 K)]5.429: [Rescan (parallel) , 0.0018369 secs]5.431: [weak refs processing, 0.0001649 secs]5.431: [scrub string table, 0.0007892 secs] [1 CMS-remark: 32230K(39720K)] 33673K(52008K), 0.0028944 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 5.432: [CMS-concurrent-sweep-start] 5.447: [CMS-concurrent-sweep: 0.014/0.014 secs] [Times: user=0.03 sys=0.00, real=0.01 secs] 5.447: [CMS-concurrent-reset-start] 5.447: [CMS-concurrent-reset: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 25.095: [GC25.096: [ParNew: 12288K->1344K(12288K), 0.0236873 secs] 39467K->29787K(57588K), 0.0239223 secs] [Times: user=0.05 sys=0.00, real=0.02 secs]
咱們看到,新生代Eden區的容量很是小,只有12288K,約合10m,老年代也一樣如此(並且CMS因爲其算法的特殊性,是不會等到老年代所有佔滿了才進行Full GC的,緣由:CMS收集器)。
在我12G的機器下,大能夠給Eclipse分配多得多的內存,因而將堆的最大容量(-Xmx)設置爲1g,初始堆大小(-Xms)也設置爲1g,防止運行時自動擴展耗費時間,永久代(-XX:PermSize、-XX:MaxPermSize)設置爲256m,新生代(-Xmn)設置爲512m。
因爲這樣設置的話,內存必定是足夠的了,所以還可使用-XX:+DisableExplicitGC來防止Eclipse主動調用System.gc():
-Xms1024M -Xmx1024M -Xmn512M -XX:PermSize=256M -XX:MaxPermSize=256M -XX:+DisableExplicitGC
修改完以後,再來啓動Eclipse,查看Visual VM:
能夠看到,不論是新生代仍是老年代,都再也不有GC發生了,再來統計一下啓動時間,三次啓動分別爲4590ms、4710ms、4780ms,取平均值,進行垃圾收集器優化以後的啓動時間是4693ms。
最終調優完成後的eclipse.ini是這樣的:
-vm C:/Program Files/Java/jdk1.7.0_51/bin/javaw.exe -startup plugins/org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar --launcher.library plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.200.v20130521-0416 -product org.eclipse.epp.package.standard.product --launcher.defaultAction openFile --launcher.XXMaxPermSize 128M -showsplash org.eclipse.platform --launcher.XXMaxPermSize 128M --launcher.defaultAction openFile --launcher.appendVmargs -vmargs -Dosgi.requiredJavaVersion=1.6 -Xverify:none -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:gc.log -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -Xms40m -Xmx128M
從一開始的5953ms,到最後的4693ms,好吧,我不得不認可省下的這一秒鐘啓動時間,並不會對我又多大幫助,不過,經過這個例子,我也演示了JVM調優中的一些套路:
這些東西都是一樣能夠應用到生存環境的。
以上,但願能對你有所幫助。